display used-up date on used-up ammo
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@ -73,7 +73,7 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("toggle_show_used", _, %{assigns: %{show_used: show_used}} = socket) do
|
||||
def handle_event("toggle_show_used", _params, %{assigns: %{show_used: show_used}} = socket) do
|
||||
{:noreply, socket |> assign(:show_used, !show_used) |> display_ammo_groups()}
|
||||
end
|
||||
|
||||
@ -87,16 +87,24 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|
||||
containers_count = Containers.get_containers_count!(current_user)
|
||||
|
||||
columns = [
|
||||
%{label: gettext("Ammo type"), key: "ammo_type"},
|
||||
%{label: gettext("Count"), key: "count"},
|
||||
%{label: gettext("Price paid"), key: "price_paid"},
|
||||
%{label: gettext("% left"), key: "remaining"},
|
||||
%{label: gettext("Range"), key: "range"},
|
||||
%{label: gettext("Container"), key: "container"},
|
||||
%{label: gettext("Added on"), key: "added_on"},
|
||||
%{label: nil, key: "actions", sortable: false}
|
||||
%{label: gettext("Ammo type"), key: :ammo_type},
|
||||
%{label: gettext("Count"), key: :count},
|
||||
%{label: gettext("Price paid"), key: :price_paid},
|
||||
%{label: gettext("% left"), key: :remaining},
|
||||
%{label: gettext("Range"), key: :range},
|
||||
%{label: gettext("Container"), key: :container},
|
||||
%{label: gettext("Added on"), key: :added_on}
|
||||
]
|
||||
|
||||
columns =
|
||||
if show_used do
|
||||
columns ++ [%{label: gettext("Used up on"), key: :used_up_on}]
|
||||
else
|
||||
columns
|
||||
end
|
||||
|
||||
columns = columns ++ [%{label: nil, key: :actions, sortable: false}]
|
||||
|
||||
rows =
|
||||
ammo_groups
|
||||
|> Enum.map(fn ammo_group -> ammo_group |> get_row_data_for_ammo_group(columns) end)
|
||||
@ -119,8 +127,8 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|
||||
|> Enum.into(%{}, fn %{key: key} -> {key, get_value_for_key(key, ammo_group)} end)
|
||||
end
|
||||
|
||||
@spec get_value_for_key(String.t(), AmmoGroup.t()) :: any()
|
||||
defp get_value_for_key("ammo_type", %{ammo_type: ammo_type}) do
|
||||
@spec get_value_for_key(atom(), AmmoGroup.t()) :: any()
|
||||
defp get_value_for_key(:ammo_type, %{ammo_type: ammo_type}) do
|
||||
{ammo_type.name,
|
||||
live_patch(ammo_type.name,
|
||||
to: Routes.ammo_type_show_path(Endpoint, :show, ammo_type),
|
||||
@ -128,12 +136,12 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|
||||
)}
|
||||
end
|
||||
|
||||
defp get_value_for_key("price_paid", %{price_paid: nil}), do: {"a", nil}
|
||||
defp get_value_for_key(:price_paid, %{price_paid: nil}), do: {"a", nil}
|
||||
|
||||
defp get_value_for_key("price_paid", %{price_paid: price_paid}),
|
||||
defp get_value_for_key(:price_paid, %{price_paid: price_paid}),
|
||||
do: gettext("$%{amount}", amount: price_paid |> :erlang.float_to_binary(decimals: 2))
|
||||
|
||||
defp get_value_for_key("added_on", %{inserted_at: inserted_at}) do
|
||||
defp get_value_for_key(:added_on, %{inserted_at: inserted_at}) do
|
||||
assigns = %{inserted_at: inserted_at}
|
||||
|
||||
{inserted_at,
|
||||
@ -142,7 +150,22 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|
||||
"""}
|
||||
end
|
||||
|
||||
defp get_value_for_key("range", %{staged: staged} = ammo_group) do
|
||||
defp get_value_for_key(:used_up_on, ammo_group) do
|
||||
last_shot_group_date =
|
||||
case ammo_group |> Ammo.get_last_used_shot_group() do
|
||||
%{date: last_shot_group_date} -> last_shot_group_date
|
||||
_no_shot_groups -> nil
|
||||
end
|
||||
|
||||
assigns = %{last_shot_group_date: last_shot_group_date}
|
||||
|
||||
{last_shot_group_date,
|
||||
~H"""
|
||||
<%= @last_shot_group_date |> display_date() %>
|
||||
"""}
|
||||
end
|
||||
|
||||
defp get_value_for_key(:range, %{staged: staged} = ammo_group) do
|
||||
assigns = %{ammo_group: ammo_group}
|
||||
|
||||
{staged,
|
||||
@ -165,10 +188,10 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|
||||
"""}
|
||||
end
|
||||
|
||||
defp get_value_for_key("remaining", ammo_group),
|
||||
defp get_value_for_key(:remaining, ammo_group),
|
||||
do: "#{ammo_group |> Ammo.get_percentage_remaining()}%"
|
||||
|
||||
defp get_value_for_key("actions", ammo_group) do
|
||||
defp get_value_for_key(:actions, ammo_group) do
|
||||
assigns = %{ammo_group: ammo_group}
|
||||
|
||||
~H"""
|
||||
@ -199,9 +222,9 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|
||||
"""
|
||||
end
|
||||
|
||||
defp get_value_for_key("container", %{container: nil}), do: {nil, nil}
|
||||
defp get_value_for_key(:container, %{container: nil}), do: {nil, nil}
|
||||
|
||||
defp get_value_for_key("container", %{container: %{name: container_name}} = ammo_group) do
|
||||
defp get_value_for_key(:container, %{container: %{name: container_name}} = ammo_group) do
|
||||
assigns = %{ammo_group: ammo_group}
|
||||
|
||||
{container_name,
|
||||
@ -222,6 +245,5 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|
||||
"""}
|
||||
end
|
||||
|
||||
defp get_value_for_key(key, ammo_group),
|
||||
do: ammo_group |> Map.get(key |> String.to_existing_atom())
|
||||
defp get_value_for_key(key, ammo_group), do: ammo_group |> Map.get(key)
|
||||
end
|
||||
|
@ -84,10 +84,10 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
|
||||
ammo_group = ammo_group |> Repo.preload([:container, :ammo_type, :shot_groups], force: true)
|
||||
|
||||
columns = [
|
||||
%{label: gettext("Rounds shot"), key: "count"},
|
||||
%{label: gettext("Notes"), key: "notes"},
|
||||
%{label: gettext("Date"), key: "date"},
|
||||
%{label: nil, key: "actions", sortable: false}
|
||||
%{label: gettext("Rounds shot"), key: :count},
|
||||
%{label: gettext("Notes"), key: :notes},
|
||||
%{label: gettext("Date"), key: :date},
|
||||
%{label: nil, key: :actions, sortable: false}
|
||||
]
|
||||
|
||||
rows =
|
||||
@ -110,10 +110,10 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
|
||||
|> Enum.into(%{}, fn %{key: key} ->
|
||||
value =
|
||||
case key do
|
||||
"date" ->
|
||||
:date ->
|
||||
{date, date |> display_date()}
|
||||
|
||||
"actions" ->
|
||||
:actions ->
|
||||
~H"""
|
||||
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
|
||||
<%= live_patch to: Routes.ammo_group_show_path(Endpoint, :edit_shot_group, @ammo_group, shot_group),
|
||||
@ -136,7 +136,7 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
|
||||
"""
|
||||
|
||||
key ->
|
||||
shot_group |> Map.get(key |> String.to_existing_atom())
|
||||
shot_group |> Map.get(key)
|
||||
end
|
||||
|
||||
{key, value}
|
||||
|
Reference in New Issue
Block a user