From ca81924ebe8e9847722ae2277b63b9981b8d0b14 Mon Sep 17 00:00:00 2001 From: shibao Date: Sun, 19 Mar 2023 13:42:59 -0400 Subject: [PATCH] fix ammo type table not displaying correct information --- CHANGELOG.md | 1 + .../components/ammo_type_table_component.ex | 87 +++++++++++-------- priv/gettext/de/LC_MESSAGES/default.po | 4 +- priv/gettext/default.pot | 4 +- priv/gettext/en/LC_MESSAGES/default.po | 4 +- priv/gettext/es/LC_MESSAGES/default.po | 4 +- priv/gettext/fr/LC_MESSAGES/default.po | 4 +- priv/gettext/ga/LC_MESSAGES/default.po | 4 +- test/cannery_web/live/ammo_type_live_test.exs | 22 ++--- 9 files changed, 73 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ef938b7..4f3ae2fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # v0.8.6 - Fix duplicate entries showing up - Show ammo groups under a type in a table by default +- Only show historical ammo type information when displaying "Show used" in table # v0.8.5 - Add link in readme to github mirror diff --git a/lib/cannery_web/components/ammo_type_table_component.ex b/lib/cannery_web/components/ammo_type_table_component.ex index f362e6e4..7b2a93fb 100644 --- a/lib/cannery_web/components/ammo_type_table_component.ex +++ b/lib/cannery_web/components/ammo_type_table_component.ex @@ -103,13 +103,13 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do [ %{ label: gettext("Used packs"), - key: :used_packs_count, - type: :used_packs_count + key: :used_pack_count, + type: :used_pack_count }, %{ label: gettext("Total ever packs"), - key: :historical_packs_count, - type: :historical_packs_count + key: :historical_pack_count, + type: :historical_pack_count } ] else @@ -122,23 +122,21 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do ]) round_counts = ammo_types |> Ammo.get_round_count_for_ammo_types(current_user) - - used_counts = - show_used && ammo_types |> ActivityLog.get_used_count_for_ammo_types(current_user) - - historical_round_counts = - show_used && ammo_types |> Ammo.get_historical_count_for_ammo_types(current_user) - packs_count = ammo_types |> Ammo.get_ammo_groups_count_for_types(current_user) - - historical_packs_count = - show_used && ammo_types |> Ammo.get_ammo_groups_count_for_types(current_user, true) - - used_packs_count = - show_used && ammo_types |> Ammo.get_used_ammo_groups_count_for_types(current_user) - average_costs = ammo_types |> Ammo.get_average_cost_for_ammo_types(current_user) + [used_counts, historical_round_counts, historical_pack_counts, used_pack_counts] = + if show_used do + [ + ammo_types |> ActivityLog.get_used_count_for_ammo_types(current_user), + ammo_types |> Ammo.get_historical_count_for_ammo_types(current_user), + ammo_types |> Ammo.get_ammo_groups_count_for_types(current_user, true), + ammo_types |> Ammo.get_used_ammo_groups_count_for_types(current_user) + ] + else + [nil, nil, nil, nil] + end + extra_data = %{ actions: actions, current_user: current_user, @@ -146,8 +144,8 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do round_counts: round_counts, historical_round_counts: historical_round_counts, packs_count: packs_count, - used_packs_count: used_packs_count, - historical_packs_count: historical_packs_count, + used_pack_counts: used_pack_counts, + historical_pack_counts: historical_pack_counts, average_costs: average_costs } @@ -192,33 +190,46 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do _key, %{id: ammo_type_id}, %{historical_round_counts: historical_round_counts} - ), - do: Map.get(historical_round_counts, ammo_type_id) - - defp get_ammo_type_value(:used_round_count, _key, %{id: ammo_type_id}, %{ - used_counts: used_counts - }), - do: Map.get(used_counts, ammo_type_id) + ) do + Map.get(historical_round_counts, ammo_type_id) + end defp get_ammo_type_value( - :historical_packs_count, + :used_round_count, _key, %{id: ammo_type_id}, - %{historical_packs_count: historical_packs_count} - ), - do: Map.get(historical_packs_count, ammo_type_id) + %{used_counts: used_counts} + ) do + Map.get(used_counts, ammo_type_id) + end - defp get_ammo_type_value(:used_packs_count, _key, %{id: ammo_type_id}, %{ - used_packs_count: used_packs_count - }), - do: Map.get(used_packs_count, ammo_type_id) + defp get_ammo_type_value( + :historical_pack_count, + _key, + %{id: ammo_type_id}, + %{historical_pack_counts: historical_pack_counts} + ) do + Map.get(historical_pack_counts, ammo_type_id) + end + + defp get_ammo_type_value( + :used_pack_count, + _key, + %{id: ammo_type_id}, + %{used_pack_counts: used_pack_counts} + ) do + Map.get(used_pack_counts, ammo_type_id, 0) + end defp get_ammo_type_value(:ammo_count, _key, %{id: ammo_type_id}, %{packs_count: packs_count}), do: Map.get(packs_count, ammo_type_id) - defp get_ammo_type_value(:avg_price_paid, _key, %{id: ammo_type_id}, %{ - average_costs: average_costs - }) do + defp get_ammo_type_value( + :avg_price_paid, + _key, + %{id: ammo_type_id}, + %{average_costs: average_costs} + ) do case Map.get(average_costs, ammo_type_id) do nil -> gettext("No cost information") count -> gettext("$%{amount}", amount: display_currency(count)) diff --git a/priv/gettext/de/LC_MESSAGES/default.po b/priv/gettext/de/LC_MESSAGES/default.po index a78c914f..d9f38d53 100644 --- a/priv/gettext/de/LC_MESSAGES/default.po +++ b/priv/gettext/de/LC_MESSAGES/default.po @@ -528,7 +528,7 @@ msgstr "Schießkladde" #: lib/cannery_web/components/ammo_group_table_component.ex:154 #: lib/cannery_web/components/ammo_group_table_component.ex:230 -#: lib/cannery_web/components/ammo_type_table_component.ex:224 +#: lib/cannery_web/components/ammo_type_table_component.ex:235 #: lib/cannery_web/components/core_components/ammo_group_card.html.heex:45 #: lib/cannery_web/components/core_components/ammo_group_card.html.heex:50 #: lib/cannery_web/live/ammo_group_live/show.html.heex:37 @@ -622,7 +622,7 @@ msgid "Rounds:" msgstr "Patronen:" #: lib/cannery_web/components/ammo_group_table_component.ex:227 -#: lib/cannery_web/components/ammo_type_table_component.ex:223 +#: lib/cannery_web/components/ammo_type_table_component.ex:234 #: lib/cannery_web/live/ammo_type_live/show.html.heex:139 #, elixir-autogen, elixir-format msgid "No cost information" diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index f1c72605..c944c844 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -522,7 +522,7 @@ msgstr "" #: lib/cannery_web/components/ammo_group_table_component.ex:154 #: lib/cannery_web/components/ammo_group_table_component.ex:230 -#: lib/cannery_web/components/ammo_type_table_component.ex:224 +#: lib/cannery_web/components/ammo_type_table_component.ex:235 #: lib/cannery_web/components/core_components/ammo_group_card.html.heex:45 #: lib/cannery_web/components/core_components/ammo_group_card.html.heex:50 #: lib/cannery_web/live/ammo_group_live/show.html.heex:37 @@ -616,7 +616,7 @@ msgid "Rounds:" msgstr "" #: lib/cannery_web/components/ammo_group_table_component.ex:227 -#: lib/cannery_web/components/ammo_type_table_component.ex:223 +#: lib/cannery_web/components/ammo_type_table_component.ex:234 #: lib/cannery_web/live/ammo_type_live/show.html.heex:139 #, elixir-autogen, elixir-format msgid "No cost information" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index 50e7ea78..6cbd8396 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -522,7 +522,7 @@ msgstr "" #: lib/cannery_web/components/ammo_group_table_component.ex:154 #: lib/cannery_web/components/ammo_group_table_component.ex:230 -#: lib/cannery_web/components/ammo_type_table_component.ex:224 +#: lib/cannery_web/components/ammo_type_table_component.ex:235 #: lib/cannery_web/components/core_components/ammo_group_card.html.heex:45 #: lib/cannery_web/components/core_components/ammo_group_card.html.heex:50 #: lib/cannery_web/live/ammo_group_live/show.html.heex:37 @@ -616,7 +616,7 @@ msgid "Rounds:" msgstr "" #: lib/cannery_web/components/ammo_group_table_component.ex:227 -#: lib/cannery_web/components/ammo_type_table_component.ex:223 +#: lib/cannery_web/components/ammo_type_table_component.ex:234 #: lib/cannery_web/live/ammo_type_live/show.html.heex:139 #, elixir-autogen, elixir-format msgid "No cost information" diff --git a/priv/gettext/es/LC_MESSAGES/default.po b/priv/gettext/es/LC_MESSAGES/default.po index 78a89902..55130406 100644 --- a/priv/gettext/es/LC_MESSAGES/default.po +++ b/priv/gettext/es/LC_MESSAGES/default.po @@ -529,7 +529,7 @@ msgstr "Registro de tiros" #: lib/cannery_web/components/ammo_group_table_component.ex:154 #: lib/cannery_web/components/ammo_group_table_component.ex:230 -#: lib/cannery_web/components/ammo_type_table_component.ex:224 +#: lib/cannery_web/components/ammo_type_table_component.ex:235 #: lib/cannery_web/components/core_components/ammo_group_card.html.heex:45 #: lib/cannery_web/components/core_components/ammo_group_card.html.heex:50 #: lib/cannery_web/live/ammo_group_live/show.html.heex:37 @@ -623,7 +623,7 @@ msgid "Rounds:" msgstr "Balas:" #: lib/cannery_web/components/ammo_group_table_component.ex:227 -#: lib/cannery_web/components/ammo_type_table_component.ex:223 +#: lib/cannery_web/components/ammo_type_table_component.ex:234 #: lib/cannery_web/live/ammo_type_live/show.html.heex:139 #, elixir-autogen, elixir-format msgid "No cost information" diff --git a/priv/gettext/fr/LC_MESSAGES/default.po b/priv/gettext/fr/LC_MESSAGES/default.po index 3b181a9a..e7b3e2e5 100644 --- a/priv/gettext/fr/LC_MESSAGES/default.po +++ b/priv/gettext/fr/LC_MESSAGES/default.po @@ -530,7 +530,7 @@ msgstr "Évènements de tir" #: lib/cannery_web/components/ammo_group_table_component.ex:154 #: lib/cannery_web/components/ammo_group_table_component.ex:230 -#: lib/cannery_web/components/ammo_type_table_component.ex:224 +#: lib/cannery_web/components/ammo_type_table_component.ex:235 #: lib/cannery_web/components/core_components/ammo_group_card.html.heex:45 #: lib/cannery_web/components/core_components/ammo_group_card.html.heex:50 #: lib/cannery_web/live/ammo_group_live/show.html.heex:37 @@ -624,7 +624,7 @@ msgid "Rounds:" msgstr "Cartouches :" #: lib/cannery_web/components/ammo_group_table_component.ex:227 -#: lib/cannery_web/components/ammo_type_table_component.ex:223 +#: lib/cannery_web/components/ammo_type_table_component.ex:234 #: lib/cannery_web/live/ammo_type_live/show.html.heex:139 #, elixir-autogen, elixir-format msgid "No cost information" diff --git a/priv/gettext/ga/LC_MESSAGES/default.po b/priv/gettext/ga/LC_MESSAGES/default.po index b9ab00db..31e4d4bd 100644 --- a/priv/gettext/ga/LC_MESSAGES/default.po +++ b/priv/gettext/ga/LC_MESSAGES/default.po @@ -524,7 +524,7 @@ msgstr "" #: lib/cannery_web/components/ammo_group_table_component.ex:154 #: lib/cannery_web/components/ammo_group_table_component.ex:230 -#: lib/cannery_web/components/ammo_type_table_component.ex:224 +#: lib/cannery_web/components/ammo_type_table_component.ex:235 #: lib/cannery_web/components/core_components/ammo_group_card.html.heex:45 #: lib/cannery_web/components/core_components/ammo_group_card.html.heex:50 #: lib/cannery_web/live/ammo_group_live/show.html.heex:37 @@ -618,7 +618,7 @@ msgid "Rounds:" msgstr "" #: lib/cannery_web/components/ammo_group_table_component.ex:227 -#: lib/cannery_web/components/ammo_type_table_component.ex:223 +#: lib/cannery_web/components/ammo_type_table_component.ex:234 #: lib/cannery_web/live/ammo_type_live/show.html.heex:139 #, elixir-autogen, elixir-format msgid "No cost information" diff --git a/test/cannery_web/live/ammo_type_live_test.exs b/test/cannery_web/live/ammo_type_live_test.exs index 45027908..6e88df26 100644 --- a/test/cannery_web/live/ammo_type_live_test.exs +++ b/test/cannery_web/live/ammo_type_live_test.exs @@ -230,9 +230,9 @@ defmodule CanneryWeb.AmmoTypeLiveTest do assert html =~ gettext("Used packs") assert html =~ gettext("Total ever packs") - assert html =~ "20" - assert html =~ "0" - assert html =~ "1" + assert html =~ "\n20\n" + assert html =~ "\n0\n" + assert html =~ "\n1\n" shot_group_fixture(%{"count" => 5}, current_user, ammo_group) @@ -243,8 +243,8 @@ defmodule CanneryWeb.AmmoTypeLiveTest do |> element(~s/input[type="checkbox"][aria-labelledby="toggle_show_used-label"}]/) |> render_click() - assert html =~ "15" - assert html =~ "5" + assert html =~ "\n15\n" + assert html =~ "\n5\n" end end @@ -297,7 +297,7 @@ defmodule CanneryWeb.AmmoTypeLiveTest do {:ok, _show_live, html} = live(conn, Routes.ammo_type_show_path(conn, :show, ammo_type)) assert html =~ ammo_type_name - assert html =~ "some ammo group" + assert html =~ "\n20\n" assert html =~ container_name end @@ -310,7 +310,7 @@ defmodule CanneryWeb.AmmoTypeLiveTest do |> element(~s/input[type="checkbox"][aria-labelledby="toggle_table-label"}]/) |> render_click() - assert html =~ "some ammo group" + assert html =~ "\n20\n" assert html =~ container_name end end @@ -323,14 +323,14 @@ defmodule CanneryWeb.AmmoTypeLiveTest do {:ok, show_live, html} = live(conn, Routes.ammo_type_show_path(conn, :show, ammo_type)) assert html =~ dgettext("actions", "Show used") - refute html =~ "some ammo group" + refute html =~ "\n20\n" html = show_live |> element(~s/input[type="checkbox"][aria-labelledby="toggle_show_used-label"}]/) |> render_click() - assert html =~ "some ammo group" + assert html =~ "\n20\n" assert html =~ "Empty" assert html =~ container_name end @@ -345,14 +345,14 @@ defmodule CanneryWeb.AmmoTypeLiveTest do |> render_click() assert html =~ dgettext("actions", "Show used") - refute html =~ "some ammo group" + refute html =~ "\n20\n" html = show_live |> element(~s/input[type="checkbox"][aria-labelledby="toggle_show_used-label"}]/) |> render_click() - assert html =~ "some ammo group" + assert html =~ "\n20\n" assert html =~ "Empty" assert html =~ container_name end