From d7a5b4252e2b4384897af014a86da4f7a4deeac4 Mon Sep 17 00:00:00 2001 From: sorax Date: Tue, 23 Jan 2024 20:24:39 +0100 Subject: [PATCH] feat: create & delete networks --- lib/radiator_web/live/admin_live/index.ex | 47 +++++++++++++++++++ .../live/admin_live/index.html.heex | 29 +++++++++++- lib/radiator_web/live/episode_live/index.ex | 7 +-- 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/lib/radiator_web/live/admin_live/index.ex b/lib/radiator_web/live/admin_live/index.ex index bdc20149..8e5e5078 100644 --- a/lib/radiator_web/live/admin_live/index.ex +++ b/lib/radiator_web/live/admin_live/index.ex @@ -8,6 +8,7 @@ defmodule RadiatorWeb.AdminLive.Index do @impl true def mount(_params, _session, socket) do socket + |> assign(:action, nil) |> assign(:page_title, "Admin Dashboard") |> assign(:page_description, "Tools to create and manage your prodcasts") |> assign(:networks, Podcast.list_networks(preload: :shows)) @@ -15,6 +16,52 @@ defmodule RadiatorWeb.AdminLive.Index do |> reply(:ok) end + @impl true + def handle_event("new", _params, socket) do + network = %Podcast.Network{} + changeset = Podcast.change_network(network) + + socket + |> assign(:action, :new) + |> assign(:network, network) + |> assign(:form, to_form(changeset)) + |> reply(:noreply) + end + + def handle_event("validate", %{"network" => params}, socket) do + changeset = + socket.assigns.network + |> Podcast.change_network(params) + |> Map.put(:action, :validate) + + socket + |> assign(:form, to_form(changeset)) + |> reply(:noreply) + end + + def handle_event("save", %{"network" => params}, socket) do + case Podcast.create_network(params) do + {:ok, _network} -> + socket + |> put_flash(:info, "Network created successfully") + |> reply(:noreply) + + {:error, %Ecto.Changeset{} = changeset} -> + socket + |> assign(:form, to_form(changeset)) + |> reply(:noreply) + end + end + + def handle_event("delete", %{"id" => id}, socket) do + network = Podcast.get_network!(id) + {:ok, _} = Podcast.delete_network(network) + + socket + # |> stream_delete(:networks, network)} + |> reply(:noreply) + end + defp get_bookmarklet(api_uri, socket) do token = socket.assigns.current_user diff --git a/lib/radiator_web/live/admin_live/index.html.heex b/lib/radiator_web/live/admin_live/index.html.heex index 8e9655b3..eda5a877 100644 --- a/lib/radiator_web/live/admin_live/index.html.heex +++ b/lib/radiator_web/live/admin_live/index.html.heex @@ -1,7 +1,13 @@
-

<%= network.title %>

+

+ <%= network.title %> + +

+
<.link :for={{show, i} <- Enum.with_index(network.shows)} @@ -13,7 +19,26 @@
- + + + <.simple_form + :if={@action == :new} + for={@form} + id="network-form" + phx-change="validate" + phx-submit="save" + > + <.input field={@form[:title]} type="text" label="Title" /> + <:actions> + <.button phx-disable-with="Saving...">Save Network + +
diff --git a/lib/radiator_web/live/episode_live/index.ex b/lib/radiator_web/live/episode_live/index.ex index 5e49563d..23622ba5 100644 --- a/lib/radiator_web/live/episode_live/index.ex +++ b/lib/radiator_web/live/episode_live/index.ex @@ -72,9 +72,10 @@ defmodule RadiatorWeb.EpisodeLive.Index do end def handle_event("delete_node", node_id, socket) do - node_id - |> Outline.get_node!() - |> Outline.delete_node() + case Outline.get_node(node_id) do + nil -> nil + node -> Outline.delete_node(node) + end socket |> reply(:noreply)