Skip to content

Commit

Permalink
outline_nodes: extract notify
Browse files Browse the repository at this point in the history
  • Loading branch information
sorax committed Mar 16, 2024
1 parent bfa50f8 commit c45fe13
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 53 deletions.
70 changes: 35 additions & 35 deletions assets/js/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,31 +116,31 @@ export const Hooks = {
this.pushEvent("delete_node", nextNode.uuid)
break

// case "Tab":
// event.preventDefault()

// if (event.shiftKey) {
// if (node.parent_id) {
// // outdent
// node.prev_id = node.parent_id
// node.parent_id = undefined
// updateItem(node, container)

// focusItem(item)
// this.pushEvent("update_node", node)
// }
// } else {
// if (node.prev_id) {
// // indent
// node.parent_id = node.prev_id
// node.prev_id = undefined // TODO: prev_id should be the id of the last child of the parent node
// updateItem(node, container)

// focusItem(item)
// this.pushEvent("update_node", node)
// }
// }
// break
case "Tab":
event.preventDefault()

if (event.shiftKey) {
if (node.parent_id) {
// outdent
node.prev_id = node.parent_id
node.parent_id = undefined
updateItem(node, container)

focusItem(item)
this.pushEvent("update_node", node)
}
} else {
if (node.prev_id) {
// indent
node.parent_id = node.prev_id
node.prev_id = undefined // TODO: prev_id should be the id of the last child of the parent node
updateItem(node, container)

focusItem(item)
this.pushEvent("update_node", node)
}
}
break
}
})

Expand Down Expand Up @@ -170,18 +170,18 @@ export const Hooks = {
focusItem(lastItem)
})

// this.handleEvent("insert", (node: Node) => {
// const item = createItem(node)
// container.append(item)
// })
this.handleEvent("insert", (node: Node) => {
const item = createItem(node)
container.append(item)
})

// this.handleEvent("update", (node: Node) => {
// updateItem(node, container)
// })
this.handleEvent("update", (node: Node) => {
updateItem(node, container)
})

// this.handleEvent("delete", (node: Node) => {
// deleteItem(node)
// })
this.handleEvent("delete", (node: Node) => {
deleteItem(node)
})
}
}
}
31 changes: 19 additions & 12 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,30 @@ defmodule Radiator.Outline do
@moduledoc """
The Outline context.
"""

import Ecto.Query, warn: false

alias Phoenix.PubSub
alias Radiator.Outline.Node
alias Radiator.Outline.Notify
alias Radiator.Repo

@topic "outline-node"
def create(attrs \\ %{}, socket_id \\ nil) do
attrs
|> create_node()
|> Notify.broadcast_node_action(:insert, socket_id)
end

def update(%Node{} = node, attrs, socket_id \\ nil) do
node
|> update_node(attrs)
|> Notify.broadcast_node_action(:update, socket_id)
end

def delete(%Node{} = node, socket_id \\ nil) do
node
|> delete()
|> Notify.broadcast_node_action(:delete, socket_id)
end

@doc """
Returns the list of nodes.
Expand Down Expand Up @@ -94,7 +111,6 @@ defmodule Radiator.Outline do
%Node{}
|> Node.changeset(attrs)
|> Repo.insert()
|> broadcast_node_action(:insert)
end

@doc """
Expand All @@ -113,7 +129,6 @@ defmodule Radiator.Outline do
node
|> Node.changeset(attrs)
|> Repo.update()
|> broadcast_node_action(:update)
end

@doc """
Expand All @@ -131,7 +146,6 @@ defmodule Radiator.Outline do
def delete_node(%Node{} = node) do
node
|> Repo.delete()
|> broadcast_node_action(:delete)
end

@doc """
Expand All @@ -146,11 +160,4 @@ defmodule Radiator.Outline do
def change_node(%Node{} = node, attrs \\ %{}) do
Node.changeset(node, attrs)
end

defp broadcast_node_action({:ok, node}, action) do
PubSub.broadcast(Radiator.PubSub, @topic, {action, node})
{:ok, node}
end

defp broadcast_node_action({:error, error}, _action), do: {:error, error}
end
17 changes: 17 additions & 0 deletions lib/radiator/outline/notify.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Radiator.Outline.Notify do
@moduledoc """
Inform all connected users on changes.
"""

alias Phoenix.PubSub

@topic "outline-node"

def broadcast_node_action({:ok, node}, action, socket_id) do
PubSub.broadcast(Radiator.PubSub, @topic, {action, node, socket_id})

{:ok, node}
end

def broadcast_node_action({:error, error}, _action, _socket_id), do: {:error, error}
end
17 changes: 11 additions & 6 deletions lib/radiator_web/live/episode_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule RadiatorWeb.EpisodeLive.Index do
episode = socket.assigns.selected_episode
attrs = Map.merge(params, %{"creator_id" => user.id, "episode_id" => episode.id})

case Outline.create_node(attrs) do
case Outline.create(attrs, socket.id) do
{:ok, node} -> socket |> reply(:reply, Map.put(node, :temp_id, temp_id))
_ -> socket |> reply(:noreply)
end
Expand All @@ -61,7 +61,7 @@ defmodule RadiatorWeb.EpisodeLive.Index do

case Outline.get_node(uuid) do
nil -> nil
node -> Outline.update_node(node, attrs)
node -> Outline.update(node, attrs, socket.id)
end

socket
Expand All @@ -71,27 +71,32 @@ defmodule RadiatorWeb.EpisodeLive.Index do
def handle_event("delete_node", node_id, socket) do
case Outline.get_node(node_id) do
nil -> nil
node -> Outline.delete_node(node)
node -> Outline.delete(node, socket.id)
end

socket
|> reply(:noreply)
end

@impl true
def handle_info({:insert, node}, socket) do
def handle_info({_, _node, socket_id}, socket) when socket_id == socket.id do
socket
|> reply(:noreply)
end

def handle_info({:insert, node, _socket_id}, socket) do
socket
|> push_event("insert", node)
|> reply(:noreply)
end

def handle_info({:update, node}, socket) do
def handle_info({:update, node, _socket_id}, socket) do
socket
|> push_event("update", node)
|> reply(:noreply)
end

def handle_info({:delete, node}, socket) do
def handle_info({:delete, node, _socket_id}, socket) do
socket
|> push_event("delete", node)
|> reply(:noreply)
Expand Down

0 comments on commit c45fe13

Please sign in to comment.