add models, context and liveviews
This commit is contained in:
55
lib/cannery_web/live/container_live/form_component.ex
Normal file
55
lib/cannery_web/live/container_live/form_component.ex
Normal file
@ -0,0 +1,55 @@
|
||||
defmodule CanneryWeb.ContainerLive.FormComponent do
|
||||
use CanneryWeb, :live_component
|
||||
|
||||
alias Cannery.Containers
|
||||
|
||||
@impl true
|
||||
def update(%{container: container} = assigns, socket) do
|
||||
changeset = Containers.change_container(container)
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign(:changeset, changeset)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"container" => container_params}, socket) do
|
||||
changeset =
|
||||
socket.assigns.container
|
||||
|> Containers.change_container(container_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{:noreply, socket |> assign(:changeset, changeset)}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"container" => container_params}, socket) do
|
||||
save_container(socket, socket.assigns.action, container_params)
|
||||
end
|
||||
|
||||
defp save_container(socket, :edit, container_params) do
|
||||
case Containers.update_container(socket.assigns.container, container_params) do
|
||||
{:ok, _container} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Container updated successfully")
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, socket |> assign(:changeset, changeset)}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_container(socket, :new, container_params) do
|
||||
case Containers.create_container(container_params) do
|
||||
{:ok, _container} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Container created successfully")
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, socket |> assign(changeset: changeset)}
|
||||
end
|
||||
end
|
||||
end
|
26
lib/cannery_web/live/container_live/form_component.html.leex
Normal file
26
lib/cannery_web/live/container_live/form_component.html.leex
Normal file
@ -0,0 +1,26 @@
|
||||
<h2><%= @title %></h2>
|
||||
|
||||
<%= f = form_for @changeset, "#",
|
||||
id: "container-form",
|
||||
phx_target: @myself,
|
||||
phx_change: "validate",
|
||||
phx_submit: "save" %>
|
||||
|
||||
<%= label f, :name %>
|
||||
<%= text_input f, :name %>
|
||||
<%= error_tag f, :name %>
|
||||
|
||||
<%= label f, :desc %>
|
||||
<%= text_input f, :desc %>
|
||||
<%= error_tag f, :desc %>
|
||||
|
||||
<%= label f, :type %>
|
||||
<%= text_input f, :type %>
|
||||
<%= error_tag f, :type %>
|
||||
|
||||
<%= label f, :location %>
|
||||
<%= text_input f, :location %>
|
||||
<%= error_tag f, :location %>
|
||||
|
||||
<%= submit "Save", phx_disable_with: "Saving..." %>
|
||||
</form>
|
46
lib/cannery_web/live/container_live/index.ex
Normal file
46
lib/cannery_web/live/container_live/index.ex
Normal file
@ -0,0 +1,46 @@
|
||||
defmodule CanneryWeb.ContainerLive.Index do
|
||||
use CanneryWeb, :live_view
|
||||
|
||||
alias Cannery.Containers
|
||||
alias Cannery.Containers.Container
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, socket |> assign(:containers, list_containers())}
|
||||
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 Container")
|
||||
|> assign(:container, Containers.get_container!(id))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "New Container")
|
||||
|> assign(:container, %Container{})
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "Listing Containers")
|
||||
|> assign(:container, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
container = Containers.get_container!(id)
|
||||
{:ok, _} = Containers.delete_container(container)
|
||||
|
||||
{:noreply, socket |> assign(:containers, list_containers())}
|
||||
end
|
||||
|
||||
defp list_containers do
|
||||
Containers.list_containers()
|
||||
end
|
||||
end
|
41
lib/cannery_web/live/container_live/index.html.leex
Normal file
41
lib/cannery_web/live/container_live/index.html.leex
Normal file
@ -0,0 +1,41 @@
|
||||
<h1>Listing Containers</h1>
|
||||
|
||||
<%= if @live_action in [:new, :edit] do %>
|
||||
<%= live_modal CanneryWeb.ContainerLive.FormComponent,
|
||||
id: @container.id || :new,
|
||||
title: @page_title,
|
||||
action: @live_action,
|
||||
container: @container,
|
||||
return_to: Routes.container_index_path(@socket, :index) %>
|
||||
<% end %>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Desc</th>
|
||||
<th>Type</th>
|
||||
<th>Location</th>
|
||||
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="containers">
|
||||
<%= for container <- @containers do %>
|
||||
<tr id="container-<%= container.id %>">
|
||||
<td><%= container.name %></td>
|
||||
<td><%= container.desc %></td>
|
||||
<td><%= container.type %></td>
|
||||
<td><%= container.location %></td>
|
||||
|
||||
<td>
|
||||
<span><%= live_redirect "Show", to: Routes.container_show_path(@socket, :show, container) %></span>
|
||||
<span><%= live_patch "Edit", to: Routes.container_index_path(@socket, :edit, container) %></span>
|
||||
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: container.id, data: [confirm: "Are you sure?"] %></span>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<span><%= live_patch "New Container", to: Routes.container_index_path(@socket, :new) %></span>
|
21
lib/cannery_web/live/container_live/show.ex
Normal file
21
lib/cannery_web/live/container_live/show.ex
Normal file
@ -0,0 +1,21 @@
|
||||
defmodule CanneryWeb.ContainerLive.Show do
|
||||
use CanneryWeb, :live_view
|
||||
|
||||
alias Cannery.Containers
|
||||
|
||||
@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(:container, Containers.get_container!(id))}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: "Show Container"
|
||||
defp page_title(:edit), do: "Edit Container"
|
||||
end
|
37
lib/cannery_web/live/container_live/show.html.leex
Normal file
37
lib/cannery_web/live/container_live/show.html.leex
Normal file
@ -0,0 +1,37 @@
|
||||
<h1>Show Container</h1>
|
||||
|
||||
<%= if @live_action in [:edit] do %>
|
||||
<%= live_modal CanneryWeb.ContainerLive.FormComponent,
|
||||
id: @container.id,
|
||||
title: @page_title,
|
||||
action: @live_action,
|
||||
container: @container,
|
||||
return_to: Routes.container_show_path(@socket, :show, @container) %>
|
||||
<% end %>
|
||||
|
||||
<ul>
|
||||
|
||||
<li>
|
||||
<strong>Name:</strong>
|
||||
<%= @container.name %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Desc:</strong>
|
||||
<%= @container.desc %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Type:</strong>
|
||||
<%= @container.type %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Location:</strong>
|
||||
<%= @container.location %>
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
|
||||
<span><%= live_patch "Edit", to: Routes.container_show_path(@socket, :edit, @container), class: "button" %></span>
|
||||
<span><%= live_redirect "Back", to: Routes.container_index_path(@socket, :index) %></span>
|
Reference in New Issue
Block a user