From 36a0a1c6c812a4600846b0571b8232d3c6f862b4 Mon Sep 17 00:00:00 2001 From: shibao Date: Mon, 7 Nov 2022 00:25:34 -0500 Subject: [PATCH] add used filtering to ammo type show page --- CHANGELOG.md | 1 + lib/cannery/ammo.ex | 41 +++++++++++++++--- lib/cannery_web/components/ammo_group_card.ex | 2 +- lib/cannery_web/live/ammo_type_live/show.ex | 42 +++++++++++++------ .../live/ammo_type_live/show.html.heex | 8 ++++ lib/cannery_web/views/view_helpers.ex | 40 ++++++++++++++++++ priv/gettext/de/LC_MESSAGES/default.po | 11 +++-- priv/gettext/de/LC_MESSAGES/errors.po | 2 +- priv/gettext/de/LC_MESSAGES/prompts.po | 2 +- priv/gettext/default.pot | 11 +++-- priv/gettext/en/LC_MESSAGES/default.po | 11 +++-- priv/gettext/en/LC_MESSAGES/errors.po | 2 +- priv/gettext/en/LC_MESSAGES/prompts.po | 2 +- priv/gettext/errors.pot | 2 +- priv/gettext/es/LC_MESSAGES/default.po | 11 +++-- priv/gettext/es/LC_MESSAGES/errors.po | 2 +- priv/gettext/es/LC_MESSAGES/prompts.po | 2 +- priv/gettext/fr/LC_MESSAGES/default.po | 11 +++-- priv/gettext/fr/LC_MESSAGES/errors.po | 2 +- priv/gettext/fr/LC_MESSAGES/prompts.po | 2 +- priv/gettext/prompts.pot | 2 +- 21 files changed, 165 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91f9d25a..cfb35951 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Make ammo catalog page include ammo count - Make ammo type show page a bit more compact - Make ammo type show page include container names for each ammo +- Make ammo type show page filter used-up ammo - Make container show page a bit more compact - Forgot to add the logo as the favicon whoops diff --git a/lib/cannery/ammo.ex b/lib/cannery/ammo.ex index 23036e7e..fcb142b8 100644 --- a/lib/cannery/ammo.ex +++ b/lib/cannery/ammo.ex @@ -220,7 +220,15 @@ defmodule Cannery.Ammo do """ @spec list_ammo_groups_for_type(AmmoType.t(), User.t()) :: [AmmoGroup.t()] - def list_ammo_groups_for_type(%AmmoType{id: ammo_type_id, user_id: user_id}, %User{id: user_id}) do + @spec list_ammo_groups_for_type(AmmoType.t(), User.t(), include_empty :: boolean()) :: + [AmmoGroup.t()] + def list_ammo_groups_for_type(ammo_type, user, include_empty \\ false) + + def list_ammo_groups_for_type( + %AmmoType{id: ammo_type_id, user_id: user_id}, + %User{id: user_id}, + _include_empty = true + ) do Repo.all( from ag in AmmoGroup, left_join: sg in assoc(ag, :shot_groups), @@ -231,6 +239,22 @@ defmodule Cannery.Ammo do ) end + def list_ammo_groups_for_type( + %AmmoType{id: ammo_type_id, user_id: user_id}, + %User{id: user_id}, + _include_empty = false + ) do + Repo.all( + from ag in AmmoGroup, + left_join: sg in assoc(ag, :shot_groups), + where: ag.ammo_type_id == ^ammo_type_id, + where: ag.user_id == ^user_id, + where: not (ag.count == 0), + preload: [shot_groups: sg], + order_by: ag.id + ) + end + @doc """ Returns the count of ammo_groups for an ammo type. @@ -285,22 +309,27 @@ defmodule Cannery.Ammo do """ @spec list_ammo_groups(User.t()) :: [AmmoGroup.t()] @spec list_ammo_groups(User.t(), include_empty :: boolean()) :: [AmmoGroup.t()] - def list_ammo_groups(%User{id: user_id}, include_empty \\ false) do - if include_empty do + def list_ammo_groups(user, include_empty \\ false) + + def list_ammo_groups(%User{id: user_id}, _include_empty = true) do + Repo.all( from ag in AmmoGroup, left_join: sg in assoc(ag, :shot_groups), where: ag.user_id == ^user_id, preload: [shot_groups: sg], order_by: ag.id - else + ) + end + + def list_ammo_groups(%User{id: user_id}, _include_empty = false) do + Repo.all( from ag in AmmoGroup, left_join: sg in assoc(ag, :shot_groups), where: ag.user_id == ^user_id, where: not (ag.count == 0), preload: [shot_groups: sg], order_by: ag.id - end - |> Repo.all() + ) end @doc """ diff --git a/lib/cannery_web/components/ammo_group_card.ex b/lib/cannery_web/components/ammo_group_card.ex index b993203b..596c9cf8 100644 --- a/lib/cannery_web/components/ammo_group_card.ex +++ b/lib/cannery_web/components/ammo_group_card.ex @@ -32,7 +32,7 @@ defmodule CanneryWeb.Components.AmmoGroupCard do
<%= gettext("Count:") %> - <%= @ammo_group.count %> + <%= if @ammo_group.count == 0, do: "Empty", else: @ammo_group.count %> <%= if @ammo_group.notes do %> diff --git a/lib/cannery_web/live/ammo_type_live/show.ex b/lib/cannery_web/live/ammo_type_live/show.ex index 18e74b31..54ff6ed9 100644 --- a/lib/cannery_web/live/ammo_type_live/show.ex +++ b/lib/cannery_web/live/ammo_type_live/show.ex @@ -9,22 +9,12 @@ defmodule CanneryWeb.AmmoTypeLive.Show do alias CanneryWeb.Endpoint @impl true - def mount(_params, _session, socket), do: {:ok, socket} + def mount(_params, _session, socket), do: {:ok, socket |> assign(show_used: false)} @impl true def handle_params(%{"id" => id}, _params, %{assigns: %{current_user: current_user}} = socket) do ammo_type = Ammo.get_ammo_type!(id, current_user) - - socket = - socket - |> assign( - page_title: page_title(socket.assigns.live_action), - ammo_type: ammo_type, - ammo_groups: ammo_type |> Ammo.list_ammo_groups_for_type(current_user), - avg_cost_per_round: ammo_type |> Ammo.get_average_cost_for_ammo_type!(current_user) - ) - - {:noreply, socket} + {:noreply, socket |> display_ammo_type(ammo_type)} end @impl true @@ -41,6 +31,34 @@ defmodule CanneryWeb.AmmoTypeLive.Show do {:noreply, socket |> put_flash(:info, prompt) |> push_redirect(to: redirect_to)} end + @impl true + def handle_event("toggle_show_used", _, %{assigns: %{show_used: show_used}} = socket) do + {:noreply, socket |> assign(:show_used, !show_used) |> display_ammo_type()} + end + + defp display_ammo_type( + %{ + assigns: %{ + live_action: live_action, + current_user: current_user, + show_used: show_used + } + } = socket, + ammo_type + ) do + socket + |> assign( + page_title: page_title(live_action), + ammo_type: ammo_type, + ammo_groups: ammo_type |> Ammo.list_ammo_groups_for_type(current_user, show_used), + avg_cost_per_round: ammo_type |> Ammo.get_average_cost_for_ammo_type!(current_user) + ) + end + + defp display_ammo_type(%{assigns: %{ammo_type: ammo_type}} = socket) do + socket |> display_ammo_type(ammo_type) + end + defp page_title(:show), do: gettext("Show Ammo type") defp page_title(:edit), do: gettext("Edit Ammo type") end diff --git a/lib/cannery_web/live/ammo_type_live/show.html.heex b/lib/cannery_web/live/ammo_type_live/show.html.heex index 8416a4ee..7597a585 100644 --- a/lib/cannery_web/live/ammo_type_live/show.html.heex +++ b/lib/cannery_web/live/ammo_type_live/show.html.heex @@ -113,6 +113,14 @@
+
+ <.toggle_button action="toggle_show_used" value={@show_used}> + + <%= gettext("Show used") %> + + +
+
<%= if @ammo_groups |> Enum.empty?() do %>

