defmodule GenericRestServerWeb.ItemLive.Show do use GenericRestServerWeb, :live_view alias GenericRestServer.Items @impl true def render(assigns) do ~H""" <.header> Item {@item.id} <:subtitle>This is a item record from your database. <:actions> <.button navigate={~p"/items"}> <.icon name="hero-arrow-left" /> <.button variant="primary" navigate={~p"/items/#{@item}/edit?return_to=show"}> <.icon name="hero-pencil-square" /> Edit item <.list> <:item title="Name">{@item.name} <:item title="Description">{@item.description} <:item title="Info">{@item.info} <:item title="Amount">{@item.amount} <:item title="Factor">{@item.factor} <:item title="Type">{@item.type} """ end @impl true def mount(%{"id" => id}, _session, socket) do if connected?(socket) do Items.subscribe_items(socket.assigns.current_scope) end {:ok, socket |> assign(:page_title, "Show Item") |> assign(:item, Items.get_item!(socket.assigns.current_scope, id))} end @impl true def handle_info( {:updated, %GenericRestServer.Items.Item{id: id} = item}, %{assigns: %{item: %{id: id}}} = socket ) do {:noreply, assign(socket, :item, item)} end def handle_info( {:deleted, %GenericRestServer.Items.Item{id: id}}, %{assigns: %{item: %{id: id}}} = socket ) do {:noreply, socket |> put_flash(:error, "The current item was deleted.") |> push_navigate(to: ~p"/items")} end def handle_info({type, %GenericRestServer.Items.Item{}}, socket) when type in [:created, :updated, :deleted] do {:noreply, socket} end end