Use LiveToast instead of built-in flash helpers

So that they can stack and get dismissed automatically
This commit is contained in:
Claudio Ortolina
2025-07-16 15:26:30 +01:00
parent 41f040363e
commit acd34424f7
18 changed files with 64 additions and 61 deletions
+2
View File
@@ -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
+3 -2
View File
@@ -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
@@ -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)
@@ -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} ->
@@ -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)
)}
@@ -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
@@ -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
+1 -1
View File
@@ -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
@@ -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} ->
+10 -10
View File
@@ -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
@@ -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
@@ -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
@@ -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} ->
@@ -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} ->
@@ -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]
@@ -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
@@ -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} ->
@@ -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