diff --git a/lib/cannery_web/components/move_ammo_group_component.ex b/lib/cannery_web/components/move_ammo_group_component.ex index 81fc2ec1..b3a49ba1 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 574b9ad3..00000000 --- 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 %> -