diff --git a/lib/cannery_web/views/view_helpers.ex b/lib/cannery_web/views/view_helpers.ex index fc44d815..90fcec64 100644 --- a/lib/cannery_web/views/view_helpers.ex +++ b/lib/cannery_web/views/view_helpers.ex @@ -5,6 +5,7 @@ defmodule CanneryWeb.ViewHelpers do :view` """ + import Phoenix.LiveView import Phoenix.LiveView.Helpers @id_length 16 @@ -65,4 +66,43 @@ defmodule CanneryWeb.ViewHelpers do if(Application.get_env(:cannery, CanneryWeb.ViewHelpers)[:shibao_mode], do: "q_q", else: "😔") def display_emoji(other_emoji), do: other_emoji + + @doc """ + A toggle button element that can be directed to a liveview or a + live_component's `handle_event/3`. + + ## Examples + + <.toggle_button action="my_liveview_action" value={@some_value}> + Toggle me! + + <.toggle_button action="my_live_component_action" target={@myself} value={@some_value}> + Whatever you want + + """ + def toggle_button(assigns) do + assigns = assigns |> assign_new(:id, fn -> assigns.action end) + + ~H""" + + """ + end end diff --git a/priv/gettext/de/LC_MESSAGES/default.po b/priv/gettext/de/LC_MESSAGES/default.po index 396e2401..2f117d34 100644 --- a/priv/gettext/de/LC_MESSAGES/default.po +++ b/priv/gettext/de/LC_MESSAGES/default.po @@ -178,7 +178,7 @@ msgstr "Munitionsgruppe bearbeiten" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/index.ex:23 -#: lib/cannery_web/live/ammo_type_live/show.ex:45 +#: lib/cannery_web/live/ammo_type_live/show.ex:63 msgid "Edit Ammo type" msgstr "Munitionstyp bearbeiten" @@ -322,7 +322,7 @@ msgid "No Ammo Types" msgstr "Keine Munitionsarten" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/show.html.heex:119 +#: lib/cannery_web/live/ammo_type_live/show.html.heex:127 msgid "No ammo for this type" msgstr "Keine Munition dieser Art" @@ -416,7 +416,7 @@ msgid "Settings" msgstr "Einstellungen" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/show.ex:44 +#: lib/cannery_web/live/ammo_type_live/show.ex:62 msgid "Show Ammo type" msgstr "Zeige Munitionsarten" @@ -945,3 +945,8 @@ msgstr "Summe aller Patronen" #: lib/cannery_web/components/ammo_group_card.ex:61 msgid "Container:" msgstr "Behälter" + +#, elixir-autogen, elixir-format +#: lib/cannery_web/live/ammo_type_live/show.html.heex:119 +msgid "Show used" +msgstr "" diff --git a/priv/gettext/de/LC_MESSAGES/errors.po b/priv/gettext/de/LC_MESSAGES/errors.po index 228d043f..17f319d3 100644 --- a/priv/gettext/de/LC_MESSAGES/errors.po +++ b/priv/gettext/de/LC_MESSAGES/errors.po @@ -188,7 +188,7 @@ msgstr "" "%{multiplier}" #, elixir-autogen, elixir-format -#: lib/cannery/ammo.ex:442 +#: lib/cannery/ammo.ex:479 msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/de/LC_MESSAGES/prompts.po b/priv/gettext/de/LC_MESSAGES/prompts.po index 4b27bc49..bba435d9 100644 --- a/priv/gettext/de/LC_MESSAGES/prompts.po +++ b/priv/gettext/de/LC_MESSAGES/prompts.po @@ -33,7 +33,7 @@ msgstr "%{name} erfolgreich erstellt" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/index.ex:41 -#: lib/cannery_web/live/ammo_type_live/show.ex:38 +#: lib/cannery_web/live/ammo_type_live/show.ex:28 #: lib/cannery_web/live/invite_live/index.ex:53 #: lib/cannery_web/live/invite_live/index.ex:133 #: lib/cannery_web/live/tag_live/index.ex:38 diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index b74a1ac6..655644ad 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -163,7 +163,7 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/index.ex:23 -#: lib/cannery_web/live/ammo_type_live/show.ex:45 +#: lib/cannery_web/live/ammo_type_live/show.ex:63 msgid "Edit Ammo type" msgstr "" @@ -307,7 +307,7 @@ msgid "No Ammo Types" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/show.html.heex:119 +#: lib/cannery_web/live/ammo_type_live/show.html.heex:127 msgid "No ammo for this type" msgstr "" @@ -399,7 +399,7 @@ msgid "Settings" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/show.ex:44 +#: lib/cannery_web/live/ammo_type_live/show.ex:62 msgid "Show Ammo type" msgstr "" @@ -928,3 +928,8 @@ msgstr "" #: lib/cannery_web/components/ammo_group_card.ex:61 msgid "Container:" msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery_web/live/ammo_type_live/show.html.heex:119 +msgid "Show used" +msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index 06f8046d..843fc0e2 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -164,7 +164,7 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/index.ex:23 -#: lib/cannery_web/live/ammo_type_live/show.ex:45 +#: lib/cannery_web/live/ammo_type_live/show.ex:63 msgid "Edit Ammo type" msgstr "" @@ -308,7 +308,7 @@ msgid "No Ammo Types" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/show.html.heex:119 +#: lib/cannery_web/live/ammo_type_live/show.html.heex:127 msgid "No ammo for this type" msgstr "" @@ -400,7 +400,7 @@ msgid "Settings" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/show.ex:44 +#: lib/cannery_web/live/ammo_type_live/show.ex:62 msgid "Show Ammo type" msgstr "" @@ -929,3 +929,8 @@ msgstr "" #: lib/cannery_web/components/ammo_group_card.ex:61 msgid "Container:" msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery_web/live/ammo_type_live/show.html.heex:119 +msgid "Show used" +msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/errors.po b/priv/gettext/en/LC_MESSAGES/errors.po index 13dc2534..fa6e7ed8 100644 --- a/priv/gettext/en/LC_MESSAGES/errors.po +++ b/priv/gettext/en/LC_MESSAGES/errors.po @@ -171,7 +171,7 @@ msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier} msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery/ammo.ex:442 +#: lib/cannery/ammo.ex:479 msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/prompts.po b/priv/gettext/en/LC_MESSAGES/prompts.po index f1792691..49c48add 100644 --- a/priv/gettext/en/LC_MESSAGES/prompts.po +++ b/priv/gettext/en/LC_MESSAGES/prompts.po @@ -21,7 +21,7 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/index.ex:41 -#: lib/cannery_web/live/ammo_type_live/show.ex:38 +#: lib/cannery_web/live/ammo_type_live/show.ex:28 #: lib/cannery_web/live/invite_live/index.ex:53 #: lib/cannery_web/live/invite_live/index.ex:133 #: lib/cannery_web/live/tag_live/index.ex:38 diff --git a/priv/gettext/errors.pot b/priv/gettext/errors.pot index b18e8457..c29767f3 100644 --- a/priv/gettext/errors.pot +++ b/priv/gettext/errors.pot @@ -170,7 +170,7 @@ msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier} msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery/ammo.ex:442 +#: lib/cannery/ammo.ex:479 msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/default.po b/priv/gettext/es/LC_MESSAGES/default.po index c1d8fae7..00ba7a63 100644 --- a/priv/gettext/es/LC_MESSAGES/default.po +++ b/priv/gettext/es/LC_MESSAGES/default.po @@ -178,7 +178,7 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/index.ex:23 -#: lib/cannery_web/live/ammo_type_live/show.ex:45 +#: lib/cannery_web/live/ammo_type_live/show.ex:63 msgid "Edit Ammo type" msgstr "" @@ -322,7 +322,7 @@ msgid "No Ammo Types" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/show.html.heex:119 +#: lib/cannery_web/live/ammo_type_live/show.html.heex:127 msgid "No ammo for this type" msgstr "" @@ -414,7 +414,7 @@ msgid "Settings" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/show.ex:44 +#: lib/cannery_web/live/ammo_type_live/show.ex:62 msgid "Show Ammo type" msgstr "" @@ -943,3 +943,8 @@ msgstr "" #: lib/cannery_web/components/ammo_group_card.ex:61 msgid "Container:" msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery_web/live/ammo_type_live/show.html.heex:119 +msgid "Show used" +msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/errors.po b/priv/gettext/es/LC_MESSAGES/errors.po index 1db6bbba..cfeeba7b 100644 --- a/priv/gettext/es/LC_MESSAGES/errors.po +++ b/priv/gettext/es/LC_MESSAGES/errors.po @@ -186,7 +186,7 @@ msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier} msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery/ammo.ex:442 +#: lib/cannery/ammo.ex:479 msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/prompts.po b/priv/gettext/es/LC_MESSAGES/prompts.po index f180dce3..79efd175 100644 --- a/priv/gettext/es/LC_MESSAGES/prompts.po +++ b/priv/gettext/es/LC_MESSAGES/prompts.po @@ -33,7 +33,7 @@ msgstr "%{name} creado exitosamente" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/index.ex:41 -#: lib/cannery_web/live/ammo_type_live/show.ex:38 +#: lib/cannery_web/live/ammo_type_live/show.ex:28 #: lib/cannery_web/live/invite_live/index.ex:53 #: lib/cannery_web/live/invite_live/index.ex:133 #: lib/cannery_web/live/tag_live/index.ex:38 diff --git a/priv/gettext/fr/LC_MESSAGES/default.po b/priv/gettext/fr/LC_MESSAGES/default.po index 61a49954..1aef84e4 100644 --- a/priv/gettext/fr/LC_MESSAGES/default.po +++ b/priv/gettext/fr/LC_MESSAGES/default.po @@ -178,7 +178,7 @@ msgstr "Éditer le groupe de munition" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/index.ex:23 -#: lib/cannery_web/live/ammo_type_live/show.ex:45 +#: lib/cannery_web/live/ammo_type_live/show.ex:63 msgid "Edit Ammo type" msgstr "Éditer le type de munition" @@ -322,7 +322,7 @@ msgid "No Ammo Types" msgstr "Aucun type de munition" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/show.html.heex:119 +#: lib/cannery_web/live/ammo_type_live/show.html.heex:127 msgid "No ammo for this type" msgstr "Aucune munition pour ce type" @@ -416,7 +416,7 @@ msgid "Settings" msgstr "Paramètres" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/show.ex:44 +#: lib/cannery_web/live/ammo_type_live/show.ex:62 msgid "Show Ammo type" msgstr "Montrer le type de munition" @@ -947,3 +947,8 @@ msgstr "Quantité de cartouches" #: lib/cannery_web/components/ammo_group_card.ex:61 msgid "Container:" msgstr "Conteneur" + +#, elixir-autogen, elixir-format +#: lib/cannery_web/live/ammo_type_live/show.html.heex:119 +msgid "Show used" +msgstr "" diff --git a/priv/gettext/fr/LC_MESSAGES/errors.po b/priv/gettext/fr/LC_MESSAGES/errors.po index 919864a0..208245bb 100644 --- a/priv/gettext/fr/LC_MESSAGES/errors.po +++ b/priv/gettext/fr/LC_MESSAGES/errors.po @@ -187,7 +187,7 @@ msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier} msgstr "Nombre de copies invalide, doit être 1 et %{max}. Été %{multiplier}" #, elixir-autogen, elixir-format -#: lib/cannery/ammo.ex:442 +#: lib/cannery/ammo.ex:479 msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/fr/LC_MESSAGES/prompts.po b/priv/gettext/fr/LC_MESSAGES/prompts.po index 77113b4b..ee461b70 100644 --- a/priv/gettext/fr/LC_MESSAGES/prompts.po +++ b/priv/gettext/fr/LC_MESSAGES/prompts.po @@ -33,7 +33,7 @@ msgstr "%{name} créé· avec succès" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/index.ex:41 -#: lib/cannery_web/live/ammo_type_live/show.ex:38 +#: lib/cannery_web/live/ammo_type_live/show.ex:28 #: lib/cannery_web/live/invite_live/index.ex:53 #: lib/cannery_web/live/invite_live/index.ex:133 #: lib/cannery_web/live/tag_live/index.ex:38 diff --git a/priv/gettext/prompts.pot b/priv/gettext/prompts.pot index 9941fc2c..0dffeec1 100644 --- a/priv/gettext/prompts.pot +++ b/priv/gettext/prompts.pot @@ -20,7 +20,7 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/index.ex:41 -#: lib/cannery_web/live/ammo_type_live/show.ex:38 +#: lib/cannery_web/live/ammo_type_live/show.ex:28 #: lib/cannery_web/live/invite_live/index.ex:53 #: lib/cannery_web/live/invite_live/index.ex:133 #: lib/cannery_web/live/tag_live/index.ex:38