Handle toast messages from inside Release component
This commit is contained in:
@@ -71,6 +71,7 @@ defmodule MusicLibraryWeb do
|
|||||||
def live_component do
|
def live_component do
|
||||||
quote do
|
quote do
|
||||||
use Phoenix.LiveComponent
|
use Phoenix.LiveComponent
|
||||||
|
import MusicLibraryWeb.Hooks.ShowToast, only: [put_toast!: 2]
|
||||||
|
|
||||||
unquote(html_helpers())
|
unquote(html_helpers())
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -250,26 +250,25 @@ defmodule MusicLibraryWeb.Components.Release do
|
|||||||
case ScrobbleActivity.scrobble_release(release_with_tracks, finished_at: DateTime.utc_now()) do
|
case ScrobbleActivity.scrobble_release(release_with_tracks, finished_at: DateTime.utc_now()) do
|
||||||
{:ok, _} ->
|
{:ok, _} ->
|
||||||
send_update_after(socket.assigns.myself, %{already_scrobbled: false}, 3000)
|
send_update_after(socket.assigns.myself, %{already_scrobbled: false}, 3000)
|
||||||
|
put_toast!(:info, gettext("Release scrobbled successfully"))
|
||||||
|
|
||||||
{:noreply,
|
{:noreply, socket |> assign(:already_scrobbled, true)}
|
||||||
socket
|
|
||||||
|> assign(:already_scrobbled, true)
|
|
||||||
|> put_toast(:info, gettext("Release scrobbled successfully"))}
|
|
||||||
|
|
||||||
{:error, reason} ->
|
{:error, reason} ->
|
||||||
Logger.error("Error scrobbling release: #{inspect(reason)}")
|
Logger.error("Error scrobbling release: #{inspect(reason)}")
|
||||||
|
|
||||||
{:noreply,
|
put_toast!(
|
||||||
socket
|
:error,
|
||||||
|> put_toast(
|
gettext("Error scrobbling release") <> "," <> inspect(reason)
|
||||||
:error,
|
)
|
||||||
gettext("Error scrobbling release") <> "," <> inspect(reason)
|
|
||||||
)}
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("scrobble_release", _params, socket) do
|
def handle_event("scrobble_release", _params, socket) do
|
||||||
{:noreply, socket |> put_toast(:error, gettext("Error scrobbling release"))}
|
put_toast!(:error, gettext("Error scrobbling release"))
|
||||||
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("scrobble_medium", %{"number" => number}, socket)
|
def handle_event("scrobble_medium", %{"number" => number}, socket)
|
||||||
@@ -282,26 +281,25 @@ defmodule MusicLibraryWeb.Components.Release do
|
|||||||
) do
|
) do
|
||||||
{:ok, _} ->
|
{:ok, _} ->
|
||||||
send_update_after(socket.assigns.myself, %{already_scrobbled: false}, 3000)
|
send_update_after(socket.assigns.myself, %{already_scrobbled: false}, 3000)
|
||||||
|
put_toast!(:info, gettext("Disc scrobbled successfully"))
|
||||||
|
|
||||||
{:noreply,
|
{:noreply, socket |> assign(:already_scrobbled, true)}
|
||||||
socket
|
|
||||||
|> assign(:already_scrobbled, true)
|
|
||||||
|> put_toast(:info, gettext("Disc scrobbled successfully"))}
|
|
||||||
|
|
||||||
{:error, reason} ->
|
{:error, reason} ->
|
||||||
Logger.error("Error scrobbling medium: #{inspect(reason)}")
|
Logger.error("Error scrobbling medium: #{inspect(reason)}")
|
||||||
|
|
||||||
{:noreply,
|
put_toast!(
|
||||||
socket
|
:error,
|
||||||
|> put_toast(
|
gettext("Error scrobbling disc") <> "," <> inspect(reason)
|
||||||
:error,
|
)
|
||||||
gettext("Error scrobbling disc") <> "," <> inspect(reason)
|
|
||||||
)}
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("scrobble_medium", _params, socket) do
|
def handle_event("scrobble_medium", _params, socket) do
|
||||||
{:noreply, socket |> put_toast(:error, gettext("Error scrobbling disc"))}
|
put_toast!(:error, gettext("Error scrobbling disc"))
|
||||||
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("toggle_track", %{"track-id" => track_id}, socket) do
|
def handle_event("toggle_track", %{"track-id" => track_id}, socket) do
|
||||||
@@ -344,7 +342,8 @@ defmodule MusicLibraryWeb.Components.Release do
|
|||||||
selected_track_ids = socket.assigns.selected_tracks
|
selected_track_ids = socket.assigns.selected_tracks
|
||||||
|
|
||||||
if MapSet.size(selected_track_ids) == 0 do
|
if MapSet.size(selected_track_ids) == 0 do
|
||||||
{:noreply, socket |> put_toast(:error, gettext("No tracks selected"))}
|
put_toast!(:error, gettext("No tracks selected"))
|
||||||
|
{:noreply, socket}
|
||||||
else
|
else
|
||||||
case ScrobbleActivity.scrobble_tracks(
|
case ScrobbleActivity.scrobble_tracks(
|
||||||
selected_track_ids,
|
selected_track_ids,
|
||||||
@@ -353,28 +352,29 @@ defmodule MusicLibraryWeb.Components.Release do
|
|||||||
) do
|
) do
|
||||||
{:ok, _} ->
|
{:ok, _} ->
|
||||||
send_update_after(socket.assigns.myself, %{already_scrobbled: false}, 3000)
|
send_update_after(socket.assigns.myself, %{already_scrobbled: false}, 3000)
|
||||||
|
put_toast!(:info, gettext("Selected tracks scrobbled successfully"))
|
||||||
|
|
||||||
{:noreply,
|
{:noreply,
|
||||||
socket
|
socket
|
||||||
|> assign(:already_scrobbled, true)
|
|> assign(:already_scrobbled, true)
|
||||||
|> assign(:selected_tracks, MapSet.new())
|
|> assign(:selected_tracks, MapSet.new())}
|
||||||
|> put_toast(:info, gettext("Selected tracks scrobbled successfully"))}
|
|
||||||
|
|
||||||
{:error, reason} ->
|
{:error, reason} ->
|
||||||
Logger.error("Error scrobbling tracks: #{inspect(reason)}")
|
Logger.error("Error scrobbling tracks: #{inspect(reason)}")
|
||||||
|
|
||||||
{:noreply,
|
put_toast!(
|
||||||
socket
|
:error,
|
||||||
|> put_toast(
|
gettext("Error scrobbling selected tracks") <> "," <> inspect(reason)
|
||||||
:error,
|
)
|
||||||
gettext("Error scrobbling selected tracks") <> "," <> inspect(reason)
|
|
||||||
)}
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("scrobble_selected_tracks", _params, socket) do
|
def handle_event("scrobble_selected_tracks", _params, socket) do
|
||||||
{:noreply, socket |> put_toast(:error, gettext("Error scrobbling selected tracks"))}
|
put_toast!(:error, gettext("Error scrobbling selected tracks"))
|
||||||
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp medium_selected?(medium, selected_tracks) do
|
defp medium_selected?(medium, selected_tracks) do
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
defmodule MusicLibraryWeb.Hooks.ShowToast do
|
||||||
|
import Phoenix.LiveView
|
||||||
|
import LiveToast, only: [put_toast: 3]
|
||||||
|
|
||||||
|
def put_toast!(type, message) do
|
||||||
|
send(self(), {:put_toast, type, message})
|
||||||
|
end
|
||||||
|
|
||||||
|
def on_mount(_name, _params, _session, socket) do
|
||||||
|
{:cont, attach_hook(socket, :put_toast, :handle_info, &maybe_put_toast/2)}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_put_toast({:put_toast, type, message}, socket) do
|
||||||
|
{:halt, put_toast(socket, type, message)}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_put_toast(_, socket), do: {:cont, socket}
|
||||||
|
end
|
||||||
@@ -39,7 +39,11 @@ defmodule MusicLibraryWeb.Router do
|
|||||||
get "/assets/:transform_payload", AssetController, :show
|
get "/assets/:transform_payload", AssetController, :show
|
||||||
|
|
||||||
live_session :default,
|
live_session :default,
|
||||||
on_mount: [MusicLibraryWeb.Hooks.StaticAssets, MusicLibraryWeb.Hooks.GetTimezone] do
|
on_mount: [
|
||||||
|
MusicLibraryWeb.Hooks.StaticAssets,
|
||||||
|
MusicLibraryWeb.Hooks.GetTimezone,
|
||||||
|
MusicLibraryWeb.Hooks.ShowToast
|
||||||
|
] do
|
||||||
live "/", StatsLive.Index, :index
|
live "/", StatsLive.Index, :index
|
||||||
|
|
||||||
live "/collection", CollectionLive.Index, :index
|
live "/collection", CollectionLive.Index, :index
|
||||||
|
|||||||
Reference in New Issue
Block a user