Handle toast messages from inside Release component
This commit is contained in:
@@ -71,6 +71,7 @@ defmodule MusicLibraryWeb do
|
||||
def live_component do
|
||||
quote do
|
||||
use Phoenix.LiveComponent
|
||||
import MusicLibraryWeb.Hooks.ShowToast, only: [put_toast!: 2]
|
||||
|
||||
unquote(html_helpers())
|
||||
end
|
||||
|
||||
@@ -250,26 +250,25 @@ defmodule MusicLibraryWeb.Components.Release do
|
||||
case ScrobbleActivity.scrobble_release(release_with_tracks, finished_at: DateTime.utc_now()) do
|
||||
{:ok, _} ->
|
||||
send_update_after(socket.assigns.myself, %{already_scrobbled: false}, 3000)
|
||||
put_toast!(:info, gettext("Release scrobbled successfully"))
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:already_scrobbled, true)
|
||||
|> put_toast(:info, gettext("Release scrobbled successfully"))}
|
||||
{:noreply, socket |> assign(:already_scrobbled, true)}
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error("Error scrobbling release: #{inspect(reason)}")
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_toast(
|
||||
:error,
|
||||
gettext("Error scrobbling release") <> "," <> inspect(reason)
|
||||
)}
|
||||
put_toast!(
|
||||
:error,
|
||||
gettext("Error scrobbling release") <> "," <> inspect(reason)
|
||||
)
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def handle_event("scrobble_medium", %{"number" => number}, socket)
|
||||
@@ -282,26 +281,25 @@ defmodule MusicLibraryWeb.Components.Release do
|
||||
) do
|
||||
{:ok, _} ->
|
||||
send_update_after(socket.assigns.myself, %{already_scrobbled: false}, 3000)
|
||||
put_toast!(:info, gettext("Disc scrobbled successfully"))
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:already_scrobbled, true)
|
||||
|> put_toast(:info, gettext("Disc scrobbled successfully"))}
|
||||
{:noreply, socket |> assign(:already_scrobbled, true)}
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error("Error scrobbling medium: #{inspect(reason)}")
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_toast(
|
||||
:error,
|
||||
gettext("Error scrobbling disc") <> "," <> inspect(reason)
|
||||
)}
|
||||
put_toast!(
|
||||
:error,
|
||||
gettext("Error scrobbling disc") <> "," <> inspect(reason)
|
||||
)
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
case ScrobbleActivity.scrobble_tracks(
|
||||
selected_track_ids,
|
||||
@@ -353,28 +352,29 @@ defmodule MusicLibraryWeb.Components.Release do
|
||||
) do
|
||||
{:ok, _} ->
|
||||
send_update_after(socket.assigns.myself, %{already_scrobbled: false}, 3000)
|
||||
put_toast!(:info, gettext("Selected tracks scrobbled successfully"))
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:already_scrobbled, true)
|
||||
|> assign(:selected_tracks, MapSet.new())
|
||||
|> put_toast(:info, gettext("Selected tracks scrobbled successfully"))}
|
||||
|> assign(:selected_tracks, MapSet.new())}
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error("Error scrobbling tracks: #{inspect(reason)}")
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_toast(
|
||||
:error,
|
||||
gettext("Error scrobbling selected tracks") <> "," <> inspect(reason)
|
||||
)}
|
||||
put_toast!(
|
||||
:error,
|
||||
gettext("Error scrobbling selected tracks") <> "," <> inspect(reason)
|
||||
)
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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 "/collection", CollectionLive.Index, :index
|
||||
|
||||
Reference in New Issue
Block a user