Add initial project
This commit is contained in:
94
lib/wish_web/live/item_live/form_component.ex
Normal file
94
lib/wish_web/live/item_live/form_component.ex
Normal file
@@ -0,0 +1,94 @@
|
||||
defmodule WishWeb.ItemLive.FormComponent do
|
||||
use WishWeb, :live_component
|
||||
|
||||
alias Wish.Wishlist
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div>
|
||||
<.header>
|
||||
<%= @title %>
|
||||
<:subtitle>Use this form to manage item records in your database.</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.simple_form
|
||||
for={@form}
|
||||
id="item-form"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
>
|
||||
<.input field={@form[:title]} type="text" label="Title" />
|
||||
<.input field={@form[:description]} type="text" label="Description" />
|
||||
<.input field={@form[:url]} type="text" label="Url" />
|
||||
<.input field={@form[:received]} type="checkbox" label="Received" />
|
||||
<.input field={@form[:desire]} type="number" label="Desire" />
|
||||
<:actions>
|
||||
<.button phx-disable-with="Saving...">Save Item</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def update(%{item: item} = assigns, socket) do
|
||||
changeset = Wishlist.change_item(item)
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign_form(changeset)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"item" => item_params}, socket) do
|
||||
changeset =
|
||||
socket.assigns.item
|
||||
|> Wishlist.change_item(item_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{:noreply, assign_form(socket, changeset)}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"item" => item_params}, socket) do
|
||||
save_item(socket, socket.assigns.action, item_params)
|
||||
end
|
||||
|
||||
defp save_item(socket, :edit, item_params) do
|
||||
case Wishlist.update_item(socket.assigns.item, item_params) do
|
||||
{:ok, item} ->
|
||||
notify_parent({:saved, item})
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Item updated successfully")
|
||||
|> push_patch(to: socket.assigns.patch)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign_form(socket, changeset)}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_item(socket, :new, item_params) do
|
||||
case Wishlist.create_item(item_params) do
|
||||
{:ok, item} ->
|
||||
notify_parent({:saved, item})
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Item created successfully")
|
||||
|> push_patch(to: socket.assigns.patch)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign_form(socket, changeset)}
|
||||
end
|
||||
end
|
||||
|
||||
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
|
||||
assign(socket, :form, to_form(changeset))
|
||||
end
|
||||
|
||||
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
|
||||
end
|
||||
47
lib/wish_web/live/item_live/index.ex
Normal file
47
lib/wish_web/live/item_live/index.ex
Normal file
@@ -0,0 +1,47 @@
|
||||
defmodule WishWeb.ItemLive.Index do
|
||||
use WishWeb, :live_view
|
||||
|
||||
alias Wish.Wishlist
|
||||
alias Wish.Wishlist.Item
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, stream(socket, :items, Wishlist.list_items())}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(params, _url, socket) do
|
||||
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
||||
end
|
||||
|
||||
defp apply_action(socket, :edit, %{"id" => id}) do
|
||||
socket
|
||||
|> assign(:page_title, "Edit Item")
|
||||
|> assign(:item, Wishlist.get_item!(id))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "New Item")
|
||||
|> assign(:item, %Item{})
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "Listing Items")
|
||||
|> assign(:item, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({WishWeb.ItemLive.FormComponent, {:saved, item}}, socket) do
|
||||
{:noreply, stream_insert(socket, :items, item)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
item = Wishlist.get_item!(id)
|
||||
{:ok, _} = Wishlist.delete_item(item)
|
||||
|
||||
{:noreply, stream_delete(socket, :items, item)}
|
||||
end
|
||||
end
|
||||
44
lib/wish_web/live/item_live/index.html.heex
Normal file
44
lib/wish_web/live/item_live/index.html.heex
Normal file
@@ -0,0 +1,44 @@
|
||||
<.header>
|
||||
Listing Items
|
||||
<:actions>
|
||||
<.link patch={~p"/items/new"}>
|
||||
<.button>New Item</.button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.table
|
||||
id="items"
|
||||
rows={@streams.items}
|
||||
row_click={fn {_id, item} -> JS.navigate(~p"/items/#{item}") end}
|
||||
>
|
||||
<:col :let={{_id, item}} label="Title"><%= item.title %></:col>
|
||||
<:col :let={{_id, item}} label="Description"><%= item.description %></:col>
|
||||
<:col :let={{_id, item}} label="Url"><%= item.url %></:col>
|
||||
<:col :let={{_id, item}} label="Desire"><%= item.desire %></:col>
|
||||
<:action :let={{_id, item}}>
|
||||
<div class="sr-only">
|
||||
<.link navigate={~p"/items/#{item}"}>Show</.link>
|
||||
</div>
|
||||
<.link patch={~p"/items/#{item}/edit"}>Edit</.link>
|
||||
</:action>
|
||||
<:action :let={{id, item}}>
|
||||
<.link
|
||||
phx-click={JS.push("delete", value: %{id: item.id}) |> hide("##{id}")}
|
||||
data-confirm="Are you sure?"
|
||||
>
|
||||
Delete
|
||||
</.link>
|
||||
</:action>
|
||||
</.table>
|
||||
|
||||
<.modal :if={@live_action in [:new, :edit]} id="item-modal" show on_cancel={JS.patch(~p"/items")}>
|
||||
<.live_component
|
||||
module={WishWeb.ItemLive.FormComponent}
|
||||
id={@item.id || :new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
item={@item}
|
||||
patch={~p"/items"}
|
||||
/>
|
||||
</.modal>
|
||||
21
lib/wish_web/live/item_live/show.ex
Normal file
21
lib/wish_web/live/item_live/show.ex
Normal file
@@ -0,0 +1,21 @@
|
||||
defmodule WishWeb.ItemLive.Show 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(:page_title, page_title(socket.assigns.live_action))
|
||||
|> assign(:item, Wishlist.get_item!(id))}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: "Show Item"
|
||||
defp page_title(:edit), do: "Edit Item"
|
||||
end
|
||||
30
lib/wish_web/live/item_live/show.html.heex
Normal file
30
lib/wish_web/live/item_live/show.html.heex
Normal file
@@ -0,0 +1,30 @@
|
||||
<.header>
|
||||
Item <%= @item.id %>
|
||||
<:subtitle>This is a item record from your database.</:subtitle>
|
||||
<:actions>
|
||||
<.link patch={~p"/items/#{@item}/show/edit"} phx-click={JS.push_focus()}>
|
||||
<.button>Edit item</.button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.list>
|
||||
<:item title="Title"><%= @item.title %></:item>
|
||||
<:item title="Description"><%= @item.description %></:item>
|
||||
<:item title="Url"><%= @item.url %></:item>
|
||||
<:item title="Received"><%= @item.received %></:item>
|
||||
<:item title="Desire"><%= @item.desire %></:item>
|
||||
</.list>
|
||||
|
||||
<.back navigate={~p"/items"}>Back to items</.back>
|
||||
|
||||
<.modal :if={@live_action == :edit} id="item-modal" show on_cancel={JS.patch(~p"/items/#{@item}")}>
|
||||
<.live_component
|
||||
module={WishWeb.ItemLive.FormComponent}
|
||||
id={@item.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
item={@item}
|
||||
patch={~p"/items/#{@item}"}
|
||||
/>
|
||||
</.modal>
|
||||
Reference in New Issue
Block a user