From da8c7889923ba434ef8a0ea8ca88b3d268952445 Mon Sep 17 00:00:00 2001 From: shibao Date: Fri, 4 Mar 2022 20:53:18 -0500 Subject: [PATCH] use table component for move ammo group component --- .../components/move_ammo_group_component.ex | 87 ++++++++++++++++++- .../move_ammo_group_component.html.heex | 70 --------------- 2 files changed, 85 insertions(+), 72 deletions(-) delete mode 100644 lib/cannery_web/components/move_ammo_group_component.html.heex diff --git a/lib/cannery_web/components/move_ammo_group_component.ex b/lib/cannery_web/components/move_ammo_group_component.ex index 81fc2ec..b3a49ba 100644 --- a/lib/cannery_web/components/move_ammo_group_component.ex +++ b/lib/cannery_web/components/move_ammo_group_component.ex @@ -4,7 +4,7 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do """ use CanneryWeb, :live_component - alias Cannery.{Accounts.User, Ammo, Ammo.AmmoGroup, Containers} + alias Cannery.{Accounts.User, Ammo, Ammo.AmmoGroup, Containers, Containers.Container} alias CanneryWeb.Endpoint alias Phoenix.LiveView.Socket @@ -28,7 +28,12 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do Containers.list_containers(current_user) |> Enum.reject(fn %{id: id} -> id == container_id end) - {:ok, socket |> assign(assigns) |> assign(changeset: changeset, containers: containers)} + socket = + socket + |> assign(assigns) + |> assign(changeset: changeset, containers: containers) + + {:ok, socket} end @impl true @@ -55,4 +60,82 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do {:noreply, socket} end + + @impl true + def render(%{containers: containers} = assigns) do + columns = [ + %{label: gettext("Container"), key: "name"}, + %{label: gettext("Type"), key: "type"}, + %{label: gettext("Location"), key: "location"}, + %{ + label: nil, + key: "actions", + sortable: false, + class: "px-4 py-2 space-x-4 flex justify-center items-center" + } + ] + + rows = containers |> get_rows_for_containers(assigns, columns) + + assigns = assigns |> Map.merge(%{columns: columns, rows: rows}) + + ~H""" +
+

+ <%= gettext("Move ammo") %> +

+ + <%= if @containers |> Enum.empty?() do %> +

+ <%= gettext("No other containers") %> + <%= display_emoji("😔") %> +

+ + <%= live_patch(dgettext("actions", "Add another container!"), + to: Routes.container_index_path(Endpoint, :new), + class: "btn btn-primary" + ) %> + <% else %> + <.live_component + module={CanneryWeb.Components.TableComponent} + id="move_ammo_group_table" + columns={@columns} + rows={@rows} + /> + <% end %> +
+ """ + end + + @spec get_rows_for_containers([Container.t()], map(), [map()]) :: [map()] + defp get_rows_for_containers(containers, assigns, columns) do + containers + |> Enum.map(fn container -> + assigns = assigns |> Map.put(:container, container) + + columns + |> Enum.into(%{}, fn %{key: key} -> + value = + case key do + "actions" -> + ~H""" + + """ + + key -> + container |> Map.get(key |> String.to_existing_atom()) + end + + {key, value} + end) + end) + end end diff --git a/lib/cannery_web/components/move_ammo_group_component.html.heex b/lib/cannery_web/components/move_ammo_group_component.html.heex deleted file mode 100644 index 574b9ad..0000000 --- a/lib/cannery_web/components/move_ammo_group_component.html.heex +++ /dev/null @@ -1,70 +0,0 @@ -
-

- <%= gettext("Move ammo") %> -

- - <%= if @containers |> Enum.empty?() do %> -

- <%= gettext("No other containers") %> - <%= display_emoji("😔") %> -

- - <%= live_patch(dgettext("actions", "Add another container!"), - to: Routes.container_index_path(Endpoint, :new), - class: "btn btn-primary" - ) %> - <% else %> -
- - - - - - - - - - - - - - <%= for container <- @containers do %> - - - - - - - - - - <% end %> - -
- <%= gettext("Container") %> - - <%= gettext("Type") %> - - <%= gettext("Location") %> -
- <%= container.name %> - - <%= container.type %> - - <%= container.location %> - -
- -
-
-
- <% end %> -