Skip to content

Commit

Permalink
feat: create & delete networks
Browse files Browse the repository at this point in the history
  • Loading branch information
sorax committed Jan 27, 2024
1 parent 1200cca commit d7a5b42
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
47 changes: 47 additions & 0 deletions lib/radiator_web/live/admin_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,60 @@ 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))
|> assign(:bookmarklet, get_bookmarklet(Endpoint.url() <> "/api/v1/outline", socket))
|> 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
Expand Down
29 changes: 27 additions & 2 deletions lib/radiator_web/live/admin_live/index.html.heex
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<div>
<section class="my-12">
<div :for={network <- @networks}>
<h2 class="my-4 text-2xl"><%= network.title %></h2>
<h2 class="my-4 text-2xl">
<%= network.title %>
<button phx-click="delete" phx-value-id={network.id} data-confirm="Are you sure?">
<.icon name="hero-x-circle" class="w-6 h-6" />
</button>
</h2>

<div class="grid grid-cols-3 gap-6 sm:grid-cols-6">
<.link
:for={{show, i} <- Enum.with_index(network.shows)}
Expand All @@ -13,7 +19,26 @@
</.link>
</div>
</div>
<button class="my-4 rounded text-white bg-[#df7366] px-8 py-2">Create Network</button>
<button
:if={@action != :new}
class="my-4 rounded text-white bg-[#df7366] px-8 py-2"
phx-click="new"
>
Create Network
</button>

<.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</.button>
</:actions>
</.simple_form>
</section>

<section class="my-12">
Expand Down
7 changes: 4 additions & 3 deletions lib/radiator_web/live/episode_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d7a5b42

Please sign in to comment.