diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex
index 982d794..e316257 100644
--- a/lib/live_beats/media_library.ex
+++ b/lib/live_beats/media_library.ex
@@ -184,14 +184,16 @@ defmodule LiveBeats.MediaLibrary do
case LiveBeats.Repo.transaction(multi) do
{:ok, results} ->
- {:ok,
- results
- |> Enum.filter(&match?({{:song, _ref}, _}, &1))
- |> Enum.map(fn {{:song, ref}, song} ->
- consume_file.(ref, fn tmp_path -> store_mp3(song, tmp_path) end)
- {ref, song}
- end)
- |> Enum.into(%{})}
+ songs =
+ results
+ |> Enum.filter(&match?({{:song, _ref}, _}, &1))
+ |> Enum.map(fn {{:song, ref}, song} ->
+ consume_file.(ref, fn tmp_path -> store_mp3(song, tmp_path) end)
+ {ref, song}
+ end)
+ broadcast_imported(user, songs)
+
+ {:ok, Enum.into(songs, %{})}
{:error, failed_op, failed_val, _changes} ->
failed_op =
@@ -205,6 +207,11 @@ defmodule LiveBeats.MediaLibrary do
end
end
+ defp broadcast_imported(%Accounts.User{} = user, songs) do
+ songs = Enum.map(songs, fn {_ref, song} -> song end)
+ broadcast!(user.id, %Events.SongsImported{user_id: user.id, songs: songs})
+ end
+
def parse_file_name(name) do
case Regex.split(~r/[-–]/, Path.rootname(name), parts: 2) do
[title] -> %{title: String.trim(title), artist: nil}
diff --git a/lib/live_beats/media_library/events.ex b/lib/live_beats/media_library/events.ex
index 2831ed2..36739ff 100644
--- a/lib/live_beats/media_library/events.ex
+++ b/lib/live_beats/media_library/events.ex
@@ -10,4 +10,8 @@ defmodule LiveBeats.MediaLibrary.Events do
defmodule PublicProfileUpdated do
defstruct profile: nil
end
+
+ defmodule SongsImported do
+ defstruct user_id: nil, songs: []
+ end
end
diff --git a/lib/live_beats_web/live/live_helpers.ex b/lib/live_beats_web/live/live_helpers.ex
index ff533a4..d15cb6a 100644
--- a/lib/live_beats_web/live/live_helpers.ex
+++ b/lib/live_beats_web/live/live_helpers.ex
@@ -557,7 +557,7 @@ defmodule LiveBeatsWeb.LiveHelpers do
<% end %>
-
+
<%= for {row, i} <- Enum.with_index(@rows) do %>
<.live_component
module={@module}
diff --git a/lib/live_beats_web/live/profile_live.ex b/lib/live_beats_web/live/profile_live.ex
index e707732..2b3fe8e 100644
--- a/lib/live_beats_web/live/profile_live.ex
+++ b/lib/live_beats_web/live/profile_live.ex
@@ -43,18 +43,21 @@ defmodule LiveBeatsWeb.ProfileLive do
<:title let={user}><%= user.username %>
- <%= for song <- if(@owns_profile?, do: @songs, else: []), id = "delete-modal-#{song.id}" do %>
- <.modal
- id={id}
- on_confirm={JS.push("delete", value: %{id: song.id}) |> hide_modal(id) |> hide("#song-#{song.id}")}
- >
- Are you sure you want to delete "<%= song.title %>"?
- <:cancel>Cancel
- <:confirm>Delete
-
- <% end %>
+
+ <%= for song <- if(@owns_profile?, do: @songs, else: []), id = "delete-modal-#{song.id}" do %>
+ <.modal
+ id={id}
+ on_confirm={JS.push("delete", value: %{id: song.id}) |> hide_modal(id) |> hide("#song-#{song.id}")}
+ >
+ Are you sure you want to delete "<%= song.title %>"?
+ <:cancel>Cancel
+ <:confirm>Delete
+
+ <% end %>
+
<.live_table
+ id="songs"
module={SongRowComponent}
rows={@songs}
row_id={fn song -> "song-#{song.id}" end}
@@ -88,7 +91,7 @@ defmodule LiveBeatsWeb.ProfileLive do
active_song_id =
if song = MediaLibrary.get_current_active_song(profile) do
- SongRowComponent.send_status(song.id, song.status)
+ SongRowComponent.send_status(song, song.status)
song.id
end
@@ -158,12 +161,17 @@ defmodule LiveBeatsWeb.ProfileLive do
{:noreply, pause_song(socket, song.id)}
end
+ def handle_info({MediaLibrary, %MediaLibrary.Events.SongsImported{songs: songs}}, socket) do
+ {:noreply, update(socket, :songs, &(&1 ++ songs))}
+ end
+
def handle_info({MediaLibrary, _}, socket), do: {:noreply, socket}
def handle_info({Accounts, _}, socket), do: {:noreply, socket}
defp stop_song(socket, song_id) do
- SongRowComponent.send_status(song_id, :stopped)
+ song = MediaLibrary.get_song!(song_id)
+ SongRowComponent.send_status(song, :stopped)
if socket.assigns.active_song_id == song_id do
assign(socket, :active_song_id, nil)
@@ -173,7 +181,8 @@ defmodule LiveBeatsWeb.ProfileLive do
end
defp pause_song(socket, song_id) do
- SongRowComponent.send_status(song_id, :paused)
+ song = MediaLibrary.get_song!(song_id)
+ SongRowComponent.send_status(song, :paused)
socket
end
@@ -182,18 +191,18 @@ defmodule LiveBeatsWeb.ProfileLive do
cond do
active_song_id == song.id ->
- SongRowComponent.send_status(song.id, :playing)
+ SongRowComponent.send_status(song, :playing)
socket
active_song_id ->
- SongRowComponent.send_status(song.id, :playing)
+ SongRowComponent.send_status(song, :playing)
socket
|> stop_song(active_song_id)
|> assign(active_song_id: song.id)
true ->
- SongRowComponent.send_status(song.id, :playing)
+ SongRowComponent.send_status(song, :playing)
assign(socket, active_song_id: song.id)
end
end
diff --git a/lib/live_beats_web/live/profile_live/song_row_component.ex b/lib/live_beats_web/live/profile_live/song_row_component.ex
index 0ddcb69..e66278c 100644
--- a/lib/live_beats_web/live/profile_live/song_row_component.ex
+++ b/lib/live_beats_web/live/profile_live/song_row_component.ex
@@ -1,8 +1,10 @@
defmodule LiveBeatsWeb.ProfileLive.SongRowComponent do
use LiveBeatsWeb, :live_component
- def send_status(id, status) when status in [:playing, :paused, :stopped] do
- send_update(__MODULE__, id: "song-#{id}", action: :send, status: status)
+ alias LiveBeats.MediaLibrary.Song
+
+ def send_status(%Song{} = song, status) when status in [:playing, :paused, :stopped] do
+ send_update(__MODULE__, id: "song-#{song.id}", action: :send, song: song, status: status)
end
def render(assigns) do
@@ -42,8 +44,12 @@ defmodule LiveBeatsWeb.ProfileLive.SongRowComponent do
"""
end
- def update(%{action: :send, status: status}, socket) when status in [:playing, :paused, :stopped] do
- {:ok, assign(socket, status: status)}
+ def mount(socket) do
+ {:ok, socket, temporary_assigns: [song: nil]}
+ end
+
+ def update(%{action: :send, status: status, song: song}, socket) when status in [:playing, :paused, :stopped] do
+ {:ok, assign(socket, status: status, song: song)}
end
def update(assigns, socket) do
diff --git a/lib/live_beats_web/live/profile_live/upload_form_component.ex b/lib/live_beats_web/live/profile_live/upload_form_component.ex
index de9dee4..61c2ad2 100644
--- a/lib/live_beats_web/live/profile_live/upload_form_component.ex
+++ b/lib/live_beats_web/live/profile_live/upload_form_component.ex
@@ -54,7 +54,7 @@ defmodule LiveBeatsWeb.ProfileLive.UploadFormComponent do
{:noreply,
socket
|> put_flash(:info, "#{map_size(songs)} song(s) uploaded")
- |> push_redirect(to: profile_path(current_user))}
+ |> push_patch(to: profile_path(current_user))}
{:error, {failed_op, reason}} ->
{:noreply, put_error(socket, {failed_op, reason})}