From 09f356313499ce65a5d1ef212129b27942041326 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Tue, 19 Nov 2024 11:25:11 +0000 Subject: [PATCH] Can refresh cover for a record in the collection --- lib/music_library/records.ex | 2 +- .../live/record_live/show.ex | 20 ++++++++++ .../live/record_live/show.html.heex | 5 +++ priv/gettext/default.pot | 39 ++++++++++++------- test/music_library/records_test.exs | 2 +- 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index 78a01612..1238b477 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -131,7 +131,7 @@ defmodule MusicLibrary.Records do record |> Record.add_cover_data(thumb_data) - |> Repo.update!() + |> Repo.update() end end diff --git a/lib/music_library_web/live/record_live/show.ex b/lib/music_library_web/live/record_live/show.ex index 93d0fa94..0748a1d1 100644 --- a/lib/music_library_web/live/record_live/show.ex +++ b/lib/music_library_web/live/record_live/show.ex @@ -63,6 +63,26 @@ defmodule MusicLibraryWeb.RecordLive.Show do end end + def handle_event("refresh_cover", %{"id" => id}, socket) do + record = Records.get_record!(id) + + case Records.refresh_cover(record) do + {:ok, updated_record} -> + {:noreply, + socket + |> put_flash(:info, gettext("Cover refreshed successfully")) + |> assign(:record, updated_record)} + + {:error, reason} -> + {:noreply, + socket + |> put_flash( + :error, + gettext("Error refreshing MusicBrainz data") <> "," <> inspect(reason) + )} + end + end + @impl true def handle_info({MusicLibraryWeb.RecordLive.FormComponent, {:saved, record}}, socket) do {:noreply, assign(socket, :record, record)} diff --git a/lib/music_library_web/live/record_live/show.html.heex b/lib/music_library_web/live/record_live/show.html.heex index c1d96356..35baa281 100644 --- a/lib/music_library_web/live/record_live/show.html.heex +++ b/lib/music_library_web/live/record_live/show.html.heex @@ -32,6 +32,11 @@ <%= gettext("Edit") %> + <.link phx-click={JS.push("refresh_cover", value: %{id: @record.id})}> + <.button type="button" class="relative -ml-px inline-flex items-center rounded-none"> + <%= gettext("Refresh Cover") %> + + <.link phx-click={JS.push("refresh_musicbrainz_data", value: %{id: @record.id})}> <.button type="button" class="relative -ml-px inline-flex items-center rounded-none"> <%= gettext("Refresh MB Data") %> diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 9270bd38..282bcafb 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -17,7 +17,7 @@ msgid "Actions" msgstr "" #: lib/music_library_web/live/record_live/index.html.heex:185 -#: lib/music_library_web/live/record_live/show.html.heex:42 +#: lib/music_library_web/live/record_live/show.html.heex:47 #: lib/music_library_web/live/wishlist_live/index.html.heex:195 #: lib/music_library_web/live/wishlist_live/show.html.heex:42 #, elixir-autogen, elixir-format @@ -29,7 +29,7 @@ msgstr "" msgid "Attempting to reconnect" msgstr "" -#: lib/music_library_web/live/record_live/show.html.heex:135 +#: lib/music_library_web/live/record_live/show.html.heex:140 #, elixir-autogen, elixir-format msgid "Back to records" msgstr "" @@ -62,7 +62,7 @@ msgid "Cover art" msgstr "" #: lib/music_library_web/live/record_live/index.html.heex:187 -#: lib/music_library_web/live/record_live/show.html.heex:48 +#: lib/music_library_web/live/record_live/show.html.heex:53 #: lib/music_library_web/live/wishlist_live/index.html.heex:197 #: lib/music_library_web/live/wishlist_live/show.html.heex:48 #, elixir-autogen, elixir-format @@ -71,7 +71,7 @@ msgstr "" #: lib/music_library_web/live/record_live/index.ex:55 #: lib/music_library_web/live/record_live/index.html.heex:176 -#: lib/music_library_web/live/record_live/show.ex:72 +#: lib/music_library_web/live/record_live/show.ex:92 #: lib/music_library_web/live/record_live/show.html.heex:32 #: lib/music_library_web/live/wishlist_live/index.ex:55 #: lib/music_library_web/live/wishlist_live/index.html.heex:176 @@ -105,7 +105,7 @@ msgstr "" msgid "Formats" msgstr "" -#: lib/music_library_web/live/record_live/show.html.heex:57 +#: lib/music_library_web/live/record_live/show.html.heex:62 #: lib/music_library_web/live/wishlist_live/show.html.heex:58 #, elixir-autogen, elixir-format msgid "Genres" @@ -128,7 +128,7 @@ msgstr "" msgid "Import from MusicBrainz" msgstr "" -#: lib/music_library_web/live/record_live/show.html.heex:110 +#: lib/music_library_web/live/record_live/show.html.heex:115 #: lib/music_library_web/live/wishlist_live/show.html.heex:103 #, elixir-autogen, elixir-format msgid "Inserted at" @@ -155,7 +155,7 @@ msgid "Logout" msgstr "" #: lib/music_library_web/live/record_live/form_component.ex:46 -#: lib/music_library_web/live/record_live/show.html.heex:65 +#: lib/music_library_web/live/record_live/show.html.heex:70 #: lib/music_library_web/live/wishlist_live/show.html.heex:66 #, elixir-autogen, elixir-format msgid "MusicBrainz ID" @@ -205,7 +205,7 @@ msgstr "" msgid "Purchased at" msgstr "" -#: lib/music_library_web/live/record_live/show.html.heex:87 +#: lib/music_library_web/live/record_live/show.html.heex:92 #, elixir-autogen, elixir-format msgid "Purchased on" msgstr "" @@ -255,7 +255,7 @@ msgid "Search for records" msgstr "" #: lib/music_library_web/live/record_live/index.html.heex:156 -#: lib/music_library_web/live/record_live/show.ex:71 +#: lib/music_library_web/live/record_live/show.ex:91 #: lib/music_library_web/live/wishlist_live/index.html.heex:156 #: lib/music_library_web/live/wishlist_live/show.ex:69 #, elixir-autogen, elixir-format @@ -312,7 +312,7 @@ msgstr "" msgid "Types" msgstr "" -#: lib/music_library_web/live/record_live/show.html.heex:118 +#: lib/music_library_web/live/record_live/show.html.heex:123 #: lib/music_library_web/live/wishlist_live/show.html.heex:111 #, elixir-autogen, elixir-format msgid "Updated at" @@ -351,13 +351,13 @@ msgstr "" msgid "close" msgstr "" -#: lib/music_library_web/live/record_live/show.html.heex:75 +#: lib/music_library_web/live/record_live/show.html.heex:80 #: lib/music_library_web/live/wishlist_live/show.html.heex:76 #, elixir-autogen, elixir-format msgid "Copy MusicBrainz ID to clipboard" msgstr "" -#: lib/music_library_web/live/record_live/show.html.heex:129 +#: lib/music_library_web/live/record_live/show.html.heex:134 #: lib/music_library_web/live/wishlist_live/show.html.heex:122 #, elixir-autogen, elixir-format msgid "MusicBrainz data" @@ -391,13 +391,14 @@ msgstr "" msgid "Number of included records" msgstr "" -#: lib/music_library_web/live/record_live/show.html.heex:98 +#: lib/music_library_web/live/record_live/show.html.heex:103 #: lib/music_library_web/live/wishlist_live/show.html.heex:91 #, elixir-autogen, elixir-format msgid "Includes" msgstr "" #: lib/music_library_web/live/record_live/show.ex:61 +#: lib/music_library_web/live/record_live/show.ex:81 #: lib/music_library_web/live/wishlist_live/show.ex:59 #, elixir-autogen, elixir-format msgid "Error refreshing MusicBrainz data" @@ -409,7 +410,7 @@ msgstr "" msgid "MusicBrainz data refreshed successfully" msgstr "" -#: lib/music_library_web/live/record_live/show.html.heex:37 +#: lib/music_library_web/live/record_live/show.html.heex:42 #: lib/music_library_web/live/wishlist_live/show.html.heex:37 #, elixir-autogen, elixir-format msgid "Refresh MB Data" @@ -419,3 +420,13 @@ msgstr "" #, elixir-autogen, elixir-format msgid "Refresh LastFm Feed" msgstr "" + +#: lib/music_library_web/live/record_live/show.ex:73 +#, elixir-autogen, elixir-format +msgid "Cover refreshed successfully" +msgstr "" + +#: lib/music_library_web/live/record_live/show.html.heex:37 +#, elixir-autogen, elixir-format +msgid "Refresh Cover" +msgstr "" diff --git a/test/music_library/records_test.exs b/test/music_library/records_test.exs index 70245328..f5cac231 100644 --- a/test/music_library/records_test.exs +++ b/test/music_library/records_test.exs @@ -290,7 +290,7 @@ defmodule MusicLibrary.RecordsTest do {:ok, raven_cover_data} end) - updated_record = Records.refresh_cover(record) + assert {:ok, updated_record} = Records.refresh_cover(record) assert updated_record.cover_data !== record.cover_data