use shot group table component instead

This commit is contained in:
shibao 2022-12-03 21:40:05 -05:00
parent 45da547f62
commit 58b32817ba
15 changed files with 182 additions and 112 deletions

View File

@ -0,0 +1,117 @@
defmodule CanneryWeb.Components.ShotGroupTableComponent do
@moduledoc """
A component that displays a list of shot groups
"""
use CanneryWeb, :live_component
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Repo}
alias Ecto.UUID
alias Phoenix.LiveView.{Rendered, Socket}
@impl true
@spec update(
%{
required(:id) => UUID.t(),
required(:current_user) => User.t(),
optional(:shot_groups) => [ShotGroup.t()],
optional(:actions) => Rendered.t(),
optional(any()) => any()
},
Socket.t()
) :: {:ok, Socket.t()}
def update(%{id: _id, shot_groups: _shot_groups, current_user: _current_user} = assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign_new(:actions, fn -> [] end)
|> display_shot_groups()
{:ok, socket}
end
defp display_shot_groups(
%{
assigns: %{
shot_groups: shot_groups,
current_user: current_user,
actions: actions
}
} = socket
) do
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}
]
extra_data = %{current_user: current_user, actions: actions}
rows =
shot_groups
|> Enum.map(fn shot_group ->
shot_group |> get_row_data_for_shot_group(columns, extra_data)
end)
socket
|> assign(
columns: columns,
rows: rows
)
end
@impl true
def render(assigns) do
~H"""
<div id={@id} class="w-full">
<.live_component
module={CanneryWeb.Components.TableComponent}
id={"table-#{@id}"}
columns={@columns}
rows={@rows}
initial_key={:date}
initial_sort_mode={:desc}
/>
</div>
"""
end
@spec get_row_data_for_shot_group(ShotGroup.t(), columns :: [map()], extra_data :: map()) ::
map()
defp get_row_data_for_shot_group(shot_group, columns, extra_data) do
shot_group = shot_group |> Repo.preload(ammo_group: :ammo_type)
columns
|> Map.new(fn %{key: key} ->
{key, get_row_value(key, shot_group, extra_data)}
end)
end
defp get_row_value(
:name,
%{ammo_group: %{ammo_type: %{name: ammo_type_name} = ammo_group}},
_extra_data
) do
assigns = %{ammo_group: ammo_group, ammo_type_name: ammo_type_name}
name_block = ~H"""
<.link navigate={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} class="link">
<%= @ammo_type_name %>
</.link>
"""
{ammo_type_name, name_block}
end
defp get_row_value(:date, %{date: date}, _extra_data), do: date |> display_date()
defp get_row_value(:actions, shot_group, %{actions: actions}) do
assigns = %{actions: actions, shot_group: shot_group}
~H"""
<%= render_slot(@actions, @shot_group) %>
"""
end
defp get_row_value(key, shot_group, _extra_data), do: shot_group |> Map.get(key)
end

View File

@ -112,26 +112,11 @@ defmodule CanneryWeb.RangeLive.Index do
|> Repo.preload(ammo_group: :ammo_type)
ammo_groups = Ammo.list_staged_ammo_groups(current_user)
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 shot_group -> shot_group |> get_row_data_for_shot_group(columns) end)
chart_data = shot_groups |> get_chart_data_for_shot_group()
socket
|> assign(
ammo_groups: ammo_groups,
columns: columns,
rows: rows,
chart_data: chart_data,
shot_groups: shot_groups
)
@ -153,59 +138,4 @@ defmodule CanneryWeb.RangeLive.Index do
end)
|> Enum.sort_by(fn %{date: date} -> date end, Date)
end
@spec get_row_data_for_shot_group(ShotGroup.t(), [map()]) :: map()
defp get_row_data_for_shot_group(%{date: date} = shot_group, columns) do
shot_group = shot_group |> Repo.preload(ammo_group: :ammo_type)
assigns = %{shot_group: shot_group}
columns
|> Map.new(fn %{key: key} ->
value =
case key do
:name ->
{shot_group.ammo_group.ammo_type.name,
~H"""
<.link
navigate={Routes.ammo_group_show_path(Endpoint, :show, @shot_group.ammo_group)}
class="link"
>
<%= @shot_group.ammo_group.ammo_type.name %>
</.link>
"""}
:date ->
date |> display_date()
:actions ->
~H"""
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
<.link
patch={Routes.range_index_path(Endpoint, :edit, @shot_group)}
class="text-primary-600 link"
data-qa={"edit-#{@shot_group.id}"}
>
<i class="fa-fw fa-lg fas fa-edit"></i>
</.link>
<.link
href="#"
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?")}
data-qa={"delete-#{@shot_group.id}"}
>
<i class="fa-fw fa-lg fas fa-trash"></i>
</.link>
</div>
"""
key ->
shot_group |> Map.get(key)
end
{key, value}
end)
end
end

