live_beats/lib/live_beats_web/live/song_live/index.ex

93 lines
2.6 KiB
Elixir
Raw Normal View History

2021-10-29 16:12:23 +00:00
defmodule LiveBeatsWeb.SongLive.Index do
use LiveBeatsWeb, :live_view
alias LiveBeats.MediaLibrary
alias LiveBeats.MediaLibrary.Song
2021-11-02 03:25:28 +00:00
alias LiveBeatsWeb.SongLive.DeleteDialogComponent
2021-10-29 16:12:23 +00:00
def render(assigns) do
~H"""
<.title_bar>
Listing Songs
<:actions>
2021-11-02 03:25:28 +00:00
<.button primary patch_to={Routes.song_index_path(@socket, :new)}>Upload Songs</.button>
2021-10-29 16:12:23 +00:00
</:actions>
</.title_bar>
<%= if @live_action in [:new, :edit] do %>
<.modal show id="add-songs" return_to={Routes.song_index_path(@socket, :index)}>
<.live_component
2021-11-01 19:57:53 +00:00
module={LiveBeatsWeb.SongLive.UploadFormComponent}
2021-10-29 16:12:23 +00:00
title={@page_title}
id={@song.id || :new}
action={@live_action}
return_to={Routes.song_index_path(@socket, :index)}
song={@song}
2021-11-01 01:19:52 +00:00
genres={@genres}
2021-10-29 16:12:23 +00:00
/>
</.modal>
<% end %>
2021-11-02 16:24:53 +00:00
<.modal id="delete-modal">
<:cancel>Cancel</:cancel>
<:confirm>Delete</:confirm>
</.modal>
2021-11-02 03:25:28 +00:00
<.table rows={@songs} row_id={fn song -> "song-#{song.id}" end}>
2021-10-29 16:12:23 +00:00
<:col let={song} label="Title"><%= song.title %></:col>
<:col let={song} label="Artist"><%= song.artist %></:col>
<:col let={song} label="Duration"><%= song.duration %></:col>
<:col let={song} label="">
<.link redirect_to={Routes.song_show_path(@socket, :show, song)}>Show</.link>
<.link patch_to={Routes.song_index_path(@socket, :edit, song)}>Edit</.link>
2021-11-02 16:24:53 +00:00
<.link phx-click={
show_modal(
"delete-modal",
content: "Are you sure you want to delete \"#{song.title}\"?",
on_confirm: JS.push("delete", value: %{id: song.id}) |> hide("#song-#{song.id}")
)
}>Delete</.link>
2021-10-29 16:12:23 +00:00
</:col>
</.table>
"""
end
def mount(_params, _session, socket) do
{:ok, assign(socket, :songs, list_songs())}
end
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Song")
|> assign(:song, MediaLibrary.get_song!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Song")
|> assign(:song, %Song{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Songs")
|> assign(:song, nil)
end
def handle_event("delete", %{"id" => id}, socket) do
song = MediaLibrary.get_song!(id)
{:ok, _} = MediaLibrary.delete_song(song)
{:noreply, assign(socket, :songs, list_songs())}
end
defp list_songs do
MediaLibrary.list_songs()
end
end