From acd34424f7d4ba41fe00cfb18f6d62343f84b98e Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Wed, 16 Jul 2025 15:26:30 +0100 Subject: [PATCH] Use LiveToast instead of built-in flash helpers So that they can stack and get dismissed automatically --- lib/music_library_web.ex | 2 ++ lib/music_library_web/auth.ex | 5 +++-- .../components/barcode_scanner_component.ex | 6 +++--- .../components/form_component.ex | 2 +- .../components/release_component.ex | 8 ++++---- .../controllers/last_fm_controller.ex | 4 ++-- .../controllers/session_controller.ex | 2 +- lib/music_library_web/hooks/static_assets.ex | 2 +- .../live/artist_live/form_component.ex | 2 +- .../live/artist_live/show.ex | 20 +++++++++---------- .../live/collection_live/index.ex | 6 +++--- .../live/collection_live/show.ex | 18 ++++++++--------- .../form_component.ex | 4 ++-- .../scrobble_rules_live/form_component.ex | 4 ++-- .../live/scrobble_rules_live/index.ex | 6 +++--- .../live/stats_live/index.ex | 6 +++--- .../live/wishlist_live/index.ex | 8 ++++---- .../live/wishlist_live/show.ex | 20 +++++++++---------- 18 files changed, 64 insertions(+), 61 deletions(-) diff --git a/lib/music_library_web.ex b/lib/music_library_web.ex index 2f4ec854..a51427f2 100644 --- a/lib/music_library_web.ex +++ b/lib/music_library_web.ex @@ -47,6 +47,7 @@ defmodule MusicLibraryWeb do use Gettext, backend: MusicLibraryWeb.Gettext + import LiveToast, only: [put_toast: 3] import Plug.Conn unquote(verified_routes()) @@ -95,6 +96,7 @@ defmodule MusicLibraryWeb do use Gettext, backend: MusicLibraryWeb.Gettext use Fluxon + import LiveToast, only: [put_toast: 3] # Core UI components and translation import MusicLibraryWeb.CoreComponents # HTML escaping functionality diff --git a/lib/music_library_web/auth.ex b/lib/music_library_web/auth.ex index 71ae0b27..f862d904 100644 --- a/lib/music_library_web/auth.ex +++ b/lib/music_library_web/auth.ex @@ -1,7 +1,8 @@ defmodule MusicLibraryWeb.Auth do use Gettext, backend: MusicLibraryWeb.Gettext - import Phoenix.Controller, only: [put_flash: 3, redirect: 2] + import LiveToast, only: [put_toast: 3] + import Phoenix.Controller, only: [redirect: 2] import Plug.Conn def correct_login_password?(password) do @@ -35,7 +36,7 @@ defmodule MusicLibraryWeb.Auth do conn else conn - |> put_flash(:error, gettext("You must be logged in to access this page")) + |> put_toast(:error, gettext("You must be logged in to access this page")) |> redirect(to: "/login") |> halt() end diff --git a/lib/music_library_web/components/barcode_scanner_component.ex b/lib/music_library_web/components/barcode_scanner_component.ex index 9c22bd2a..196916ea 100644 --- a/lib/music_library_web/components/barcode_scanner_component.ex +++ b/lib/music_library_web/components/barcode_scanner_component.ex @@ -323,7 +323,7 @@ defmodule MusicLibraryWeb.BarcodeScannerComponent do "Failed to search release for barcode #{number}: #{inspect(reason)}" end) - put_flash( + put_toast( socket, :error, gettext("Failed to search release for barcode %{number}", number: number) @@ -339,7 +339,7 @@ defmodule MusicLibraryWeb.BarcodeScannerComponent do socket = case BarcodeScan.import_results(socket.assigns.scan_results, current_time) do [] -> - put_flash(socket, :info, gettext("Records imported successfully")) + put_toast(socket, :info, gettext("Records imported successfully")) errors -> errors_summary = @@ -347,7 +347,7 @@ defmodule MusicLibraryWeb.BarcodeScannerComponent do "#{number}: #{inspect(reason)}" end) - put_flash( + put_toast( socket, :error, gettext("Some records could not be imported: %{summary}", summary: errors_summary) diff --git a/lib/music_library_web/components/form_component.ex b/lib/music_library_web/components/form_component.ex index 815ff2e4..bf3166e4 100644 --- a/lib/music_library_web/components/form_component.ex +++ b/lib/music_library_web/components/form_component.ex @@ -198,7 +198,7 @@ defmodule MusicLibraryWeb.FormComponent do {:noreply, socket - |> put_flash(:info, gettext("Record updated successfully")) + |> put_toast(:info, gettext("Record updated successfully")) |> push_patch(to: socket.assigns.patch)} {:error, %Ecto.Changeset{} = changeset} -> diff --git a/lib/music_library_web/components/release_component.ex b/lib/music_library_web/components/release_component.ex index 2efd9dfc..354e0785 100644 --- a/lib/music_library_web/components/release_component.ex +++ b/lib/music_library_web/components/release_component.ex @@ -147,12 +147,12 @@ defmodule MusicLibraryWeb.ReleaseComponent do {:noreply, socket |> assign(:already_scrobbled, true) - |> put_flash(:info, gettext("Release scrobbled successfully"))} + |> put_toast(:info, gettext("Release scrobbled successfully"))} {:error, reason} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error scrobbling release") <> "," <> inspect(reason) )} @@ -177,12 +177,12 @@ defmodule MusicLibraryWeb.ReleaseComponent do {:noreply, socket |> assign(:already_scrobbled, true) - |> put_flash(:info, gettext("Disc scrobbled successfully"))} + |> put_toast(:info, gettext("Disc scrobbled successfully"))} {:error, reason} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error scrobbling disc") <> "," <> inspect(reason) )} diff --git a/lib/music_library_web/controllers/last_fm_controller.ex b/lib/music_library_web/controllers/last_fm_controller.ex index 511391ea..092b5422 100644 --- a/lib/music_library_web/controllers/last_fm_controller.ex +++ b/lib/music_library_web/controllers/last_fm_controller.ex @@ -7,12 +7,12 @@ defmodule MusicLibraryWeb.LastFmController do with {:ok, session} <- LastFm.get_session(token), {:ok, _secret} <- Secrets.store("last_fm_session_key", session.key) do conn - |> put_flash(:info, gettext("Successfully connected your Last.fm account")) + |> put_toast(:info, gettext("Successfully connected your Last.fm account")) |> redirect(to: ~p"/") else {:error, reason} -> conn - |> put_flash(:error, "Failed to connect your Last.fm account: #{reason}") + |> put_toast(:error, "Failed to connect your Last.fm account: #{reason}") |> redirect(to: ~p"/") end end diff --git a/lib/music_library_web/controllers/session_controller.ex b/lib/music_library_web/controllers/session_controller.ex index a94892da..be65de0d 100644 --- a/lib/music_library_web/controllers/session_controller.ex +++ b/lib/music_library_web/controllers/session_controller.ex @@ -22,7 +22,7 @@ defmodule MusicLibraryWeb.SessionController do |> redirect(to: ~p"/") else conn - |> put_flash(:error, gettext("Invalid password")) + |> put_toast(:error, gettext("Invalid password")) |> redirect(to: ~p"/login") end end diff --git a/lib/music_library_web/hooks/static_assets.ex b/lib/music_library_web/hooks/static_assets.ex index 32ea207c..7adf8d48 100644 --- a/lib/music_library_web/hooks/static_assets.ex +++ b/lib/music_library_web/hooks/static_assets.ex @@ -4,7 +4,7 @@ defmodule MusicLibraryWeb.Hooks.StaticAssets do def on_mount(:default, _params, _session, socket) do socket = if static_changed?(socket) do - put_flash(socket, :warning, gettext("The application has been updated, please reload.")) + put_toast(socket, :warning, gettext("The application has been updated, please reload.")) else socket end diff --git a/lib/music_library_web/live/artist_live/form_component.ex b/lib/music_library_web/live/artist_live/form_component.ex index a4f840de..2582fe01 100644 --- a/lib/music_library_web/live/artist_live/form_component.ex +++ b/lib/music_library_web/live/artist_live/form_component.ex @@ -145,7 +145,7 @@ defmodule MusicLibraryWeb.ArtistLive.FormComponent do {:noreply, socket - |> put_flash(:info, gettext("Artist updated successfully")) + |> put_toast(:info, gettext("Artist updated successfully")) |> push_patch(to: socket.assigns.patch)} {:error, %Ecto.Changeset{} = changeset} -> diff --git a/lib/music_library_web/live/artist_live/show.ex b/lib/music_library_web/live/artist_live/show.ex index 4510746c..eeacafda 100644 --- a/lib/music_library_web/live/artist_live/show.ex +++ b/lib/music_library_web/live/artist_live/show.ex @@ -108,13 +108,13 @@ defmodule MusicLibraryWeb.ArtistLive.Show do {:ok, _record} -> {:noreply, socket - |> put_flash(:info, gettext("Record wishlisted successfully")) + |> put_toast(:info, gettext("Record wishlisted successfully")) |> push_patch(to: ~p"/artists/#{socket.assigns.artist.musicbrainz_id}")} {:error, %Ecto.Changeset{} = changeset} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error wishlisting record") <> "," <> inspect(changeset.errors) ) @@ -123,7 +123,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do {:error, reason} -> {:noreply, socket - |> put_flash(:error, gettext("Error wishlisting record") <> "," <> inspect(reason)) + |> put_toast(:error, gettext("Error wishlisting record") <> "," <> inspect(reason)) |> push_patch(to: ~p"/artists/#{socket.assigns.artist.musicbrainz_id}")} end end @@ -134,12 +134,12 @@ defmodule MusicLibraryWeb.ArtistLive.Show do {:noreply, socket |> assign(:artist_info, artist_info) - |> put_flash(:info, gettext("Artist info refreshed successfully"))} + |> put_toast(:info, gettext("Artist info refreshed successfully"))} {:error, reason} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error refreshing artist info") <> "," <> inspect(reason) )} @@ -152,12 +152,12 @@ defmodule MusicLibraryWeb.ArtistLive.Show do {:noreply, socket |> assign(:artist_info, artist_info) - |> put_flash(:info, gettext("Artist image refreshed successfully"))} + |> put_toast(:info, gettext("Artist image refreshed successfully"))} {:error, reason} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error refreshing artist image") <> "," <> inspect(reason) )} @@ -173,12 +173,12 @@ defmodule MusicLibraryWeb.ArtistLive.Show do {:noreply, socket |> assign_records(socket.assigns.artist.musicbrainz_id) - |> put_flash(:info, gettext("Record added to the collection"))} + |> put_toast(:info, gettext("Record added to the collection"))} {:error, %Ecto.Changeset{} = changeset} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error importing record") <> "," <> inspect(changeset.errors) )} @@ -192,7 +192,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do {:noreply, socket |> assign_records(socket.assigns.artist.musicbrainz_id) - |> put_flash(:info, gettext("Record deleted"))} + |> put_toast(:info, gettext("Record deleted"))} end defp apply_action(socket, :show, %{"musicbrainz_id" => musicbrainz_id}) do diff --git a/lib/music_library_web/live/collection_live/index.ex b/lib/music_library_web/live/collection_live/index.ex index 4dc6bc5b..cfaf45d3 100644 --- a/lib/music_library_web/live/collection_live/index.ex +++ b/lib/music_library_web/live/collection_live/index.ex @@ -104,13 +104,13 @@ defmodule MusicLibraryWeb.CollectionLive.Index do {:ok, record} -> {:noreply, socket - |> put_flash(:info, gettext("Record imported successfully")) + |> put_toast(:info, gettext("Record imported successfully")) |> push_navigate(to: ~p"/collection/#{record.id}")} {:error, %Ecto.Changeset{} = changeset} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error importing record") <> "," <> inspect(changeset.errors) ) @@ -119,7 +119,7 @@ defmodule MusicLibraryWeb.CollectionLive.Index do {:error, reason} -> {:noreply, socket - |> put_flash(:error, gettext("Error importing record") <> "," <> inspect(reason)) + |> put_toast(:error, gettext("Error importing record") <> "," <> inspect(reason)) |> push_patch(to: ~p"/collection")} end end diff --git a/lib/music_library_web/live/collection_live/show.ex b/lib/music_library_web/live/collection_live/show.ex index 0a0f0846..c67c7197 100644 --- a/lib/music_library_web/live/collection_live/show.ex +++ b/lib/music_library_web/live/collection_live/show.ex @@ -63,13 +63,13 @@ defmodule MusicLibraryWeb.CollectionLive.Show do {:ok, updated_record} -> {:noreply, socket - |> put_flash(:info, gettext("MusicBrainz data refreshed successfully")) + |> put_toast(:info, gettext("MusicBrainz data refreshed successfully")) |> assign(:record, updated_record)} {:error, reason} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error refreshing MusicBrainz data") <> "," <> inspect(reason) )} @@ -83,13 +83,13 @@ defmodule MusicLibraryWeb.CollectionLive.Show do {:ok, updated_record} -> {:noreply, socket - |> put_flash(:info, gettext("Genres populated successfully")) + |> put_toast(:info, gettext("Genres populated successfully")) |> assign(:record, updated_record)} {:error, reason} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error populating genres") <> "," <> inspect(reason) )} @@ -104,12 +104,12 @@ defmodule MusicLibraryWeb.CollectionLive.Show do {:noreply, socket |> assign(:record, record) - |> put_flash(:info, gettext("Cover refreshed successfully"))} + |> put_toast(:info, gettext("Cover refreshed successfully"))} {:error, reason} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error refreshing cover") <> "," <> inspect(reason) )} @@ -124,12 +124,12 @@ defmodule MusicLibraryWeb.CollectionLive.Show do {:ok, _worker} -> {:noreply, socket - |> put_flash(:info, gettext("In progress - record will update automatically"))} + |> put_toast(:info, gettext("In progress - record will update automatically"))} {:error, reason} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error") <> "," <> inspect(reason) )} @@ -145,7 +145,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do def handle_info({:update, record}, socket) do {:noreply, socket - |> put_flash(:info, gettext("Record updated in the background")) + |> put_toast(:info, gettext("Record updated in the background")) |> assign(:record, record)} end diff --git a/lib/music_library_web/live/online_store_template_live/form_component.ex b/lib/music_library_web/live/online_store_template_live/form_component.ex index 8e48e984..9fbef3ea 100644 --- a/lib/music_library_web/live/online_store_template_live/form_component.ex +++ b/lib/music_library_web/live/online_store_template_live/form_component.ex @@ -118,7 +118,7 @@ defmodule MusicLibraryWeb.OnlineStoreTemplateLive.FormComponent do {:noreply, socket - |> put_flash(:info, gettext("Online store template updated successfully")) + |> put_toast(:info, gettext("Online store template updated successfully")) |> push_patch(to: socket.assigns.patch)} {:error, %Ecto.Changeset{} = changeset} -> @@ -133,7 +133,7 @@ defmodule MusicLibraryWeb.OnlineStoreTemplateLive.FormComponent do {:noreply, socket - |> put_flash(:info, gettext("Online store template created successfully")) + |> put_toast(:info, gettext("Online store template created successfully")) |> push_patch(to: socket.assigns.patch)} {:error, %Ecto.Changeset{} = changeset} -> diff --git a/lib/music_library_web/live/scrobble_rules_live/form_component.ex b/lib/music_library_web/live/scrobble_rules_live/form_component.ex index ef68edd6..0919d89b 100644 --- a/lib/music_library_web/live/scrobble_rules_live/form_component.ex +++ b/lib/music_library_web/live/scrobble_rules_live/form_component.ex @@ -92,7 +92,7 @@ defmodule MusicLibraryWeb.ScrobbleRulesLive.FormComponent do {:noreply, socket - |> put_flash(:info, gettext("Scrobble rule updated successfully")) + |> put_toast(:info, gettext("Scrobble rule updated successfully")) |> push_patch(to: socket.assigns.patch)} {:error, %Ecto.Changeset{} = changeset} -> @@ -107,7 +107,7 @@ defmodule MusicLibraryWeb.ScrobbleRulesLive.FormComponent do {:noreply, socket - |> put_flash(:info, gettext("Scrobble rule created successfully")) + |> put_toast(:info, gettext("Scrobble rule created successfully")) |> push_patch(to: socket.assigns.patch)} {:error, %Ecto.Changeset{} = changeset} -> diff --git a/lib/music_library_web/live/scrobble_rules_live/index.ex b/lib/music_library_web/live/scrobble_rules_live/index.ex index 37e2daa7..b6b8e720 100644 --- a/lib/music_library_web/live/scrobble_rules_live/index.ex +++ b/lib/music_library_web/live/scrobble_rules_live/index.ex @@ -77,11 +77,11 @@ defmodule MusicLibraryWeb.ScrobbleRulesLive.Index do case ScrobbleRules.apply_rule(scrobble_rule) do {:ok, count} -> message = gettext("Rule applied successfully. Updated %{count} tracks.", count: count) - {:noreply, put_flash(socket, :info, message)} + {:noreply, put_toast(socket, :info, message)} {:error, reason} -> message = gettext("Error applying rule: %{reason}", reason: reason) - {:noreply, put_flash(socket, :error, message)} + {:noreply, put_toast(socket, :error, message)} end end @@ -100,7 +100,7 @@ defmodule MusicLibraryWeb.ScrobbleRulesLive.Index do count: total_updated ) - {:noreply, put_flash(socket, :info, message)} + {:noreply, put_toast(socket, :info, message)} end attr :type, :atom, required: true, values: [:album, :artist] diff --git a/lib/music_library_web/live/stats_live/index.ex b/lib/music_library_web/live/stats_live/index.ex index c994bc2d..d5fa6f46 100644 --- a/lib/music_library_web/live/stats_live/index.ex +++ b/lib/music_library_web/live/stats_live/index.ex @@ -53,13 +53,13 @@ defmodule MusicLibraryWeb.StatsLive.Index do {:ok, record} -> {:noreply, socket - |> put_flash(:info, gettext("Record wishlisted successfully")) + |> put_toast(:info, gettext("Record wishlisted successfully")) |> push_navigate(to: ~p"/wishlist/#{record.id}")} {:error, %Ecto.Changeset{} = changeset} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error wishlisting record") <> "," <> inspect(changeset.errors) )} @@ -67,7 +67,7 @@ defmodule MusicLibraryWeb.StatsLive.Index do {:error, reason} -> {:noreply, socket - |> put_flash(:error, gettext("Error wishlisting record") <> "," <> inspect(reason))} + |> put_toast(:error, gettext("Error wishlisting record") <> "," <> inspect(reason))} end end diff --git a/lib/music_library_web/live/wishlist_live/index.ex b/lib/music_library_web/live/wishlist_live/index.ex index 0f44820f..767c9564 100644 --- a/lib/music_library_web/live/wishlist_live/index.ex +++ b/lib/music_library_web/live/wishlist_live/index.ex @@ -100,13 +100,13 @@ defmodule MusicLibraryWeb.WishlistLive.Index do {:ok, record} -> {:noreply, socket - |> put_flash(:info, gettext("Record wishlisted successfully")) + |> put_toast(:info, gettext("Record wishlisted successfully")) |> push_navigate(to: ~p"/wishlist/#{record.id}")} {:error, %Ecto.Changeset{} = changeset} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error wishlisting record") <> "," <> inspect(changeset.errors) ) @@ -115,7 +115,7 @@ defmodule MusicLibraryWeb.WishlistLive.Index do {:error, reason} -> {:noreply, socket - |> put_flash(:error, gettext("Error wishlisting record") <> "," <> inspect(reason)) + |> put_toast(:error, gettext("Error wishlisting record") <> "," <> inspect(reason)) |> push_patch(to: ~p"/wishlist")} end end @@ -128,7 +128,7 @@ defmodule MusicLibraryWeb.WishlistLive.Index do {:ok, _} -> {:noreply, socket - |> put_flash(:info, gettext("Record added to the collection")) + |> put_toast(:info, gettext("Record added to the collection")) |> push_patch(to: ~p"/wishlist")} {:error, %Ecto.Changeset{} = changeset} -> diff --git a/lib/music_library_web/live/wishlist_live/show.ex b/lib/music_library_web/live/wishlist_live/show.ex index 90702dca..f7b985a0 100644 --- a/lib/music_library_web/live/wishlist_live/show.ex +++ b/lib/music_library_web/live/wishlist_live/show.ex @@ -54,13 +54,13 @@ defmodule MusicLibraryWeb.WishlistLive.Show do {:ok, updated_record} -> {:noreply, socket - |> put_flash(:info, gettext("MusicBrainz data refreshed successfully")) + |> put_toast(:info, gettext("MusicBrainz data refreshed successfully")) |> assign(:record, updated_record)} {:error, reason} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error refreshing MusicBrainz data") <> "," <> inspect(reason) )} @@ -74,13 +74,13 @@ defmodule MusicLibraryWeb.WishlistLive.Show do {:ok, updated_record} -> {:noreply, socket - |> put_flash(:info, gettext("Cover refreshed successfully")) + |> put_toast(:info, gettext("Cover refreshed successfully")) |> assign(:record, updated_record)} {:error, reason} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error refreshing Cover") <> "," <> inspect(reason) )} @@ -94,13 +94,13 @@ defmodule MusicLibraryWeb.WishlistLive.Show do {:ok, updated_record} -> {:noreply, socket - |> put_flash(:info, gettext("Genres populated successfully")) + |> put_toast(:info, gettext("Genres populated successfully")) |> assign(:record, updated_record)} {:error, reason} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error populating genres") <> "," <> inspect(reason) )} @@ -115,7 +115,7 @@ defmodule MusicLibraryWeb.WishlistLive.Show do {:ok, _} -> {:noreply, socket - |> put_flash(:info, gettext("Record added to the collection")) + |> put_toast(:info, gettext("Record added to the collection")) |> push_navigate(to: ~p"/wishlist")} {:error, %Ecto.Changeset{} = changeset} -> @@ -131,12 +131,12 @@ defmodule MusicLibraryWeb.WishlistLive.Show do {:ok, _worker} -> {:noreply, socket - |> put_flash(:info, gettext("In progress - record will update automatically"))} + |> put_toast(:info, gettext("In progress - record will update automatically"))} {:error, reason} -> {:noreply, socket - |> put_flash( + |> put_toast( :error, gettext("Error") <> "," <> inspect(reason) )} @@ -152,7 +152,7 @@ defmodule MusicLibraryWeb.WishlistLive.Show do def handle_info({:update, record}, socket) do {:noreply, socket - |> put_flash(:info, gettext("Record updated in the background")) + |> put_toast(:info, gettext("Record updated in the background")) |> assign(:record, record)} end