From 60b098785b76fc4b4b32e79448d3c093e8a20136 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Fri, 15 Nov 2024 10:01:53 +0000 Subject: [PATCH] Can refresh musicbrainz_data for collection records --- lib/music_library/records.ex | 8 ++++ .../live/record_live/show.ex | 20 +++++++++ .../live/record_live/show.html.heex | 5 +++ priv/gettext/default.pot | 41 +++++++++++++------ 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index 41aed6bc..39eea61e 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -135,6 +135,14 @@ defmodule MusicLibrary.Records do end end + def refresh_musicbrainz_data(record) do + with {:ok, data} <- musicbrainz().get_release_group(record.musicbrainz_id) do + record + |> Record.add_musicbrainz_data(data) + |> Repo.update() + end + end + defp build_record_attrs(release_group, attrs) do release_group |> Record.attrs_from_release_group() diff --git a/lib/music_library_web/live/record_live/show.ex b/lib/music_library_web/live/record_live/show.ex index 31977938..93d0fa94 100644 --- a/lib/music_library_web/live/record_live/show.ex +++ b/lib/music_library_web/live/record_live/show.ex @@ -43,6 +43,26 @@ defmodule MusicLibraryWeb.RecordLive.Show do {:noreply, push_navigate(socket, to: ~p"/records")} end + def handle_event("refresh_musicbrainz_data", %{"id" => id}, socket) do + record = Records.get_record!(id) + + case Records.refresh_musicbrainz_data(record) do + {:ok, updated_record} -> + {:noreply, + socket + |> put_flash(:info, gettext("MusicBrainz data 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 653cfd1e..853ed71d 100644 --- a/lib/music_library_web/live/record_live/show.html.heex +++ b/lib/music_library_web/live/record_live/show.html.heex @@ -4,6 +4,11 @@ <%= gettext("Edit") %> + <.link phx-click={JS.push("refresh_musicbrainz_data", value: %{id: @record.id})}> + <.button> + <%= gettext("Refresh MB Data") %> + + <.link phx-click={JS.push("delete", value: %{id: @record.id})} data-confirm={gettext("Are you sure?")} diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 168aca1c..ea3b8494 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:9 +#: lib/music_library_web/live/record_live/show.html.heex:14 #: lib/music_library_web/live/wishlist_live/index.html.heex:195 #: lib/music_library_web/live/wishlist_live/show.html.heex:9 #, elixir-autogen, elixir-format @@ -29,7 +29,7 @@ msgstr "" msgid "Attempting to reconnect" msgstr "" -#: lib/music_library_web/live/record_live/show.html.heex:127 +#: lib/music_library_web/live/record_live/show.html.heex:132 #, 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:12 +#: lib/music_library_web/live/record_live/show.html.heex:17 #: lib/music_library_web/live/wishlist_live/index.html.heex:197 #: lib/music_library_web/live/wishlist_live/show.html.heex:12 #, 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:52 +#: lib/music_library_web/live/record_live/show.ex:72 #: lib/music_library_web/live/record_live/show.html.heex:4 #: 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:49 +#: lib/music_library_web/live/record_live/show.html.heex:54 #: lib/music_library_web/live/wishlist_live/show.html.heex:50 #, 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:102 +#: lib/music_library_web/live/record_live/show.html.heex:107 #: lib/music_library_web/live/wishlist_live/show.html.heex:95 #, elixir-autogen, elixir-format msgid "Inserted at" @@ -154,7 +154,7 @@ msgstr "" msgid "Logout" 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 "MusicBrainz ID" @@ -204,7 +204,7 @@ msgstr "" msgid "Purchased at" msgstr "" -#: lib/music_library_web/live/record_live/show.html.heex:79 +#: lib/music_library_web/live/record_live/show.html.heex:84 #, elixir-autogen, elixir-format msgid "Purchased on" msgstr "" @@ -254,7 +254,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:51 +#: lib/music_library_web/live/record_live/show.ex:71 #: lib/music_library_web/live/wishlist_live/index.html.heex:156 #: lib/music_library_web/live/wishlist_live/show.ex:49 #, elixir-autogen, elixir-format @@ -311,7 +311,7 @@ msgstr "" msgid "Types" 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 "Updated at" @@ -350,13 +350,13 @@ msgstr "" msgid "close" msgstr "" -#: lib/music_library_web/live/record_live/show.html.heex:67 +#: lib/music_library_web/live/record_live/show.html.heex:72 #: lib/music_library_web/live/wishlist_live/show.html.heex:68 #, elixir-autogen, elixir-format msgid "Copy MusicBrainz ID to clipboard" msgstr "" -#: lib/music_library_web/live/record_live/show.html.heex:121 +#: lib/music_library_web/live/record_live/show.html.heex:126 #: lib/music_library_web/live/wishlist_live/show.html.heex:114 #, elixir-autogen, elixir-format msgid "MusicBrainz data" @@ -390,8 +390,23 @@ msgstr "" msgid "Number of included records" msgstr "" -#: lib/music_library_web/live/record_live/show.html.heex:90 +#: lib/music_library_web/live/record_live/show.html.heex:95 #: lib/music_library_web/live/wishlist_live/show.html.heex:83 #, elixir-autogen, elixir-format msgid "Includes" msgstr "" + +#: lib/music_library_web/live/record_live/show.ex:61 +#, elixir-autogen, elixir-format +msgid "Error refreshing MusicBrainz data" +msgstr "" + +#: lib/music_library_web/live/record_live/show.ex:53 +#, elixir-autogen, elixir-format +msgid "MusicBrainz data refreshed successfully" +msgstr "" + +#: lib/music_library_web/live/record_live/show.html.heex:9 +#, elixir-autogen, elixir-format +msgid "Refresh MB Data" +msgstr ""