Load release when opening collection show page

Includes small fixes to make both logic and tests more robust
This commit is contained in:
Claudio Ortolina
2025-05-11 11:04:28 +01:00
parent 279caf26fb
commit f21a289008
3 changed files with 24 additions and 28 deletions
+2 -2
View File
@@ -37,10 +37,10 @@ defmodule MusicBrainz.Release do
%__MODULE__{
id: r["id"],
title: r["title"],
artists: parse_artists(r["artist-credit"]),
artists: parse_artists(r["artist-credit"] || []),
date: r["date"],
barcode: r["barcode"],
media: parse_media(r["media"])
media: parse_media(r["media"] || [])
}
end
@@ -15,8 +15,19 @@ defmodule MusicLibraryWeb.ReleaseComponent do
def mount(socket) do
{:ok,
socket
|> assign(:can_scrobble?, ScrobbleActivity.can_scrobble?())
|> assign(:release_with_tracks, nil)}
|> assign(:can_scrobble?, ScrobbleActivity.can_scrobble?())}
end
@impl true
def update(%{record: record} = assigns, socket) do
{:ok,
socket
|> assign(assigns)
|> 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)}
end
@impl true
@@ -27,7 +38,6 @@ defmodule MusicLibraryWeb.ReleaseComponent do
:if={@record.selected_release_id}
id={@sheet_id}
placement="right"
on_open={JS.push("load_release_with_tracks", target: @myself)}
>
<div class="mt-6 flex justify-between items-center gap-4">
<h3 class="text-lg font-semibold text-zinc-700 dark:text-zinc-300">{gettext("Tracks")}</h3>
@@ -126,18 +136,6 @@ defmodule MusicLibraryWeb.ReleaseComponent do
end
@impl true
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
def handle_event("scrobble_release", _params, socket) do
release_with_tracks_async_result =
socket.assigns.release_with_tracks
@@ -13,12 +13,15 @@ defmodule MusicLibraryWeb.CollectionLive.ShowTest do
test "can navigate to the record edit form", %{conn: conn} do
record = record()
release_response = Fixtures.Release.release(:marbles)
Req.Test.stub(MusicBrainz.API, fn conn ->
Req.Test.json(conn, Fixtures.Release.release(:marbles))
Req.Test.json(conn, release_response)
end)
conn
|> visit(~p"/collection/#{record.id}")
|> unwrap(&render_async/1)
|> assert_has("a", text: "Edit")
|> click_link("Edit")
|> assert_path(~p"/collection/#{record}/show/edit")
@@ -30,13 +33,16 @@ defmodule MusicLibraryWeb.CollectionLive.ShowTest do
record = record()
cover_url = ~p"/covers/#{record.id}?vsn=#{record.cover_hash}"
release_response = Fixtures.Release.release(:marbles)
Req.Test.stub(MusicBrainz.API, fn conn ->
Req.Test.json(conn, Fixtures.Release.release(:marbles))
Req.Test.json(conn, release_response)
end)
session =
conn
|> visit(~p"/collection/#{record.id}")
|> unwrap(&render_async/1)
|> assert_has("h2", text: escape(record.title))
|> assert_has("p", text: record.release_date)
|> assert_has("p", text: format_label(record.format))
@@ -60,7 +66,6 @@ defmodule MusicLibraryWeb.CollectionLive.ShowTest do
end
describe "Side panel" do
@tag :skip
test "shows a record's tracks", %{conn: conn} do
record = record()
@@ -74,14 +79,7 @@ defmodule MusicLibraryWeb.CollectionLive.ShowTest do
conn
|> visit(~p"/collection/#{record.id}")
|> assert_has("button", text: "Show Tracks")
|> unwrap(fn view ->
# we can't directly click the "Show Tracks" button as
# its phx-click event uses a JS command. Bit of a hack, but we can simulate
# the click by pretending we're dealing with a JS hook, and trigger the event
# that is sent by the JS command.
view
|> Phoenix.LiveViewTest.render_hook(:load_release_with_tracks, %{})
end)
|> unwrap(&render_async/1)
|> assert_has("a", text: "Connect your Last.fm account")
release =