diff --git a/lib/cannery/containers.ex b/lib/cannery/containers.ex
index 8e8a390c..096737cc 100644
--- a/lib/cannery/containers.ex
+++ b/lib/cannery/containers.ex
@@ -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.
diff --git a/lib/cannery_web/live/container_live/form_component.ex b/lib/cannery_web/live/container_live/form_component.ex
index 51e63314..d0c5e2be 100644
--- a/lib/cannery_web/live/container_live/form_component.ex
+++ b/lib/cannery_web/live/container_live/form_component.ex
@@ -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"""
-
+
<%= @title %>
<.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"
+ ) %>
+
+ <%= error_tag(f, :name) %>
+
+ <%= 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"
+ ) %>
+
+ <%= error_tag(f, :desc) %>
+
+ <%= 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"
+ ) %>
+
+ <%= error_tag(f, :type) %>
+
+ <%= 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"
+ ) %>
+
+ <%= error_tag(f, :location) %>
+
+ <%= submit("Save",
+ class: "mx-auto btn btn-primary col-span-3",
+ phx_disable_with: "Saving..."
+ ) %>
"""
diff --git a/lib/cannery_web/live/container_live/index.ex b/lib/cannery_web/live/container_live/index.ex
index 8619c797..f269a152 100644
--- a/lib/cannery_web/live/container_live/index.ex
+++ b/lib/cannery_web/live/container_live/index.ex
@@ -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
diff --git a/lib/cannery_web/live/container_live/index.html.heex b/lib/cannery_web/live/container_live/index.html.heex
new file mode 100644
index 00000000..91a25878
--- /dev/null
+++ b/lib/cannery_web/live/container_live/index.html.heex
@@ -0,0 +1,82 @@
+
+
+ Listing Containers
+
+
+ <%= if @containers |> Enum.empty?() do %>
+
+
+ No containers
+
+
+ <%= live_patch to: Routes.container_index_path(@socket, :new),
+ class: "btn btn-primary" do %>
+ Create your first container!
+ <% end %>
+
+ <% else %>
+ <%= live_patch to: Routes.container_index_path(@socket, :new),
+ class: "btn btn-primary" do %>
+ New Container
+ <% end %>
+ <% end %>
+
+
+ <%= for container <- @containers do %>
+
+
+
+ <%= container.name %>
+
+
+ <%= if container.desc do %>
+
+ Description: <%= container.desc %>
+
+ <% end %>
+
+
+ Type: <%= container.type %>
+
+
+ <%= if container.location do %>
+
+ Location: <%= container.location %>
+
+ <% end %>
+
+
+
+ <%= 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}?"]
+ ) %>
+
+
+ <% end %>
+
+
+
+<%= 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 %>
diff --git a/lib/cannery_web/live/container_live/index.html.leex b/lib/cannery_web/live/container_live/index.html.leex
deleted file mode 100644
index 9fc2d627..00000000
--- a/lib/cannery_web/live/container_live/index.html.leex
+++ /dev/null
@@ -1,41 +0,0 @@
-Listing Containers
-
-<%= 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 %>
-
-
-
-
- Name |
- Desc |
- Type |
- Location |
-
- |
-
-
-
- <%= for container <- @containers do %>
-
- <%= container.name %> |
- <%= container.desc %> |
- <%= container.type %> |
- <%= container.location %> |
-
-
- <%= live_redirect "Show", to: Routes.container_show_path(@socket, :show, container) %>
- <%= live_patch "Edit", to: Routes.container_index_path(@socket, :edit, container) %>
- <%= link "Delete", to: "#", phx_click: "delete", phx_value_id: container.id, data: [confirm: "Are you sure?"] %>
- |
-
- <% end %>
-
-
-
-<%= live_patch "New Container", to: Routes.container_index_path(@socket, :new) %>