containers index

This commit is contained in:
shibao 2022-01-31 21:42:24 -05:00
parent 7ed77af3e0
commit 7aa353e176
5 changed files with 148 additions and 75 deletions

View File

@ -87,7 +87,19 @@ defmodule Cannery.Containers do
"""
@spec delete_container(Container.t() | Ecto.Changeset.t()) ::
{:ok, Container.t()} | {:error, Ecto.Changeset.t()}
def delete_container(container), do: Repo.delete(container)
def delete_container(container), do: container |> Repo.delete()
@doc """
Deletes a container.
## Examples
iex> delete_container(container)
%Container{}
"""
@spec delete_container!(Container.t() | Ecto.Changeset.t()) :: Container.t()
def delete_container!(container), do: container |> Repo.delete!()
@doc """
Returns an `%Ecto.Changeset{}` for tracking container changes.

View File

@ -9,16 +9,14 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
@impl true
def update(%{container: container} = assigns, socket) do
changeset = Containers.change_container(container)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
assigns = assigns |> Map.put(:changeset, container |> Containers.change_container())
{:ok, socket |> assign(assigns)}
end
@impl true
def handle_event("validate", %{"container" => container_params}, socket) do
container_params = container_params |> Map.put("user_id", socket.assigns.current_user.id)
changeset =
socket.assigns.container
|> Containers.change_container(container_params)
@ -28,6 +26,7 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
end
def handle_event("save", %{"container" => container_params}, socket) do
container_params = container_params |> Map.put("user_id", socket.assigns.current_user.id)
save_container(socket, socket.assigns.action, container_params)
end
@ -35,34 +34,56 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
def render(assigns) do
~H"""
<div>
<h2>
<h2 class="title text-xl text-primary-500">
<%= @title %>
</h2>
<.form
let={f}
for={@changeset}
id="container-form"
class="grid grid-cols-3 justify-center items-center space-y-4"
phx-target={@myself}
phx-change="validate"
phx-submit="save">
<%= label f, :name, class: "title text-lg text-primary-500" %>
<%= text_input f, :name, class: "input input-primary" %>
<%= error_tag f, :name %>
<%= label f, :desc, class: "title text-lg text-primary-500" %>
<%= text_input f, :desc, class: "input input-primary" %>
<%= error_tag f, :desc %>
<%= label f, :type, class: "title text-lg text-primary-500" %>
<%= text_input f, :type, class: "input input-primary" %>
<%= error_tag f, :type %>
<%= label f, :location, class: "title text-lg text-primary-500" %>
<%= text_input f, :location, class: "input input-primary" %>
<%= error_tag f, :location %>
<%= submit "Save", phx_disable_with: "Saving..." %>
phx-submit="save"
>
<%= label(f, :name, class: "title text-lg text-primary-500") %>
<%= text_input(f, :name,
class: "input input-primary col-span-2",
placeholder: "My cool ammo can"
) %>
<span class="col-span-3">
<%= error_tag(f, :name) %>
</span>
<%= label(f, :desc, class: "title text-lg text-primary-500") %>
<%= textarea(f, :desc,
class: "input input-primary col-span-2",
phx_hook: "MaintainAttrs",
placeholder: "Metal ammo can with the anime girl sticker"
) %>
<span class="col-span-3">
<%= error_tag(f, :desc) %>
</span>
<%= label(f, :type, class: "title text-lg text-primary-500") %>
<%= text_input(f, :type,
class: "input input-primary col-span-2",
placeholder: "Magazine, Clip, Ammo Box, etc"
) %>
<span class="col-span-3">
<%= error_tag(f, :type) %>
</span>
<%= label(f, :location, class: "title text-lg text-primary-500") %>
<%= textarea(f, :location,
class: "input input-primary col-span-2",
phx_hook: "MaintainAttrs",
placeholder: "On the bookshelf"
) %>
<span class="col-span-3">
<%= error_tag(f, :location) %>
</span>
<%= submit("Save",
class: "mx-auto btn btn-primary col-span-3",
phx_disable_with: "Saving..."
) %>
</.form>
</div>
"""

View File

@ -10,7 +10,7 @@ defmodule CanneryWeb.ContainerLive.Index do
@impl true
def mount(_params, session, socket) do
{:ok, socket |> assign_defaults(session) |> assign(:containers, list_containers())}
{:ok, socket |> assign_defaults(session) |> display_containers()}
end
@impl true
@ -38,13 +38,12 @@ defmodule CanneryWeb.ContainerLive.Index do
@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())}
Containers.get_container!(id) |> Containers.delete_container!()
{:noreply, socket |> display_containers()}
end
defp list_containers do
Containers.list_containers()
defp display_containers(%{assigns: %{current_user: current_user}} = socket) do
containers = Containers.list_containers(current_user)
socket |> assign(containers: containers)
end
end

View File

@ -0,0 +1,82 @@
<div class="flex flex-col space-y-8 justify-center items-center">
<h1 class="title text-2xl title-primary-500">
Listing Containers
</h1>
<%= if @containers |> Enum.empty?() do %>
<div class="flex flex-col space-y-4 justify-center items-center">
<h1 class="title text-xl text-primary-500">
No containers
</h1>
<%= live_patch to: Routes.container_index_path(@socket, :new),
class: "btn btn-primary" do %>
Create your first container!
<% end %>
</div>
<% else %>
<%= live_patch to: Routes.container_index_path(@socket, :new),
class: "btn btn-primary" do %>
New Container
<% end %>
<% end %>
<div class="flex flex-row flex-wrap">
<%= for container <- @containers do %>
<div id={"container-#{container.id}"}
class="px-8 py-4 flex flex-col justify-center items-center
border border-gray-400 rounded-lg shadow-lg hover:shadow-md">
<div class="mb-4 flex flex-col justify-center items-center">
<h1 class="px-4 py-2 rounded-lg title text-xl">
<%= container.name %>
</h1>
<%= if container.desc do %>
<span class="rounded-lg title text-lg">
Description: <%= container.desc %>
</span>
<% end %>
<span class="rounded-lg title text-lg">
Type: <%= container.type %>
</span>
<%= if container.location do %>
<span class="rounded-lg title text-lg">
Location: <%= container.location %>
</span>
<% end %>
</div>
<div class="flex space-x-4 justify-center items-center">
<%= live_redirect("Show",
to: Routes.container_show_path(@socket, :show, container),
class: "text-primary-500 link"
) %>
<%= live_patch("Edit",
to: Routes.container_index_path(@socket, :edit, container),
class: "text-primary-500 link"
) %>
<%= link("Delete",
to: "#",
class: "text-primary-500 link",
phx_click: "delete",
phx_value_id: container.id,
data: [confirm: "Are you sure you want to delete #{container.name}?"]
) %>
</div>
</div>
<% end %>
</div>
</div>
<%= 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),
current_user: @current_user
) %>
<% end %>

View File

@ -1,41 +0,0 @@
<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>