View File

@ -92,13 +92,36 @@
</h1>
<% else %>
<.live_component
module={CanneryWeb.Components.TableComponent}
module={CanneryWeb.Components.ShotGroupTableComponent}
id="shot_groups_index_table"
columns={@columns}
rows={@rows}
initial_key={:date}
initial_sort_mode={:desc}
/>
shot_groups={@shot_groups}
current_user={@current_user}
>
<:actions :let={shot_group}>
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
<.link
patch={Routes.range_index_path(Endpoint, :edit, shot_group)}
class="text-primary-600 link"
data-qa={"edit-#{shot_group.id}"}
>
<i class="fa-fw fa-lg fas fa-edit"></i>
</.link>
<.link
href="#"
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?")
}
data-qa={"delete-#{shot_group.id}"}
>
<i class="fa-fw fa-lg fas fa-trash"></i>
</.link>
</div>
</:actions>
</.live_component>
<% end %>
<% end %>
</div>

View File

@ -45,11 +45,11 @@ msgstr "Admins"
msgid "Admins:"
msgstr "Admins:"
#: lib/cannery_web/components/shot_group_table_component.ex:41
#: lib/cannery_web/components/topbar.ex:73
#: lib/cannery_web/live/ammo_group_live/index.ex:70
#: lib/cannery_web/live/ammo_group_live/index.ex:79
#: lib/cannery_web/live/ammo_group_live/index.html.heex:3
#: lib/cannery_web/live/range_live/index.ex:117
#, elixir-autogen, elixir-format
msgid "Ammo"
msgstr "Munition"
@ -313,10 +313,10 @@ msgstr "Keine Tags"
#: lib/cannery_web/components/add_shot_group_component.html.heex:37
#: lib/cannery_web/components/ammo_group_table_component.ex:81
#: lib/cannery_web/components/shot_group_table_component.ex:43
#: lib/cannery_web/live/ammo_group_live/form_component.html.heex:49
#: lib/cannery_web/live/ammo_group_live/show.ex:93
#: lib/cannery_web/live/range_live/form_component.html.heex:29
#: lib/cannery_web/live/range_live/index.ex:119
#, elixir-autogen, elixir-format
msgid "Notes"
msgstr "Bemerkungen"
@ -478,9 +478,9 @@ msgid "Range day"
msgstr "Range Day"
#: lib/cannery_web/components/add_shot_group_component.html.heex:45
#: lib/cannery_web/components/shot_group_table_component.ex:44
#: lib/cannery_web/live/ammo_group_live/show.ex:94
#: lib/cannery_web/live/range_live/form_component.html.heex:36
#: lib/cannery_web/live/range_live/index.ex:120
#, elixir-autogen, elixir-format
msgid "Date"
msgstr "Datum"
@ -524,8 +524,8 @@ msgstr "Keine Schüsse dokumentiert"
msgid "Rounds left"
msgstr "Patronen verbleibend"
#: lib/cannery_web/components/shot_group_table_component.ex:42
#: lib/cannery_web/live/ammo_group_live/show.ex:92
#: lib/cannery_web/live/range_live/index.ex:118
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format
msgid "Rounds shot"
@ -862,7 +862,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:151
#: lib/cannery_web/live/range_live/index.ex:136
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}"
msgstr "Patronen abgefeuert"

View File

@ -209,7 +209,7 @@ msgid "Are you sure you want to unstage this ammo?"
msgstr "Sind sie sicher, dass Sie diese Munition demarkieren möchten?"
#: lib/cannery_web/live/ammo_group_live/show.ex:137
#: lib/cannery_web/live/range_live/index.ex:196
#: lib/cannery_web/live/range_live/index.html.heex:116
#, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?"
msgstr "Sind sie sicher, dass sie die Schießkladde löschen möchten?"

View File

