Use socket assigns instead of threading IDs via JS.push
Extends the same optimization from the previous commit to all remaining show page handlers across collection, wishlist, and artist LiveViews. Also simplifies bare JS.push() calls to plain phx-click strings.
This commit is contained in:
@@ -15,7 +15,7 @@ defmodule MusicLibraryWeb.ScrobbleComponents do
|
||||
<button
|
||||
type="button"
|
||||
class="text-zinc-500 hover:text-zinc-900 dark:text-zinc-400 dark:hover:text-zinc-300"
|
||||
phx-click={JS.push("refresh_lastfm_feed")}
|
||||
phx-click="refresh_lastfm_feed"
|
||||
>
|
||||
<span class="sr-only">{gettext("Refresh LastFm Feed")}</span>
|
||||
<.icon
|
||||
|
||||
@@ -160,9 +160,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
|
||||
</.dropdown_link>
|
||||
<.dropdown_link
|
||||
id={"actions-#{@artist.musicbrainz_id}-refresh-image"}
|
||||
phx-click={
|
||||
JS.push("refresh_artist_image", value: %{id: @artist.musicbrainz_id})
|
||||
}
|
||||
phx-click="refresh_artist_image"
|
||||
>
|
||||
<.icon
|
||||
name="hero-photo"
|
||||
@@ -175,7 +173,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
|
||||
|
||||
<.dropdown_link
|
||||
id={"actions-#{@artist.musicbrainz_id}-refresh-artist-info"}
|
||||
phx-click={JS.push("refresh_artist_info", value: %{id: @artist.musicbrainz_id})}
|
||||
phx-click="refresh_artist_info"
|
||||
>
|
||||
<.icon
|
||||
name="hero-arrow-path"
|
||||
@@ -187,9 +185,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
|
||||
</.dropdown_link>
|
||||
<.dropdown_link
|
||||
id={"actions-#{@artist.musicbrainz_id}-refresh-wikipedia"}
|
||||
phx-click={
|
||||
JS.push("refresh_wikipedia_data", value: %{id: @artist.musicbrainz_id})
|
||||
}
|
||||
phx-click="refresh_wikipedia_data"
|
||||
>
|
||||
<.icon
|
||||
name="hero-arrow-path"
|
||||
@@ -201,7 +197,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
|
||||
</.dropdown_link>
|
||||
<.dropdown_link
|
||||
id={"actions-#{@artist.musicbrainz_id}-refresh-lastfm"}
|
||||
phx-click={JS.push("refresh_lastfm_data", value: %{id: @artist.musicbrainz_id})}
|
||||
phx-click="refresh_lastfm_data"
|
||||
>
|
||||
<.icon
|
||||
name="hero-arrow-path"
|
||||
@@ -531,8 +527,8 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("refresh_artist_info", %{"id" => id}, socket) do
|
||||
case Artists.fetch_artist_info(id) do
|
||||
def handle_event("refresh_artist_info", _params, socket) do
|
||||
case Artists.fetch_artist_info(socket.assigns.artist.musicbrainz_id) do
|
||||
{:ok, artist_info} ->
|
||||
{:noreply,
|
||||
socket
|
||||
@@ -551,8 +547,8 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("refresh_wikipedia_data", %{"id" => id}, socket) do
|
||||
case Artists.refresh_wikipedia_data(id) do
|
||||
def handle_event("refresh_wikipedia_data", _params, socket) do
|
||||
case Artists.refresh_wikipedia_data(socket.assigns.artist.musicbrainz_id) do
|
||||
{:ok, artist_info} ->
|
||||
{:noreply,
|
||||
socket
|
||||
@@ -571,10 +567,12 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("refresh_lastfm_data", %{"id" => id}, socket) do
|
||||
case Artists.fetch_lastfm_data(id) do
|
||||
def handle_event("refresh_lastfm_data", _params, socket) do
|
||||
musicbrainz_id = socket.assigns.artist.musicbrainz_id
|
||||
|
||||
case Artists.fetch_lastfm_data(musicbrainz_id) do
|
||||
{:ok, artist_info} ->
|
||||
id
|
||||
musicbrainz_id
|
||||
|> Records.get_artist_records()
|
||||
|> Enum.each(&Records.generate_embedding_async/1)
|
||||
|
||||
@@ -594,8 +592,8 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("refresh_artist_image", %{"id" => id}, socket) do
|
||||
case Artists.fetch_image(id) do
|
||||
def handle_event("refresh_artist_image", _params, socket) do
|
||||
case Artists.fetch_image(socket.assigns.artist.musicbrainz_id) do
|
||||
{:ok, artist_info} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|
||||
@@ -141,7 +141,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
||||
|
||||
<.dropdown_link
|
||||
id={"actions-#{@record.id}-refresh-cover"}
|
||||
phx-click={JS.push("refresh_cover", value: %{id: @record.id})}
|
||||
phx-click="refresh_cover"
|
||||
>
|
||||
<.icon
|
||||
name="hero-photo"
|
||||
@@ -154,7 +154,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
||||
|
||||
<.dropdown_link
|
||||
id={"actions-#{@record.id}-refresh-mb-data"}
|
||||
phx-click={JS.push("refresh_musicbrainz_data", value: %{id: @record.id})}
|
||||
phx-click="refresh_musicbrainz_data"
|
||||
>
|
||||
<.icon
|
||||
name="hero-arrow-path"
|
||||
@@ -167,7 +167,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
||||
|
||||
<.dropdown_link
|
||||
id={"actions-#{@record.id}-populate-genres"}
|
||||
phx-click={JS.push("populate_genres")}
|
||||
phx-click="populate_genres"
|
||||
>
|
||||
<.icon
|
||||
name="hero-sparkles"
|
||||
@@ -180,7 +180,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
||||
|
||||
<.dropdown_link
|
||||
id={"actions-#{@record.id}-regenerate-embeddings"}
|
||||
phx-click={JS.push("regenerate_embeddings")}
|
||||
phx-click="regenerate_embeddings"
|
||||
>
|
||||
<.icon
|
||||
name="hero-sparkles"
|
||||
@@ -193,7 +193,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
||||
|
||||
<.dropdown_link
|
||||
id={"actions-#{@record.id}-extract-colors"}
|
||||
phx-click={JS.push("extract_colors", value: %{id: @record.id})}
|
||||
phx-click="extract_colors"
|
||||
>
|
||||
<.icon
|
||||
name="hero-paint-brush"
|
||||
@@ -207,7 +207,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
||||
<.dropdown_separator />
|
||||
<.dropdown_link
|
||||
id={"actions-#{@record.id}-delete"}
|
||||
phx-click={JS.push("delete", value: %{id: @record.id})}
|
||||
phx-click="delete"
|
||||
data-confirm={gettext("Are you sure?")}
|
||||
class="text-red-900! hover:bg-red-50! dark:text-red-500! dark:hover:bg-red-900/30! dark:hover:text-red-600!"
|
||||
>
|
||||
@@ -370,15 +370,14 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
record = Records.get_record!(id)
|
||||
{:ok, _} = Records.delete_record(record)
|
||||
def handle_event("delete", _params, socket) do
|
||||
{:ok, _} = Records.delete_record(socket.assigns.record)
|
||||
|
||||
{:noreply, push_navigate(socket, to: ~p"/collection")}
|
||||
end
|
||||
|
||||
def handle_event("refresh_musicbrainz_data", %{"id" => id}, socket) do
|
||||
record = Records.get_record!(id)
|
||||
def handle_event("refresh_musicbrainz_data", _params, socket) do
|
||||
record = socket.assigns.record
|
||||
|
||||
case Records.refresh_musicbrainz_data(record) do
|
||||
{:ok, updated_record} ->
|
||||
@@ -417,8 +416,8 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("refresh_cover", %{"id" => id}, socket) do
|
||||
record = Records.get_record!(id)
|
||||
def handle_event("refresh_cover", _params, socket) do
|
||||
record = socket.assigns.record
|
||||
|
||||
case Records.refresh_cover(record) do
|
||||
{:ok, record} ->
|
||||
@@ -437,8 +436,8 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("extract_colors", %{"id" => id}, socket) do
|
||||
record = Records.get_record!(id)
|
||||
def handle_event("extract_colors", _params, socket) do
|
||||
record = socket.assigns.record
|
||||
|
||||
case Records.extract_colors(record) do
|
||||
{:ok, updated_record} ->
|
||||
|
||||
@@ -95,7 +95,7 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
||||
|
||||
<.dropdown_link
|
||||
id={"actions-#{@record.id}-refresh-cover"}
|
||||
phx-click={JS.push("refresh_cover", value: %{id: @record.id})}
|
||||
phx-click="refresh_cover"
|
||||
>
|
||||
<.icon
|
||||
name="hero-photo"
|
||||
@@ -108,7 +108,7 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
||||
|
||||
<.dropdown_link
|
||||
id={"actions-#{@record.id}-refresh-mb-data"}
|
||||
phx-click={JS.push("refresh_musicbrainz_data", value: %{id: @record.id})}
|
||||
phx-click="refresh_musicbrainz_data"
|
||||
>
|
||||
<.icon
|
||||
name="hero-arrow-path"
|
||||
@@ -121,7 +121,7 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
||||
|
||||
<.dropdown_link
|
||||
id={"actions-#{@record.id}-populate-genres"}
|
||||
phx-click={JS.push("populate_genres")}
|
||||
phx-click="populate_genres"
|
||||
>
|
||||
<.icon
|
||||
name="hero-sparkles"
|
||||
@@ -137,7 +137,7 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
||||
id={"actions-#{@record.id}-purchase"}
|
||||
phx-click={
|
||||
JS.dispatch("music_library:confetti")
|
||||
|> JS.push("add-to-collection", value: %{id: @record.id})
|
||||
|> JS.push("add-to-collection")
|
||||
}
|
||||
>
|
||||
<.icon
|
||||
@@ -151,7 +151,7 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
||||
|
||||
<.dropdown_link
|
||||
id={"actions-#{@record.id}-extract-colors"}
|
||||
phx-click={JS.push("extract_colors", value: %{id: @record.id})}
|
||||
phx-click="extract_colors"
|
||||
>
|
||||
<.icon
|
||||
name="hero-paint-brush"
|
||||
@@ -165,7 +165,7 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
||||
<.dropdown_separator />
|
||||
<.dropdown_link
|
||||
id={"actions-#{@record.id}-delete"}
|
||||
phx-click={JS.push("delete", value: %{id: @record.id})}
|
||||
phx-click="delete"
|
||||
data-confirm={gettext("Are you sure?")}
|
||||
class="text-red-900! hover:bg-red-50! dark:text-red-500! dark:hover:bg-red-900/30! dark:hover:text-red-600!"
|
||||
>
|
||||
@@ -301,15 +301,14 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
record = Records.get_record!(id)
|
||||
{:ok, _} = Records.delete_record(record)
|
||||
def handle_event("delete", _params, socket) do
|
||||
{:ok, _} = Records.delete_record(socket.assigns.record)
|
||||
|
||||
{:noreply, push_navigate(socket, to: ~p"/wishlist")}
|
||||
end
|
||||
|
||||
def handle_event("refresh_musicbrainz_data", %{"id" => id}, socket) do
|
||||
record = Records.get_record!(id)
|
||||
def handle_event("refresh_musicbrainz_data", _params, socket) do
|
||||
record = socket.assigns.record
|
||||
|
||||
case Records.refresh_musicbrainz_data(record) do
|
||||
{:ok, updated_record} ->
|
||||
@@ -329,8 +328,8 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("refresh_cover", %{"id" => id}, socket) do
|
||||
record = Records.get_record!(id)
|
||||
def handle_event("refresh_cover", _params, socket) do
|
||||
record = socket.assigns.record
|
||||
|
||||
case Records.refresh_cover(record) do
|
||||
{:ok, updated_record} ->
|
||||
@@ -368,8 +367,8 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("add-to-collection", %{"id" => id}, socket) do
|
||||
record = Records.get_record!(id)
|
||||
def handle_event("add-to-collection", _params, socket) do
|
||||
record = socket.assigns.record
|
||||
current_time = DateTime.utc_now()
|
||||
|
||||
case Records.update_record(record, %{"purchased_at" => current_time}) do
|
||||
@@ -384,8 +383,8 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("extract_colors", %{"id" => id}, socket) do
|
||||
record = Records.get_record!(id)
|
||||
def handle_event("extract_colors", _params, socket) do
|
||||
record = socket.assigns.record
|
||||
|
||||
case Records.extract_colors(record) do
|
||||
{:ok, updated_record} ->
|
||||
|
||||
Reference in New Issue
Block a user