From 6a0112231ba703bd6a68829bc7d27520200a835b Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Mon, 21 Oct 2024 19:24:05 +0100 Subject: [PATCH] Add purchase function --- .../live/wishlist_live/index.ex | 16 ++++++++++++++++ .../live/wishlist_live/index.html.heex | 10 ++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/music_library_web/live/wishlist_live/index.ex b/lib/music_library_web/live/wishlist_live/index.ex index 37087f51..31c5093f 100644 --- a/lib/music_library_web/live/wishlist_live/index.ex +++ b/lib/music_library_web/live/wishlist_live/index.ex @@ -118,6 +118,22 @@ defmodule MusicLibraryWeb.WishlistLive.Index do end end + def handle_event("purchase", %{"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 + |> put_flash(:info, gettext("Record updated successfully")) + |> push_patch(to: ~p"/wishlist")} + + {:error, %Ecto.Changeset{} = changeset} -> + {:noreply, assign(socket, form: to_form(changeset))} + end + end + defp merge_query(record_list_params, query) do Map.put(record_list_params, :query, query) end diff --git a/lib/music_library_web/live/wishlist_live/index.html.heex b/lib/music_library_web/live/wishlist_live/index.html.heex index 7ca5463d..c8aca7e5 100644 --- a/lib/music_library_web/live/wishlist_live/index.html.heex +++ b/lib/music_library_web/live/wishlist_live/index.html.heex @@ -145,6 +145,16 @@ <%= gettext("Edit") %> + <.link + class="block px-3 py-1 text-sm leading-6 text-gray-900 hover:bg-gray-50" + role="menuitem" + tabindex="-1" + id={"actions-#{record.id}-purchase"} + phx-click={JS.push("purchase", value: %{id: record.id})} + > + <%= gettext("Purchase") %> + + <.link class="block px-3 py-1 text-sm leading-6 text-red-900 hover:bg-red-50" role="menuitem"