@ -30,11 +30,11 @@ msgstr ""
msgid "Admins:"
msgstr ""
#: lib/cannery_web/components/shot_group_table_component.ex:41
#: lib/cannery_web/components/topbar.ex:73
#: lib/cannery_web/live/ammo_group_live/index.ex:70
#: lib/cannery_web/live/ammo_group_live/index.ex:79
#: lib/cannery_web/live/ammo_group_live/index.html.heex:3
#: lib/cannery_web/live/range_live/index.ex:117
#, elixir-autogen, elixir-format
msgid "Ammo"
msgstr ""
@ -298,10 +298,10 @@ msgstr ""
#: lib/cannery_web/components/add_shot_group_component.html.heex:37
#: lib/cannery_web/components/ammo_group_table_component.ex:81
#: lib/cannery_web/components/shot_group_table_component.ex:43
#: lib/cannery_web/live/ammo_group_live/form_component.html.heex:49
#: lib/cannery_web/live/ammo_group_live/show.ex:93
#: lib/cannery_web/live/range_live/form_component.html.heex:29
#: lib/cannery_web/live/range_live/index.ex:119
#, elixir-autogen, elixir-format
msgid "Notes"
msgstr ""
@ -461,9 +461,9 @@ msgid "Range day"
msgstr ""
#: lib/cannery_web/components/add_shot_group_component.html.heex:45
#: lib/cannery_web/components/shot_group_table_component.ex:44
#: lib/cannery_web/live/ammo_group_live/show.ex:94
#: lib/cannery_web/live/range_live/form_component.html.heex:36
#: lib/cannery_web/live/range_live/index.ex:120
#, elixir-autogen, elixir-format
msgid "Date"
msgstr ""
@ -507,8 +507,8 @@ msgstr ""
msgid "Rounds left"
msgstr ""
#: lib/cannery_web/components/shot_group_table_component.ex:42
#: lib/cannery_web/live/ammo_group_live/show.ex:92
#: lib/cannery_web/live/range_live/index.ex:118
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format
msgid "Rounds shot"
@ -845,7 +845,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:151
#: lib/cannery_web/live/range_live/index.ex:136
#, elixir-autogen, elixir-format
msgid "Rounds shot: %{count}"
msgstr ""

View File

@ -31,11 +31,11 @@ msgstr ""
msgid "Admins:"
msgstr ""
#: lib/cannery_web/components/shot_group_table_component.ex:41
#: lib/cannery_web/components/topbar.ex:73
#: lib/cannery_web/live/ammo_group_live/index.ex:70
#: lib/cannery_web/live/ammo_group_live/index.ex:79
#: lib/cannery_web/live/ammo_group_live/index.html.heex:3
#: lib/cannery_web/live/range_live/index.ex:117
#, elixir-autogen, elixir-format
msgid "Ammo"
msgstr ""
@ -299,10 +299,10 @@ msgstr ""
#: lib/cannery_web/components/add_shot_group_component.html.heex:37
#: lib/cannery_web/components/ammo_group_table_component.ex:81
#: lib/cannery_web/components/shot_group_table_component.ex:43
#: lib/cannery_web/live/ammo_group_live/form_component.html.heex:49
#: lib/cannery_web/live/ammo_group_live/show.ex:93
#: lib/cannery_web/live/range_live/form_component.html.heex:29
#: lib/cannery_web/live/range_live/index.ex:119
#, elixir-autogen, elixir-format
msgid "Notes"
msgstr ""
@ -462,9 +462,9 @@ msgid "Range day"
msgstr ""
#: lib/cannery_web/components/add_shot_group_component.html.heex:45
#: lib/cannery_web/components/shot_group_table_component.ex:44
#: lib/cannery_web/live/ammo_group_live/show.ex:94
#: lib/cannery_web/live/range_live/form_component.html.heex:36
#: lib/cannery_web/live/range_live/index.ex:120
#, elixir-autogen, elixir-format
msgid "Date"
msgstr ""
@ -508,8 +508,8 @@ msgstr ""
msgid "Rounds left"
msgstr ""
#: lib/cannery_web/components/shot_group_table_component.ex:42
#: lib/cannery_web/live/ammo_group_live/show.ex:92
#: lib/cannery_web/live/range_live/index.ex:118
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format
msgid "Rounds shot"
@ -846,7 +846,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:151
#: lib/cannery_web/live/range_live/index.ex:136
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}"
msgstr ""

