Count formats in results

This commit is contained in:
Claudio Ortolina
2026-04-20 13:24:09 +01:00
parent c4ebe636ff
commit eaece07193
4 changed files with 28 additions and 23 deletions
+14 -11
View File
@@ -67,7 +67,7 @@ defmodule MusicLibraryWeb.Components.AddRecord do
id={"musicbrainz_#{release_group.id}"}
myself={@myself}
in_cart?={in_cart?(@cart_pairs, release_group.id)}
cart_format={cart_format(@cart, release_group.id)}
cart_formats={cart_formats(@cart, release_group.id)}
release_group={release_group}
icon_name={@icon_name}
/>
@@ -241,14 +241,17 @@ defmodule MusicLibraryWeb.Components.AddRecord do
attr :release_group, MusicBrainz.ReleaseGroupSearchResult, required: true
attr :myself, :any, required: true
attr :in_cart?, :boolean, required: true
attr :cart_format, :atom, required: true
attr :cart_formats, :list, required: true
defp result(assigns) do
~H"""
<li id={@id} class="flex justify-between gap-x-6 py-5 hover:bg-zinc-100 dark:hover:bg-zinc-700">
<div class="flex w-full shrink-0 items-center justify-between px-4">
<li
id={@id}
class="flex justify-between gap-x-6 py-5 md:px-4 hover:bg-zinc-100 dark:hover:bg-zinc-700"
>
<div class="flex w-full shrink-0 items-center justify-between">
<img
class="mr-4 w-20 flex-none rounded-lg"
class="mr-4 w-12 md:w-20 flex-none rounded-lg"
alt={@release_group.title}
src={ReleaseGroupSearchResult.thumb_url(@release_group)}
onerror={"this.src = '" <> ~p"/images/cover-not-found.png" <> "';"}
@@ -272,7 +275,8 @@ defmodule MusicLibraryWeb.Components.AddRecord do
class="mr-2 inline-flex items-center gap-1 rounded-full bg-emerald-100 dark:bg-emerald-500/10 text-emerald-700 dark:text-emerald-300 text-xs font-medium px-2 py-0.5"
>
<.icon name="hero-check" class="size-3" aria-hidden="true" data-slot="icon" />
{gettext("In cart · %{format}", format: format_label(@cart_format))}
<span :if={length(@cart_formats) > 1}>{length(@cart_formats)}</span>
<span class="sr-only sm:not-sr-only">{gettext("In cart")}</span>
</span>
<.dropdown id={"actions-#{@release_group.id}"} placement="bottom-end">
@@ -615,11 +619,10 @@ defmodule MusicLibraryWeb.Components.AddRecord do
Enum.any?(cart_pairs, fn {id, _format} -> id == rg_id end)
end
defp cart_format(cart, rg_id) do
case Enum.find(cart, &(&1.release_group_id == rg_id)) do
nil -> nil
item -> item.format
end
defp cart_formats(cart, rg_id) do
cart
|> Enum.filter(&(&1.release_group_id == rg_id))
|> Enum.map(& &1.format)
end
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
+5 -5
View File
@@ -2497,11 +2497,6 @@ msgid_plural "Import %{count} records"
msgstr[0] ""
msgstr[1] ""
#: lib/music_library_web/components/add_record.ex
#, elixir-autogen, elixir-format
msgid "In cart · %{format}"
msgstr ""
#: lib/music_library_web/components/add_record.ex
#, elixir-autogen, elixir-format
msgid "Toggle cart"
@@ -2516,3 +2511,8 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "Error queuing records for import"
msgstr ""
#: lib/music_library_web/components/add_record.ex
#, elixir-autogen, elixir-format
msgid "In cart"
msgstr ""
+5 -5
View File
@@ -2497,11 +2497,6 @@ msgid_plural "Import %{count} records"
msgstr[0] ""
msgstr[1] ""
#: lib/music_library_web/components/add_record.ex
#, elixir-autogen, elixir-format
msgid "In cart · %{format}"
msgstr ""
#: lib/music_library_web/components/add_record.ex
#, elixir-autogen, elixir-format
msgid "Toggle cart"
@@ -2516,3 +2511,8 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "Error queuing records for import"
msgstr ""
#: lib/music_library_web/components/add_record.ex
#, elixir-autogen, elixir-format
msgid "In cart"
msgstr ""
@@ -293,8 +293,10 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do
|> visit(~p"/collection/import")
|> fill_in("Search for a record", with: "Marillion Marbles")
|> click_link("#musicbrainz_#{first_id} a", "CD")
|> assert_has("#musicbrainz_#{first_id} span", text: "In cart · CD")
|> assert_has("#musicbrainz_#{first_id} span", text: "In cart")
|> assert_has("#cart-items li", count: 1)
|> click_link("#musicbrainz_#{first_id} a", "Vinyl")
|> assert_has("#musicbrainz_#{first_id} span", text: "2 In cart")
assert MusicLibrary.Repo.all(Record) == []
refute_enqueued(worker: ImportFromMusicbrainzReleaseGroup)
@@ -370,7 +372,7 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do
|> LVT.element("#cart-items form")
|> LVT.render_change(%{"format" => "vinyl"})
assert LVT.render(view) =~ "In cart · Vinyl"
assert LVT.render(view) =~ "In cart"
end
test "rejects change_format when the resulting pair is already in the cart", %{conn: conn} do