defmodule WishWeb.HomeLive.Details do
  use WishWeb, :live_view

  alias Wish.Wishlist

  @impl true
  def mount(_params, _session, socket) do
    {:ok, socket}
  end

  @impl true
  def handle_params(%{"id" => id}, _, socket) do
    {:noreply,
     socket
     |> assign(:item, Wishlist.get_item!(id))}
  end

  @impl true
  def handle_event("toggle_received", %{"id" => id}, socket) do
    item = Wishlist.get_item!(id)

    case Wishlist.toggle_received(item) do
      {:ok, updated_item} ->
        {:noreply, assign(socket, :item, updated_item)}

      {:error, _} ->
        {:noreply, socket}
    end
  end
end