View File

@ -189,7 +189,7 @@ msgid "Are you sure you want to unstage this ammo?"
msgstr ""
#: lib/cannery_web/live/ammo_group_live/show.ex:137
#: lib/cannery_web/live/range_live/index.ex:196
#: lib/cannery_web/live/range_live/index.html.heex:116
#, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?"
msgstr ""

View File

@ -45,11 +45,11 @@ msgstr ""
msgid "Admins:"
msgstr ""
#: lib/cannery_web/components/shot_group_table_component.ex:41
#: lib/cannery_web/components/topbar.ex:73
#: lib/cannery_web/live/ammo_group_live/index.ex:70
#: lib/cannery_web/live/ammo_group_live/index.ex:79
#: lib/cannery_web/live/ammo_group_live/index.html.heex:3
#: lib/cannery_web/live/range_live/index.ex:117
#, elixir-autogen, elixir-format
msgid "Ammo"
msgstr ""
@ -313,10 +313,10 @@ msgstr ""
#: lib/cannery_web/components/add_shot_group_component.html.heex:37
#: lib/cannery_web/components/ammo_group_table_component.ex:81
#: lib/cannery_web/components/shot_group_table_component.ex:43
#: lib/cannery_web/live/ammo_group_live/form_component.html.heex:49
#: lib/cannery_web/live/ammo_group_live/show.ex:93
#: lib/cannery_web/live/range_live/form_component.html.heex:29
#: lib/cannery_web/live/range_live/index.ex:119
#, elixir-autogen, elixir-format
msgid "Notes"
msgstr ""
@ -476,9 +476,9 @@ msgid "Range day"
msgstr ""
#: lib/cannery_web/components/add_shot_group_component.html.heex:45
#: lib/cannery_web/components/shot_group_table_component.ex:44
#: lib/cannery_web/live/ammo_group_live/show.ex:94
#: lib/cannery_web/live/range_live/form_component.html.heex:36
#: lib/cannery_web/live/range_live/index.ex:120
#, elixir-autogen, elixir-format
msgid "Date"
msgstr ""
@ -522,8 +522,8 @@ msgstr ""
msgid "Rounds left"
msgstr ""
#: lib/cannery_web/components/shot_group_table_component.ex:42
#: lib/cannery_web/live/ammo_group_live/show.ex:92
#: lib/cannery_web/live/range_live/index.ex:118
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format
msgid "Rounds shot"
@ -860,7 +860,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:151
#: lib/cannery_web/live/range_live/index.ex:136
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}"
msgstr ""

View File

@ -208,7 +208,7 @@ msgid "Are you sure you want to unstage this ammo?"
msgstr "Está seguro que desea desmontar esta munición?"
#: lib/cannery_web/live/ammo_group_live/show.ex:137
#: lib/cannery_web/live/range_live/index.ex:196
#: lib/cannery_web/live/range_live/index.html.heex:116
#, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?"
msgstr ""

View File

@ -45,11 +45,11 @@ msgstr "Administrateur·ices"
msgid "Admins:"
msgstr "Administrateur·ices:"
#: lib/cannery_web/components/shot_group_table_component.ex:41
#: lib/cannery_web/components/topbar.ex:73
#: lib/cannery_web/live/ammo_group_live/index.ex:70
#: lib/cannery_web/live/ammo_group_live/index.ex:79
#: lib/cannery_web/live/ammo_group_live/index.html.heex:3
#: lib/cannery_web/live/range_live/index.ex:117
#, elixir-autogen, elixir-format
msgid "Ammo"
msgstr "Munition"
@ -313,10 +313,10 @@ msgstr "Aucun tag"
#: lib/cannery_web/components/add_shot_group_component.html.heex:37
#: lib/cannery_web/components/ammo_group_table_component.ex:81
#: lib/cannery_web/components/shot_group_table_component.ex:43
#: lib/cannery_web/live/ammo_group_live/form_component.html.heex:49
#: lib/cannery_web/live/ammo_group_live/show.ex:93
#: lib/cannery_web/live/range_live/form_component.html.heex:29
#: lib/cannery_web/live/range_live/index.ex:119
#, elixir-autogen, elixir-format
msgid "Notes"
msgstr "Notes"
@ -480,9 +480,9 @@ msgid "Range day"
msgstr "Journée de stand"
#: lib/cannery_web/components/add_shot_group_component.html.heex:45
#: lib/cannery_web/components/shot_group_table_component.ex:44
#: lib/cannery_web/live/ammo_group_live/show.ex:94
#: lib/cannery_web/live/range_live/form_component.html.heex:36
#: lib/cannery_web/live/range_live/index.ex:120
#, elixir-autogen, elixir-format
msgid "Date"
msgstr "Date"
@ -526,8 +526,8 @@ msgstr "Aucun tir enregistré"
msgid "Rounds left"
msgstr "Cartouches restantes"
#: lib/cannery_web/components/shot_group_table_component.ex:42
#: lib/cannery_web/live/ammo_group_live/show.ex:92
#: lib/cannery_web/live/range_live/index.ex:118
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format
msgid "Rounds shot"
@ -865,7 +865,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:151
#: lib/cannery_web/live/range_live/index.ex:136
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}"
msgstr "Cartouches tirées"

