Skip to content

Commit

Permalink
create initial node for new episodes
Browse files Browse the repository at this point in the history
  • Loading branch information
sorax committed Aug 1, 2024
1 parent 801d6ba commit 45a6daf
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 31 deletions.
16 changes: 8 additions & 8 deletions assets/js/hooks/events/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import { getNodeByItem } from "../node";
export function handleList({ nodes }: { nodes: Node[] }) {
const container: HTMLDivElement = this.el.querySelector(".children");

if (nodes.length == 0) {
const node: Node = {
uuid: self.crypto.randomUUID(),
content: "",
dirty: true,
};
nodes = [node];
}
// if (nodes.length == 0) {
// const node: Node = {
// uuid: self.crypto.randomUUID(),
// content: "",
// dirty: true,
// };
// nodes = [node];
// }

// add all items
nodes.forEach((node) => {
Expand Down
54 changes: 31 additions & 23 deletions lib/radiator_web/live/episode_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule RadiatorWeb.EpisodeLive.Index do
NodeMovedEvent
}

alias Radiator.Outline.NodeRepository
# alias Radiator.EventStore
alias Radiator.Podcast
alias Radiator.Podcast.Episode
Expand Down Expand Up @@ -120,35 +121,18 @@ defmodule RadiatorWeb.EpisodeLive.Index do
end

def handle_event("validate", %{"episode" => params}, socket) do
changeset = socket.assigns.episode |> Episode.changeset(params) |> Map.put(:action, :validate)
changeset = Episode.changeset(socket.assigns.episode, params)

socket
|> assign(:form, to_form(changeset))
|> assign(:form, to_form(changeset, action: :validate))
|> reply(:noreply)
end

def handle_event("save", %{"episode" => params}, socket) do
show_id = socket.assigns.show.id

params = Map.put(params, "show_id", show_id)

case Podcast.create_episode(params) do
{:ok, episode} ->
show = Podcast.get_show!(show_id, preload: :episodes)

socket
|> assign(:action, nil)
|> assign(:episodes, show.episodes)
|> put_flash(:info, "Episode created successfully")
|> push_patch(to: ~p"/admin/podcast/#{show}/#{episode}")
|> reply(:noreply)

{:error, %Ecto.Changeset{} = changeset} ->
socket
|> assign(:form, to_form(changeset))
|> put_flash(:info, "Episode could not be created")
|> reply(:noreply)
end
params
|> Map.put("show_id", socket.assigns.show.id)
|> Podcast.create_episode()
|> process_create_episode(socket)
end

@impl true
Expand Down Expand Up @@ -237,4 +221,28 @@ defmodule RadiatorWeb.EpisodeLive.Index do
socket
|> stream_insert(:event_logs, event, at: 0)
end

defp process_create_episode({:ok, episode}, socket) do
show = Podcast.get_show!(socket.assigns.show.id, preload: :episodes)

NodeRepository.create_node(%{
"uuid" => Ecto.UUID.generate(),
"creator_id" => socket.assigns.current_user.id,
"episode_id" => episode.id
})

socket
|> assign(:action, nil)
|> assign(:episodes, show.episodes)
|> put_flash(:info, "Episode created successfully")
|> push_navigate(to: ~p"/admin/podcast/#{show}/#{episode}")
|> reply(:noreply)
end

defp process_create_episode({:error, %Ecto.Changeset{} = changeset}, socket) do
socket
|> assign(:form, to_form(changeset))
|> put_flash(:info, "Episode could not be created")
|> reply(:noreply)
end
end
24 changes: 24 additions & 0 deletions test/radiator_web/live/episode_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ defmodule RadiatorWeb.EpisodeLiveTest do

assert page_title(live) =~ show.title
end

test "create new episode will create node as well", %{conn: conn, show: show} do
{:ok, live, _html} = live(conn, ~p"/admin/podcast/#{show.id}")

live
|> element(~s{main aside button}, "Create Episode")
|> render_click()

submit_live =
live
|> form("#episode-form", episode: %{title: "Some new episode"})
|> render_submit()

{path, %{"info" => "Episode created successfully"}} = assert_redirect(live)

episode_id = path |> Path.basename() |> String.to_integer()

{:ok, new_live, _html} = submit_live |> follow_redirect(conn)

assert new_live |> has_element?(~s{main h2}, "Some new episode")

assert_push_event(new_live, "list", %{nodes: [node]})
%{content: nil, parent_id: nil, prev_id: nil, episode_id: ^episode_id} = node
end
end

describe "Episode outline nodes" do
Expand Down

0 comments on commit 45a6daf

Please sign in to comment.