From f120e54c3ed0fe8ee99e232d4353ede23f72428b Mon Sep 17 00:00:00 2001 From: shibao Date: Fri, 4 Mar 2022 21:57:22 -0500 Subject: [PATCH] use table component for ammo group show table --- lib/cannery_web/live/ammo_group_live/show.ex | 59 ++++++++++++++++- .../live/ammo_group_live/show.html.heex | 63 ++----------------- 2 files changed, 63 insertions(+), 59 deletions(-) diff --git a/lib/cannery_web/live/ammo_group_live/show.ex b/lib/cannery_web/live/ammo_group_live/show.ex index e6f1053..c29a44b 100644 --- a/lib/cannery_web/live/ammo_group_live/show.ex +++ b/lib/cannery_web/live/ammo_group_live/show.ex @@ -5,7 +5,7 @@ defmodule CanneryWeb.AmmoGroupLive.Show do use CanneryWeb, :live_view import CanneryWeb.Components.ContainerCard - alias Cannery.{ActivityLog, Ammo, Ammo.AmmoGroup, Repo} + alias Cannery.{ActivityLog, ActivityLog.ShotGroup, Ammo, Ammo.AmmoGroup, Repo} alias CanneryWeb.Endpoint alias Phoenix.LiveView.Socket @@ -84,9 +84,64 @@ defmodule CanneryWeb.AmmoGroupLive.Show do @spec display_ammo_group(Socket.t(), AmmoGroup.t() | AmmoGroup.id()) :: Socket.t() defp display_ammo_group(socket, %AmmoGroup{} = ammo_group) do ammo_group = ammo_group |> Repo.preload([:container, :ammo_type, :shot_groups], force: true) - socket |> assign(:ammo_group, ammo_group) + + columns = [ + %{label: gettext("Rounds shot"), key: "count"}, + %{label: gettext("Notes"), key: "notes"}, + %{label: gettext("Date"), key: "date"}, + %{label: nil, key: "actions", sortable: false} + ] + + rows = + ammo_group.shot_groups + |> Enum.map(fn shot_group -> + ammo_group |> get_table_row_for_shot_group(shot_group, columns) + end) + + socket |> assign(ammo_group: ammo_group, columns: columns, rows: rows) end defp display_ammo_group(%{assigns: %{current_user: current_user}} = socket, id), do: display_ammo_group(socket, Ammo.get_ammo_group!(id, current_user)) + + @spec get_table_row_for_shot_group(AmmoGroup.t(), ShotGroup.t(), [map()]) :: [map()] + defp get_table_row_for_shot_group(ammo_group, %{date: date} = shot_group, columns) do + assigns = %{ammo_group: ammo_group, shot_group: shot_group} + + columns + |> Enum.into(%{}, fn %{key: key} -> + value = + case key do + "date" -> + {date, date |> display_date()} + + "actions" -> + ~H""" +
+ <%= live_patch to: Routes.ammo_group_show_path(Endpoint, :edit_shot_group, @ammo_group, shot_group), + class: "text-primary-600 link", + data: [qa: "edit-#{shot_group.id}"] do %> + + <% end %> + + <%= link to: "#", + class: "text-primary-600 link", + phx_click: "delete_shot_group", + phx_value_id: shot_group.id, + data: [ + confirm: dgettext("prompts", "Are you sure you want to delete this shot record?"), + qa: "delete-#{shot_group.id}" + ] do %> + + <% end %> +
+ """ + + key -> + shot_group |> Map.get(key |> String.to_existing_atom()) + end + + {key, value} + end) + end end diff --git a/lib/cannery_web/live/ammo_group_live/show.html.heex b/lib/cannery_web/live/ammo_group_live/show.html.heex index 36a4ad8..f70b529 100644 --- a/lib/cannery_web/live/ammo_group_live/show.html.heex +++ b/lib/cannery_web/live/ammo_group_live/show.html.heex @@ -111,63 +111,12 @@ <%= gettext("Rounds used") %> -
- - - - - - - - - - - - <%= for shot_group <- @ammo_group.shot_groups do %> - - - - - - - - - - <% end %> - -
- <%= gettext("Rounds shot") %> - - <%= gettext("Notes") %> - - <%= gettext("Date") %> -
- <%= shot_group.count %> - - <%= shot_group.notes %> - - <%= shot_group.date |> display_date() %> - -
- <%= live_patch to: Routes.ammo_group_show_path(Endpoint, :edit_shot_group, @ammo_group, shot_group), - class: "text-primary-600 link", - data: [qa: "edit-#{shot_group.id}"] do %> - - <% end %> - - <%= link to: "#", - class: "text-primary-600 link", - phx_click: "delete_shot_group", - phx_value_id: shot_group.id, - data: [ - confirm: dgettext("prompts", "Are you sure you want to delete this shot record?"), - qa: "delete-#{shot_group.id}" - ] do %> - - <% end %> -
-
-
+ <.live_component + module={CanneryWeb.Components.TableComponent} + id="shot_groups_table" + columns={@columns} + rows={@rows} + /> <% end %>