After 'mix phx.gen.live Items Item items name:string description:string info:string amount:integer factor:float type:string'
This commit is contained in:
69
lib/generic_rest_server_web/live/item_live/show.ex
Normal file
69
lib/generic_rest_server_web/live/item_live/show.ex
Normal file
@ -0,0 +1,69 @@
|
||||
defmodule GenericRestServerWeb.ItemLive.Show do
|
||||
use GenericRestServerWeb, :live_view
|
||||
|
||||
alias GenericRestServer.Items
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<Layouts.app flash={@flash} current_scope={@current_scope}>
|
||||
<.header>
|
||||
Item {@item.id}
|
||||
<:subtitle>This is a item record from your database.</:subtitle>
|
||||
<:actions>
|
||||
<.button navigate={~p"/items"}>
|
||||
<.icon name="hero-arrow-left" />
|
||||
</.button>
|
||||
<.button variant="primary" navigate={~p"/items/#{@item}/edit?return_to=show"}>
|
||||
<.icon name="hero-pencil-square" /> Edit item
|
||||
</.button>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.list>
|
||||
<:item title="Name">{@item.name}</:item>
|
||||
<:item title="Description">{@item.description}</:item>
|
||||
<:item title="Info">{@item.info}</:item>
|
||||
<:item title="Amount">{@item.amount}</:item>
|
||||
<:item title="Factor">{@item.factor}</:item>
|
||||
<:item title="Type">{@item.type}</:item>
|
||||
</.list>
|
||||
</Layouts.app>
|
||||
"""
|
||||
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
|
||||
Reference in New Issue
Block a user