From bea2016bfd9b4297dbd3983e4ed7861f83a3dccc Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Mon, 5 May 2025 11:38:05 +0100 Subject: [PATCH] Load release tracks async --- .../live/collection_live/show.ex | 19 +++++++++++++------ .../live/collection_live/show.html.heex | 3 ++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/music_library_web/live/collection_live/show.ex b/lib/music_library_web/live/collection_live/show.ex index 59a1db54..f5695ee9 100644 --- a/lib/music_library_web/live/collection_live/show.ex +++ b/lib/music_library_web/live/collection_live/show.ex @@ -27,7 +27,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do Records.subscribe(record_id) end - {:ok, socket} + {:ok, assign(socket, :release_with_tracks, nil)} end @impl true @@ -37,11 +37,6 @@ defmodule MusicLibraryWeb.CollectionLive.Show do socket = if record.selected_release_id do socket - |> assign_async(:release_with_tracks, fn -> - with {:ok, release} <- MusicBrainz.get_release(record.selected_release_id) do - {:ok, %{release_with_tracks: MusicBrainz.Release.from_api_response(release)}} - end - end) else socket end @@ -121,6 +116,18 @@ defmodule MusicLibraryWeb.CollectionLive.Show do end end + def handle_event("load_release_with_tracks", _params, socket) do + selected_release_id = socket.assigns.record.selected_release_id + + {:noreply, + socket + |> assign_async(:release_with_tracks, fn -> + with {:ok, release} <- MusicBrainz.get_release(selected_release_id) do + {:ok, %{release_with_tracks: MusicBrainz.Release.from_api_response(release)}} + end + end)} + end + @impl true def handle_info({MusicLibraryWeb.FormComponent, {:saved, record}}, socket) do {:noreply, assign(socket, :record, record)} diff --git a/lib/music_library_web/live/collection_live/show.html.heex b/lib/music_library_web/live/collection_live/show.html.heex index 0e897525..ab83d2b0 100644 --- a/lib/music_library_web/live/collection_live/show.html.heex +++ b/lib/music_library_web/live/collection_live/show.html.heex @@ -278,10 +278,11 @@ :if={@record.selected_release_id} id="tracks-sheet" placement="right" + on_open={JS.push("load_release_with_tracks")} >

{gettext("Tracks")}

-
+
<.async_result :let={release_with_tracks} assign={@release_with_tracks}> <:loading> {gettext("Loading release with tracks")}