From af4af84515267e29c8574589b5064ba533063aa7 Mon Sep 17 00:00:00 2001 From: shibao Date: Fri, 4 Mar 2022 22:06:01 -0500 Subject: [PATCH] use table component for shot group table --- lib/cannery_web/live/range_live/index.ex | 61 +++++++++++++++- .../live/range_live/index.html.heex | 70 ++----------------- 2 files changed, 66 insertions(+), 65 deletions(-) diff --git a/lib/cannery_web/live/range_live/index.ex b/lib/cannery_web/live/range_live/index.ex index e0f6e64..27a0e73 100644 --- a/lib/cannery_web/live/range_live/index.ex +++ b/lib/cannery_web/live/range_live/index.ex @@ -77,6 +77,65 @@ defmodule CanneryWeb.RangeLive.Index do ActivityLog.list_shot_groups(current_user) |> Repo.preload(ammo_group: :ammo_type) ammo_groups = Ammo.list_staged_ammo_groups(current_user) - socket |> assign(shot_groups: shot_groups, ammo_groups: ammo_groups) + + columns = [ + %{label: gettext("Ammo"), key: "name"}, + %{label: gettext("Rounds shot"), key: "count"}, + %{label: gettext("Notes"), key: "notes"}, + %{label: gettext("Date"), key: "date"}, + %{label: nil, key: "actions", sortable: false} + ] + + rows = + shot_groups + |> Enum.map(fn %{date: date} = shot_group -> + assigns = %{shot_group: shot_group} + + columns + |> Enum.into(%{}, fn %{key: key} -> + value = + case key do + "name" -> + {shot_group.ammo_group.ammo_type.name, + live_patch(shot_group.ammo_group.ammo_type.name, + to: Routes.ammo_group_show_path(Endpoint, :show, shot_group.ammo_group), + class: "link" + )} + + "date" -> + date |> display_date() + + "actions" -> + ~H""" +
+ <%= live_patch to: Routes.range_index_path(Endpoint, :edit, 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", + 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 %> +
+ """ + + value -> + shot_group |> Map.get(key |> String.to_existing_atom()) + end + + {key, value} + end) + end) + + socket + |> assign(ammo_groups: ammo_groups, columns: columns, rows: rows, shot_groups: shot_groups) end end diff --git a/lib/cannery_web/live/range_live/index.html.heex b/lib/cannery_web/live/range_live/index.html.heex index 4d68f6d..538dd41 100644 --- a/lib/cannery_web/live/range_live/index.html.heex +++ b/lib/cannery_web/live/range_live/index.html.heex @@ -53,70 +53,12 @@ <%= gettext("Shot log") %> -
- - - - - - - - - - - - - <%= for shot_group <- @shot_groups do %> - - - - - - - - - <% end %> - -
- <%= gettext("Ammo") %> - - <%= gettext("Rounds shot") %> - - <%= gettext("Notes") %> - - <%= gettext("Date") %> -
- <%= live_patch(shot_group.ammo_group.ammo_type.name, - to: Routes.ammo_group_show_path(Endpoint, :show, shot_group.ammo_group), - class: "link" - ) %> - - <%= shot_group.count %> - - <%= shot_group.notes %> - - <%= shot_group.date |> display_date() %> - -
- <%= live_patch to: Routes.range_index_path(Endpoint, :edit, 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", - 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 %>