Skip to content

Commit

Permalink
enhance events with more data
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Jun 18, 2024
1 parent 3f49c27 commit caceb60
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 21 deletions.
15 changes: 14 additions & 1 deletion assets/js/hooks/events/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
deleteItem,
focusItem,
moveItem,
getItemById,
} from "../item";
import { getNodeByItem } from "../node";

export function handleList({ nodes }: { nodes: Node[] }) {
const container: HTMLDivElement = this.el.querySelector(".children");
Expand Down Expand Up @@ -36,12 +38,23 @@ export function handleList({ nodes }: { nodes: Node[] }) {
focusItem(lastItem);
}

export function handleInsert({ node }: { node: Node }) {
export function handleInsert({
node,
next_id,
}: {
node: Node;
next_id: string | undefined;
}) {
const container: HTMLDivElement = this.el.querySelector(".children");

const item = createItem(node);
container.append(item);
moveItem(node, container);
const nextItem = getItemById(next_id) as HTMLDivElement;
const nextNode = getNodeByItem(nextItem);
nextNode.prev_id = node.uuid;
nextNode.dirty = false;
moveItem(nextNode, container);
}

export function handleContentChange({ node }: { node: Node }) {
Expand Down
2 changes: 1 addition & 1 deletion assets/js/hooks/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export function getItemByNode({ uuid }: Node) {
return getItemById(uuid);
}

function getItemById(uuid: string | undefined) {
export function getItemById(uuid: string | undefined) {
if (!uuid) return null;

return document.getElementById(`outline-node-${uuid}`) as HTMLDivElement;
Expand Down
22 changes: 18 additions & 4 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
defmodule Radiator.Outline.NodeRepoResult do
@moduledoc """
Generic result structure for node operations.
"""
defstruct [
:node,
:old_prev_id,
:old_next_id,
:next_id,
:children
]
end

defmodule Radiator.Outline do
@moduledoc """
The Outline context.
Expand All @@ -6,6 +19,7 @@ defmodule Radiator.Outline do
import Ecto.Query, warn: false

alias Radiator.Outline.Node
alias Radiator.Outline.NodeRepoResult
alias Radiator.Outline.NodeRepository
alias Radiator.Repo

Expand All @@ -17,7 +31,7 @@ defmodule Radiator.Outline do
## Examples
iex> insert_node(%{content: 'foo'})
{:ok, %Node{}}
{:ok, %NodeRepoResult{}}
iex> insert_node(%{content: value})
{:error, :parent_and_prev_not_consistent}
Expand All @@ -34,7 +48,7 @@ defmodule Radiator.Outline do
parent_node_id = attrs["parent_id"]
episode_id = attrs["episode_id"]
# find Node which has been previously connected to prev_node
node_to_move =
next_node =
Node
|> where(episode_id: ^episode_id)
|> where_prev_node_equals(prev_node_id)
Expand All @@ -45,9 +59,9 @@ defmodule Radiator.Outline do
prev_node <- NodeRepository.get_node_if(prev_node_id),
true <- parent_and_prev_consistent?(parent_node, prev_node),
{:ok, node} <- NodeRepository.create_node(attrs),
{:ok, _node_to_move} <- move_node_if(node_to_move, nil, node.uuid),
{:ok, _node_to_move} <- move_node_if(next_node, nil, node.uuid),
{:ok, node} <- move_node_if(node, parent_node_id, prev_node_id) do
node
%NodeRepoResult{node: node, next_id: get_node_id(next_node)}
else
false ->
Repo.rollback("Insert node failed. Parent and prev node are not consistent.")
Expand Down
2 changes: 1 addition & 1 deletion lib/radiator/outline/event/node_deleted_event.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Radiator.Outline.Event.NodeDeletedEvent do
@moduledoc false
defstruct [:event_id, :node_id, :user_id]
defstruct [:event_id, :node_id, :user_id, :children]
end
2 changes: 1 addition & 1 deletion lib/radiator/outline/event/node_inserted_event.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Radiator.Outline.Event.NodeInsertedEvent do
@moduledoc false

defstruct [:event_id, :node, :user_id]
defstruct [:event_id, :node, :user_id, :next_id]
end
11 changes: 10 additions & 1 deletion lib/radiator/outline/event/node_moved_event.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
defmodule Radiator.Outline.Event.NodeMovedEvent do
@moduledoc false
defstruct [:event_id, :node_id, :parent_id, :prev_id, :user_id]
defstruct [
:event_id,
:node_id,
:parent_id,
:prev_id,
:user_id,
:old_prev_id,
:old_next_id,
:next_id
]
end
10 changes: 8 additions & 2 deletions lib/radiator/outline/event_consumer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Radiator.Outline.EventConsumer do

alias Radiator.EventStore
alias Radiator.Outline
alias Radiator.Outline.NodeRepoResult

alias Radiator.Outline.Command.{
ChangeNodeContentCommand,
Expand Down Expand Up @@ -80,8 +81,13 @@ defmodule Radiator.Outline.EventConsumer do
:ok
end

defp handle_insert_node_result({:ok, node}, command) do
%NodeInsertedEvent{node: node, event_id: command.event_id, user_id: command.user_id}
defp handle_insert_node_result({:ok, %NodeRepoResult{node: node, next_id: next_id}}, command) do
%NodeInsertedEvent{
node: node,
event_id: command.event_id,
user_id: command.user_id,
next_id: next_id
}
|> EventStore.persist_event()
|> Dispatch.broadcast()

Expand Down
2 changes: 1 addition & 1 deletion lib/radiator_web/controllers/api/outline_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ defmodule RadiatorWeb.Api.OutlineController do
})
end

defp get_response({:ok, node}), do: {200, %{uuid: node.uuid}}
defp get_response({:ok, %{node: node}}), do: {200, %{uuid: node.uuid}}
defp get_response({:error, _}), do: {400, %{error: "params"}}
end
4 changes: 2 additions & 2 deletions lib/radiator_web/live/episode_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ defmodule RadiatorWeb.EpisodeLive.Index do
|> reply(:noreply)
end

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

Expand Down
14 changes: 7 additions & 7 deletions test/radiator/outline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule Radiator.OutlineTest do
"prev_id" => node_3.uuid
}

assert {:ok, %Node{uuid: node3_1_uuid} = node} = Outline.insert_node(node_attrs)
assert {:ok, %{node: %Node{uuid: node3_1_uuid} = node}} = Outline.insert_node(node_attrs)

assert node.parent_id == node_3.parent_id
assert node.prev_id == node_3.uuid
Expand Down Expand Up @@ -106,7 +106,7 @@ defmodule Radiator.OutlineTest do
"prev_id" => nested_node_1.uuid
}

{:ok, new_node} = Outline.insert_node(node_attrs)
{:ok, %{node: new_node}} = Outline.insert_node(node_attrs)
assert new_node.parent_id == node_3.uuid
end

Expand All @@ -121,7 +121,7 @@ defmodule Radiator.OutlineTest do
"prev_id" => nested_node_1.uuid
}

{:ok, new_node} = Outline.insert_node(node_attrs)
{:ok, %{node: new_node}} = Outline.insert_node(node_attrs)
assert new_node.prev_id == nested_node_1.uuid
end

Expand All @@ -137,7 +137,7 @@ defmodule Radiator.OutlineTest do
"prev_id" => nested_node_1.uuid
}

{:ok, new_node} = Outline.insert_node(node_attrs)
{:ok, %{node: new_node}} = Outline.insert_node(node_attrs)

assert NodeRepository.get_node!(nested_node_2.uuid).prev_id == new_node.uuid
assert new_node.prev_id == nested_node_1.uuid
Expand All @@ -156,7 +156,7 @@ defmodule Radiator.OutlineTest do
"prev_id" => nested_node_2.uuid
}

{:ok, new_node} = Outline.insert_node(node_attrs)
{:ok, %{node: new_node}} = Outline.insert_node(node_attrs)

assert NodeRepository.get_node!(nested_node_2.uuid).prev_id == nested_node_1.uuid
assert new_node.prev_id == nested_node_2.uuid
Expand All @@ -174,7 +174,7 @@ defmodule Radiator.OutlineTest do
"parent_id" => node_3.uuid
}

{:ok, new_node} = Outline.insert_node(node_attrs)
{:ok, %{node: new_node}} = Outline.insert_node(node_attrs)

assert new_node.prev_id == nil
assert NodeRepository.get_node!(nested_node_1.uuid).prev_id == new_node.uuid
Expand All @@ -187,7 +187,7 @@ defmodule Radiator.OutlineTest do
# another node in another episode without parent and prev node
node_fixture(parent_id: nil, prev_id: nil)
node_attrs = %{"content" => "new node", "episode_id" => parent_node.episode_id}
{:ok, new_node} = Outline.insert_node(node_attrs)
{:ok, %{node: new_node}} = Outline.insert_node(node_attrs)

assert new_node.prev_id == nil
assert new_node.parent_id == nil
Expand Down

0 comments on commit caceb60

Please sign in to comment.