containers index
This commit is contained in:
parent
7ed77af3e0
commit
7aa353e176
@ -87,7 +87,19 @@ defmodule Cannery.Containers do
|
|||||||
"""
|
"""
|
||||||
@spec delete_container(Container.t() | Ecto.Changeset.t()) ::
|
@spec delete_container(Container.t() | Ecto.Changeset.t()) ::
|
||||||
{:ok, Container.t()} | {:error, 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 """
|
@doc """
|
||||||
Returns an `%Ecto.Changeset{}` for tracking container changes.
|
Returns an `%Ecto.Changeset{}` for tracking container changes.
|
||||||
|
@ -9,16 +9,14 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
|||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def update(%{container: container} = assigns, socket) do
|
def update(%{container: container} = assigns, socket) do
|
||||||
changeset = Containers.change_container(container)
|
assigns = assigns |> Map.put(:changeset, container |> Containers.change_container())
|
||||||
|
{:ok, socket |> assign(assigns)}
|
||||||
{:ok,
|
|
||||||
socket
|
|
||||||
|> assign(assigns)
|
|
||||||
|> assign(:changeset, changeset)}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_event("validate", %{"container" => container_params}, socket) do
|
def handle_event("validate", %{"container" => container_params}, socket) do
|
||||||
|
container_params = container_params |> Map.put("user_id", socket.assigns.current_user.id)
|
||||||
|
|
||||||
changeset =
|
changeset =
|
||||||
socket.assigns.container
|
socket.assigns.container
|
||||||
|> Containers.change_container(container_params)
|
|> Containers.change_container(container_params)
|
||||||
@ -28,6 +26,7 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("save", %{"container" => container_params}, socket) do
|
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)
|
save_container(socket, socket.assigns.action, container_params)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -35,34 +34,56 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
|||||||
def render(assigns) do
|
def render(assigns) do
|
||||||
~H"""
|
~H"""
|
||||||
<div>
|
<div>
|
||||||
<h2>
|
<h2 class="title text-xl text-primary-500">
|
||||||
<%= @title %>
|
<%= @title %>
|
||||||
</h2>
|
</h2>
|
||||||
<.form
|
<.form
|
||||||
let={f}
|
let={f}
|
||||||
for={@changeset}
|
for={@changeset}
|
||||||
id="container-form"
|
id="container-form"
|
||||||
|
class="grid grid-cols-3 justify-center items-center space-y-4"
|
||||||
phx-target={@myself}
|
phx-target={@myself}
|
||||||
phx-change="validate"
|
phx-change="validate"
|
||||||
phx-submit="save">
|
phx-submit="save"
|
||||||
|
>
|
||||||
<%= label f, :name, class: "title text-lg text-primary-500" %>
|
<%= label(f, :name, class: "title text-lg text-primary-500") %>
|
||||||
<%= text_input f, :name, class: "input input-primary" %>
|
<%= text_input(f, :name,
|
||||||
<%= error_tag f, :name %>
|
class: "input input-primary col-span-2",
|
||||||
|
placeholder: "My cool ammo can"
|
||||||
<%= label f, :desc, class: "title text-lg text-primary-500" %>
|
) %>
|
||||||
<%= text_input f, :desc, class: "input input-primary" %>
|
<span class="col-span-3">
|
||||||
<%= error_tag f, :desc %>
|
<%= error_tag(f, :name) %>
|
||||||
|
</span>
|
||||||
<%= label f, :type, class: "title text-lg text-primary-500" %>
|
<%= label(f, :desc, class: "title text-lg text-primary-500") %>
|
||||||
<%= text_input f, :type, class: "input input-primary" %>
|
<%= textarea(f, :desc,
|
||||||
<%= error_tag f, :type %>
|
class: "input input-primary col-span-2",
|
||||||
|
phx_hook: "MaintainAttrs",
|
||||||
<%= label f, :location, class: "title text-lg text-primary-500" %>
|
placeholder: "Metal ammo can with the anime girl sticker"
|
||||||
<%= text_input f, :location, class: "input input-primary" %>
|
) %>
|
||||||
<%= error_tag f, :location %>
|
<span class="col-span-3">
|
||||||
|
<%= error_tag(f, :desc) %>
|
||||||
<%= submit "Save", phx_disable_with: "Saving..." %>
|
</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>
|
</.form>
|
||||||
</div>
|
</div>
|
||||||
"""
|
"""
|
||||||
|
@ -10,7 +10,7 @@ defmodule CanneryWeb.ContainerLive.Index do
|
|||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def mount(_params, session, socket) do
|
def mount(_params, session, socket) do
|
||||||
{:ok, socket |> assign_defaults(session) |> assign(:containers, list_containers())}
|
{:ok, socket |> assign_defaults(session) |> display_containers()}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
@ -38,13 +38,12 @@ defmodule CanneryWeb.ContainerLive.Index do
|
|||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_event("delete", %{"id" => id}, socket) do
|
def handle_event("delete", %{"id" => id}, socket) do
|
||||||
container = Containers.get_container!(id)
|
Containers.get_container!(id) |> Containers.delete_container!()
|
||||||
{:ok, _} = Containers.delete_container(container)
|
{:noreply, socket |> display_containers()}
|
||||||
|
|
||||||
{:noreply, socket |> assign(:containers, list_containers())}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp list_containers do
|
defp display_containers(%{assigns: %{current_user: current_user}} = socket) do
|
||||||
Containers.list_containers()
|
containers = Containers.list_containers(current_user)
|
||||||
|
socket |> assign(containers: containers)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
82
lib/cannery_web/live/container_live/index.html.heex
Normal file
82
lib/cannery_web/live/container_live/index.html.heex
Normal 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 %>
|
@ -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>
|
|
Loading…
Reference in New Issue
Block a user