2023-03-29 22:54:55 -04:00
|
|
|
defmodule CanneryWeb.Components.PackTableComponent do
|
2022-11-12 13:52:24 -05:00
|
|
|
@moduledoc """
|
2023-03-29 23:49:45 -04:00
|
|
|
A component that displays a list of packs
|
2022-11-12 13:52:24 -05:00
|
|
|
"""
|
|
|
|
use CanneryWeb, :live_component
|
2023-03-29 22:54:55 -04:00
|
|
|
alias Cannery.{Accounts.User, Ammo.Pack, ComparableDate}
|
2023-03-19 11:19:55 -04:00
|
|
|
alias Cannery.{ActivityLog, Ammo, Containers}
|
2023-03-23 22:07:25 -04:00
|
|
|
alias CanneryWeb.Components.TableComponent
|
2022-11-12 13:52:24 -05:00
|
|
|
alias Ecto.UUID
|
|
|
|
alias Phoenix.LiveView.{Rendered, Socket}
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
@spec update(
|
|
|
|
%{
|
|
|
|
required(:id) => UUID.t(),
|
|
|
|
required(:current_user) => User.t(),
|
2023-03-29 22:54:55 -04:00
|
|
|
required(:packs) => [Pack.t()],
|
2023-03-19 14:11:01 -04:00
|
|
|
required(:show_used) => boolean(),
|
2022-11-12 13:52:24 -05:00
|
|
|
optional(:ammo_type) => Rendered.t(),
|
|
|
|
optional(:range) => Rendered.t(),
|
|
|
|
optional(:container) => Rendered.t(),
|
|
|
|
optional(:actions) => Rendered.t(),
|
|
|
|
optional(any()) => any()
|
|
|
|
},
|
|
|
|
Socket.t()
|
|
|
|
) :: {:ok, Socket.t()}
|
2023-03-19 14:11:01 -04:00
|
|
|
def update(
|
2023-03-29 22:54:55 -04:00
|
|
|
%{id: _id, packs: _pack, current_user: _current_user, show_used: _show_used} = assigns,
|
2023-03-19 14:11:01 -04:00
|
|
|
socket
|
|
|
|
) do
|
2022-11-12 13:52:24 -05:00
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> assign(assigns)
|
|
|
|
|> assign_new(:ammo_type, fn -> [] end)
|
|
|
|
|> assign_new(:range, fn -> [] end)
|
|
|
|
|> assign_new(:container, fn -> [] end)
|
|
|
|
|> assign_new(:actions, fn -> [] end)
|
2023-03-29 22:54:55 -04:00
|
|
|
|> display_packs()
|
2022-11-12 13:52:24 -05:00
|
|
|
|
|
|
|
{:ok, socket}
|
|
|
|
end
|
|
|
|
|
2023-03-29 22:54:55 -04:00
|
|
|
defp display_packs(
|
2022-11-12 13:52:24 -05:00
|
|
|
%{
|
|
|
|
assigns: %{
|
2023-03-29 22:54:55 -04:00
|
|
|
packs: packs,
|
2022-11-12 13:52:24 -05:00
|
|
|
current_user: current_user,
|
|
|
|
ammo_type: ammo_type,
|
|
|
|
range: range,
|
|
|
|
container: container,
|
2023-03-19 14:11:01 -04:00
|
|
|
actions: actions,
|
|
|
|
show_used: show_used
|
2022-11-12 13:52:24 -05:00
|
|
|
}
|
|
|
|
} = socket
|
|
|
|
) do
|
|
|
|
columns =
|
2023-03-23 22:07:25 -04:00
|
|
|
[]
|
|
|
|
|> TableComponent.maybe_compose_columns(
|
|
|
|
%{label: gettext("Actions"), key: :actions, sortable: false},
|
|
|
|
actions != []
|
|
|
|
)
|
|
|
|
|> TableComponent.maybe_compose_columns(%{
|
|
|
|
label: gettext("Last used on"),
|
|
|
|
key: :used_up_on,
|
|
|
|
type: ComparableDate
|
|
|
|
})
|
|
|
|
|> TableComponent.maybe_compose_columns(%{
|
|
|
|
label: gettext("Purchased on"),
|
|
|
|
key: :purchased_on,
|
|
|
|
type: ComparableDate
|
|
|
|
})
|
|
|
|
|> TableComponent.maybe_compose_columns(
|
|
|
|
%{label: gettext("Container"), key: :container},
|
|
|
|
container != []
|
|
|
|
)
|
|
|
|
|> TableComponent.maybe_compose_columns(
|
|
|
|
%{label: gettext("Range"), key: :range},
|
|
|
|
range != []
|
|
|
|
)
|
|
|
|
|> TableComponent.maybe_compose_columns(%{label: gettext("CPR"), key: :cpr})
|
|
|
|
|> TableComponent.maybe_compose_columns(%{label: gettext("Price paid"), key: :price_paid})
|
|
|
|
|> TableComponent.maybe_compose_columns(
|
|
|
|
%{label: gettext("% left"), key: :remaining},
|
|
|
|
show_used
|
|
|
|
)
|
|
|
|
|> TableComponent.maybe_compose_columns(
|
|
|
|
%{label: gettext("Original Count"), key: :original_count},
|
|
|
|
show_used
|
|
|
|
)
|
|
|
|
|> TableComponent.maybe_compose_columns(%{
|
|
|
|
label: if(show_used, do: gettext("Current Count"), else: gettext("Count")),
|
|
|
|
key: :count
|
|
|
|
})
|
|
|
|
|> TableComponent.maybe_compose_columns(
|
|
|
|
%{label: gettext("Ammo type"), key: :ammo_type},
|
|
|
|
ammo_type != []
|
|
|
|
)
|
2022-11-12 13:52:24 -05:00
|
|
|
|
2023-03-19 15:05:09 -04:00
|
|
|
containers =
|
2023-03-29 22:54:55 -04:00
|
|
|
packs
|
2023-03-19 15:05:09 -04:00
|
|
|
|> Enum.map(fn %{container_id: container_id} -> container_id end)
|
|
|
|
|> Containers.get_containers(current_user)
|
|
|
|
|
2022-12-03 21:41:54 -05:00
|
|
|
extra_data = %{
|
|
|
|
current_user: current_user,
|
|
|
|
ammo_type: ammo_type,
|
|
|
|
columns: columns,
|
|
|
|
container: container,
|
2023-03-19 15:05:09 -04:00
|
|
|
containers: containers,
|
2023-03-29 22:54:55 -04:00
|
|
|
original_counts: Ammo.get_original_counts(packs, current_user),
|
|
|
|
cprs: Ammo.get_cprs(packs, current_user),
|
|
|
|
last_used_dates: ActivityLog.get_last_used_dates(packs, current_user),
|
|
|
|
percentages_remaining: Ammo.get_percentages_remaining(packs, current_user),
|
2022-12-03 21:41:54 -05:00
|
|
|
actions: actions,
|
|
|
|
range: range
|
|
|
|
}
|
|
|
|
|
2022-11-12 13:52:24 -05:00
|
|
|
rows =
|
2023-03-29 22:54:55 -04:00
|
|
|
packs
|
|
|
|
|> Enum.map(fn pack ->
|
|
|
|
pack |> get_row_data_for_pack(extra_data)
|
2022-11-12 13:52:24 -05:00
|
|
|
end)
|
|
|
|
|
|
|
|
socket |> assign(columns: columns, rows: rows)
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
|
|
|
~H"""
|
2022-11-19 13:16:13 -05:00
|
|
|
<div id={@id} class="w-full">
|
2023-03-23 22:07:25 -04:00
|
|
|
<.live_component module={TableComponent} id={"table-#{@id}"} columns={@columns} rows={@rows} />
|
2022-11-12 13:52:24 -05:00
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2023-03-29 22:54:55 -04:00
|
|
|
@spec get_row_data_for_pack(Pack.t(), additional_data :: map()) :: map()
|
|
|
|
defp get_row_data_for_pack(pack, %{columns: columns} = additional_data) do
|
2022-11-12 13:52:24 -05:00
|
|
|
columns
|
|
|
|
|> Map.new(fn %{key: key} ->
|
2023-03-29 22:54:55 -04:00
|
|
|
{key, get_value_for_key(key, pack, additional_data)}
|
2022-11-12 13:52:24 -05:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2023-03-29 22:54:55 -04:00
|
|
|
@spec get_value_for_key(atom(), Pack.t(), additional_data :: map()) ::
|
2022-11-12 13:52:24 -05:00
|
|
|
any() | {any(), Rendered.t()}
|
|
|
|
defp get_value_for_key(
|
|
|
|
:ammo_type,
|
|
|
|
%{ammo_type: %{name: ammo_type_name} = ammo_type},
|
|
|
|
%{ammo_type: ammo_type_block}
|
|
|
|
) do
|
|
|
|
assigns = %{ammo_type: ammo_type, ammo_type_block: ammo_type_block}
|
|
|
|
|
|
|
|
{ammo_type_name,
|
|
|
|
~H"""
|
|
|
|
<%= render_slot(@ammo_type_block, @ammo_type) %>
|
|
|
|
"""}
|
|
|
|
end
|
|
|
|
|
2023-03-19 14:11:01 -04:00
|
|
|
defp get_value_for_key(:price_paid, %{price_paid: nil}, _additional_data),
|
2023-03-19 14:31:53 -04:00
|
|
|
do: {0, gettext("No cost information")}
|
2022-11-12 13:52:24 -05:00
|
|
|
|
|
|
|
defp get_value_for_key(:price_paid, %{price_paid: price_paid}, _additional_data),
|
2023-03-19 14:31:53 -04:00
|
|
|
do: {price_paid, gettext("$%{amount}", amount: display_currency(price_paid))}
|
2022-11-12 13:52:24 -05:00
|
|
|
|
2023-03-18 21:06:00 -04:00
|
|
|
defp get_value_for_key(:purchased_on, %{purchased_on: purchased_on} = assigns, _additional_data) do
|
2022-11-19 14:52:01 -05:00
|
|
|
{purchased_on,
|
2022-11-12 13:52:24 -05:00
|
|
|
~H"""
|
2023-03-18 21:06:00 -04:00
|
|
|
<.date id={"#{@id}-purchased-on"} date={@purchased_on} />
|
2022-11-12 13:52:24 -05:00
|
|
|
"""}
|
|
|
|
end
|
|
|
|
|
2023-03-29 22:54:55 -04:00
|
|
|
defp get_value_for_key(:used_up_on, %{id: pack_id}, %{last_used_dates: last_used_dates}) do
|
|
|
|
last_used_date = last_used_dates |> Map.get(pack_id)
|
|
|
|
assigns = %{id: pack_id, last_used_date: last_used_date}
|
2022-11-12 13:52:24 -05:00
|
|
|
|
2023-03-18 21:06:00 -04:00
|
|
|
{last_used_date,
|
2022-11-12 13:52:24 -05:00
|
|
|
~H"""
|
2023-03-18 21:06:00 -04:00
|
|
|
<%= if @last_used_date do %>
|
|
|
|
<.date id={"#{@id}-last-used-date"} date={@last_used_date} />
|
2022-11-19 14:52:01 -05:00
|
|
|
<% else %>
|
|
|
|
<%= gettext("Never used") %>
|
|
|
|
<% end %>
|
2022-11-12 13:52:24 -05:00
|
|
|
"""}
|
|
|
|
end
|
|
|
|
|
2023-03-29 22:54:55 -04:00
|
|
|
defp get_value_for_key(:range, %{staged: staged} = pack, %{range: range}) do
|
|
|
|
assigns = %{range: range, pack: pack}
|
2022-11-12 13:52:24 -05:00
|
|
|
|
|
|
|
{staged,
|
|
|
|
~H"""
|
2023-03-29 22:54:55 -04:00
|
|
|
<%= render_slot(@range, @pack) %>
|
2022-11-12 13:52:24 -05:00
|
|
|
"""}
|
|
|
|
end
|
|
|
|
|
2023-03-19 15:05:09 -04:00
|
|
|
defp get_value_for_key(
|
|
|
|
:remaining,
|
2023-03-29 22:54:55 -04:00
|
|
|
%{id: pack_id},
|
2023-03-19 15:05:09 -04:00
|
|
|
%{percentages_remaining: percentages_remaining}
|
|
|
|
) do
|
2023-03-29 22:54:55 -04:00
|
|
|
percentage = Map.fetch!(percentages_remaining, pack_id)
|
2023-03-19 14:31:53 -04:00
|
|
|
{percentage, gettext("%{percentage}%", percentage: percentage)}
|
2023-03-19 14:11:01 -04:00
|
|
|
end
|
2022-11-12 13:52:24 -05:00
|
|
|
|
2023-03-29 22:54:55 -04:00
|
|
|
defp get_value_for_key(:actions, pack, %{actions: actions}) do
|
|
|
|
assigns = %{actions: actions, pack: pack}
|
2022-11-12 13:52:24 -05:00
|
|
|
|
|
|
|
~H"""
|
2023-03-29 22:54:55 -04:00
|
|
|
<%= render_slot(@actions, @pack) %>
|
2022-11-12 13:52:24 -05:00
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_value_for_key(:container, %{container: nil}, _additional_data), do: {nil, nil}
|
|
|
|
|
|
|
|
defp get_value_for_key(
|
|
|
|
:container,
|
2023-03-29 22:54:55 -04:00
|
|
|
%{container_id: container_id} = pack,
|
2023-03-19 15:05:09 -04:00
|
|
|
%{container: container_block, containers: containers}
|
2022-11-12 13:52:24 -05:00
|
|
|
) do
|
2023-03-19 15:05:09 -04:00
|
|
|
container = %{name: container_name} = Map.fetch!(containers, container_id)
|
|
|
|
|
2023-03-18 21:06:00 -04:00
|
|
|
assigns = %{
|
2023-03-19 15:05:09 -04:00
|
|
|
container: container,
|
|
|
|
container_block: container_block,
|
2023-03-29 22:54:55 -04:00
|
|
|
pack: pack
|
2023-03-18 21:06:00 -04:00
|
|
|
}
|
2022-11-12 13:52:24 -05:00
|
|
|
|
|
|
|
{container_name,
|
|
|
|
~H"""
|
2023-03-29 22:54:55 -04:00
|
|
|
<%= render_slot(@container_block, {@pack, @container}) %>
|
2022-11-12 13:52:24 -05:00
|
|
|
"""}
|
|
|
|
end
|
|
|
|
|
2023-03-19 14:11:01 -04:00
|
|
|
defp get_value_for_key(
|
|
|
|
:original_count,
|
2023-03-29 22:54:55 -04:00
|
|
|
%{id: pack_id},
|
2023-03-19 14:11:01 -04:00
|
|
|
%{original_counts: original_counts}
|
|
|
|
) do
|
2023-03-29 22:54:55 -04:00
|
|
|
Map.fetch!(original_counts, pack_id)
|
2023-03-18 21:06:00 -04:00
|
|
|
end
|
2022-11-12 14:33:11 -05:00
|
|
|
|
|
|
|
defp get_value_for_key(:cpr, %{price_paid: nil}, _additional_data),
|
2023-03-19 14:31:53 -04:00
|
|
|
do: {0, gettext("No cost information")}
|
2022-11-12 14:33:11 -05:00
|
|
|
|
2023-03-29 22:54:55 -04:00
|
|
|
defp get_value_for_key(:cpr, %{id: pack_id}, %{cprs: cprs}) do
|
|
|
|
amount = Map.fetch!(cprs, pack_id)
|
2023-03-19 14:31:53 -04:00
|
|
|
{amount, gettext("$%{amount}", amount: display_currency(amount))}
|
|
|
|
end
|
2022-11-12 14:33:11 -05:00
|
|
|
|
2022-11-12 13:52:24 -05:00
|
|
|
defp get_value_for_key(:count, %{count: count}, _additional_data),
|
2023-03-19 14:31:53 -04:00
|
|
|
do: if(count == 0, do: {0, gettext("Empty")}, else: count)
|
2022-11-12 13:52:24 -05:00
|
|
|
|
2023-03-29 22:54:55 -04:00
|
|
|
defp get_value_for_key(key, pack, _additional_data), do: pack |> Map.get(key)
|
2023-03-18 21:06:00 -04:00
|
|
|
|
|
|
|
@spec display_currency(float()) :: String.t()
|
|
|
|
defp display_currency(float), do: :erlang.float_to_binary(float, decimals: 2)
|
2022-11-12 13:52:24 -05:00
|
|
|
end
|