diff --git a/lib/music_library_web/live/collection_live/show.ex b/lib/music_library_web/live/collection_live/show.ex index c243a978..faa0ace6 100644 --- a/lib/music_library_web/live/collection_live/show.ex +++ b/lib/music_library_web/live/collection_live/show.ex @@ -11,7 +11,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do format_duration: 1 ] - alias MusicLibrary.Records + alias MusicLibrary.{Records, ScrobbleActivity} alias Phoenix.LiveView.JS @impl true @@ -128,6 +128,29 @@ defmodule MusicLibraryWeb.CollectionLive.Show do end)} end + def handle_event("scrobble_release", _params, socket) do + release_with_tracks_async_result = + socket.assigns.release_with_tracks + + if release_with_tracks = + release_with_tracks_async_result && release_with_tracks_async_result.result do + case ScrobbleActivity.scrobble(release_with_tracks, DateTime.utc_now()) do + {:ok, _} -> + {:noreply, + socket + |> put_flash(:info, gettext("Release scrobbled successfully"))} + + {:error, reason} -> + {:noreply, + socket + |> put_flash( + :error, + gettext("Error scrobbling release") <> "," <> inspect(reason) + )} + 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 9619f966..1c61a0a9 100644 --- a/lib/music_library_web/live/collection_live/show.html.heex +++ b/lib/music_library_web/live/collection_live/show.html.heex @@ -280,9 +280,11 @@ placement="right" on_open={JS.push("load_release_with_tracks")} > -

{gettext("Tracks")}

+

{gettext("Tracks")}

-
+ <.button phx-click="scrobble_release">{gettext("Scrobble to Last.fm")} + +
<.async_result :let={release_with_tracks} assign={@release_with_tracks}> <:loading> {gettext("Loading release with tracks")} diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 7900d46e..67b40f43 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -784,3 +784,18 @@ msgstr "" #, elixir-autogen, elixir-format msgid "Successfully connected your Last.fm account" msgstr "" + +#: lib/music_library_web/live/collection_live/show.ex +#, elixir-autogen, elixir-format +msgid "Error scrobbling release" +msgstr "" + +#: lib/music_library_web/live/collection_live/show.ex +#, elixir-autogen, elixir-format +msgid "Release scrobbled successfully" +msgstr "" + +#: lib/music_library_web/live/collection_live/show.html.heex +#, elixir-autogen, elixir-format +msgid "Scrobble to Last.fm" +msgstr ""