diff --git a/lib/music_library_web/live/artist_live/show.ex b/lib/music_library_web/live/artist_live/show.ex index 69c736c9..f2d2601b 100644 --- a/lib/music_library_web/live/artist_live/show.ex +++ b/lib/music_library_web/live/artist_live/show.ex @@ -164,24 +164,32 @@ defmodule MusicLibraryWeb.ArtistLive.Show do end end + def handle_event("add-to-collection", %{"id" => id}, socket) do + record = Records.get_record!(id) + current_time = DateTime.utc_now() + + case Records.update_record(record, %{"purchased_at" => current_time}) do + {:ok, _} -> + {:noreply, + socket + |> assign_records(socket.assigns.artist.musicbrainz_id) + |> put_flash(:info, gettext("Record added to the collection"))} + + {:error, %Ecto.Changeset{} = changeset} -> + {:noreply, assign(socket, form: to_form(changeset))} + end + end + defp apply_action(socket, :show, %{"musicbrainz_id" => musicbrainz_id}) do artist = Artists.get_artist!(musicbrainz_id) artist_info = Artists.get_artist_info!(musicbrainz_id) - %{collection: collection_records, wishlist: wishlist_records} = - musicbrainz_id - |> Records.get_artist_records() - |> group_and_sort() - socket + |> assign_records(musicbrainz_id) |> assign(:current_section, :artists) |> assign(:artist, artist) |> assign(:artist_info, artist_info) |> assign(:country, ArtistInfo.country(artist_info)) - |> stream(:collection_records, collection_records, reset: true) - |> stream(:wishlist_records, wishlist_records, reset: true) - |> assign(:collection_records_count, Enum.count(collection_records)) - |> assign(:wishlist_records_count, Enum.count(wishlist_records)) |> assign_async(:lastfm_artist_info, fn -> with {:ok, lastfm_artist_info} <- LastFm.get_artist_info(artist.musicbrainz_id, artist.name) do {:ok, %{lastfm_artist_info: lastfm_artist_info}} @@ -210,6 +218,19 @@ defmodule MusicLibraryWeb.ArtistLive.Show do |> assign(:record, nil) end + defp assign_records(socket, artist_musicbrainz_id) do + %{collection: collection_records, wishlist: wishlist_records} = + artist_musicbrainz_id + |> Records.get_artist_records() + |> group_and_sort() + + socket + |> stream(:collection_records, collection_records, reset: true) + |> stream(:wishlist_records, wishlist_records, reset: true) + |> assign(:collection_records_count, Enum.count(collection_records)) + |> assign(:wishlist_records_count, Enum.count(wishlist_records)) + end + defp page_title(:show, artist) do Enum.join( [ diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 554a2a1b..d85c8edc 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -532,6 +532,7 @@ msgstr "" msgid "Purchased" msgstr "" +#: lib/music_library_web/live/artist_live/show.ex #: lib/music_library_web/live/wishlist_live/index.ex #: lib/music_library_web/live/wishlist_live/show.ex #, elixir-autogen, elixir-format diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index 94997891..e77950ef 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -532,6 +532,7 @@ msgstr "" msgid "Purchased" msgstr "" +#: lib/music_library_web/live/artist_live/show.ex #: lib/music_library_web/live/wishlist_live/index.ex #: lib/music_library_web/live/wishlist_live/show.ex #, elixir-autogen, elixir-format