View File

@ -210,7 +210,7 @@ msgid "Are you sure you want to unstage this ammo?"
msgstr "Êtes-vous certain·e de vouloir désélectionner cette munition?"
#: lib/cannery_web/live/ammo_group_live/show.ex:137
#: lib/cannery_web/live/range_live/index.ex:196
#: lib/cannery_web/live/range_live/index.html.heex:116
#, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?"
msgstr "Êtes-vous certain·e de vouloir supprimer cet enregistrement de tir?"

View File

@ -41,11 +41,11 @@ msgstr ""
msgid "Admins:"
msgstr ""
#: lib/cannery_web/components/shot_group_table_component.ex:41
#: lib/cannery_web/components/topbar.ex:73
#: lib/cannery_web/live/ammo_group_live/index.ex:70
#: lib/cannery_web/live/ammo_group_live/index.ex:79
#: lib/cannery_web/live/ammo_group_live/index.html.heex:3
#: lib/cannery_web/live/range_live/index.ex:117
#, elixir-autogen, elixir-format
msgid "Ammo"
msgstr ""
@ -309,10 +309,10 @@ msgstr ""
#: lib/cannery_web/components/add_shot_group_component.html.heex:37
#: lib/cannery_web/components/ammo_group_table_component.ex:81
#: lib/cannery_web/components/shot_group_table_component.ex:43
#: lib/cannery_web/live/ammo_group_live/form_component.html.heex:49
#: lib/cannery_web/live/ammo_group_live/show.ex:93
#: lib/cannery_web/live/range_live/form_component.html.heex:29
#: lib/cannery_web/live/range_live/index.ex:119
#, elixir-autogen, elixir-format
msgid "Notes"
msgstr ""
@ -472,9 +472,9 @@ msgid "Range day"
msgstr ""
#: lib/cannery_web/components/add_shot_group_component.html.heex:45
#: lib/cannery_web/components/shot_group_table_component.ex:44
#: lib/cannery_web/live/ammo_group_live/show.ex:94
#: lib/cannery_web/live/range_live/form_component.html.heex:36
#: lib/cannery_web/live/range_live/index.ex:120
#, elixir-autogen, elixir-format
msgid "Date"
msgstr ""
@ -518,8 +518,8 @@ msgstr ""
msgid "Rounds left"
msgstr ""
#: lib/cannery_web/components/shot_group_table_component.ex:42
#: lib/cannery_web/live/ammo_group_live/show.ex:92
#: lib/cannery_web/live/range_live/index.ex:118
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format
msgid "Rounds shot"
@ -856,7 +856,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:151
#: lib/cannery_web/live/range_live/index.ex:136
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}"
msgstr ""

View File

@ -199,7 +199,7 @@ msgid "Are you sure you want to unstage this ammo?"
msgstr ""
#: lib/cannery_web/live/ammo_group_live/show.ex:137
#: lib/cannery_web/live/range_live/index.ex:196
#: lib/cannery_web/live/range_live/index.html.heex:116
#, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?"
msgstr ""

View File

@ -188,7 +188,7 @@ msgid "Are you sure you want to unstage this ammo?"
msgstr ""
#: lib/cannery_web/live/ammo_group_live/show.ex:137
#: lib/cannery_web/live/range_live/index.ex:196
#: lib/cannery_web/live/range_live/index.html.heex:116
#, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?"
msgstr ""