diff --git a/CHANGELOG.md b/CHANGELOG.md index cfb3595..81ac53d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - 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 +- Make container show page filter used-up ammo - Forgot to add the logo as the favicon whoops # v0.5.4 diff --git a/lib/cannery/ammo.ex b/lib/cannery/ammo.ex index fcb142b..8ad8485 100644 --- a/lib/cannery/ammo.ex +++ b/lib/cannery/ammo.ex @@ -5,7 +5,7 @@ defmodule Cannery.Ammo do import CanneryWeb.Gettext import Ecto.Query, warn: false - alias Cannery.{Accounts.User, Containers, Repo} + alias Cannery.{Accounts.User, Containers, Containers.Container, Repo} alias Cannery.ActivityLog.ShotGroup alias Cannery.Ammo.{AmmoGroup, AmmoType} alias Ecto.Changeset @@ -255,6 +255,51 @@ defmodule Cannery.Ammo do ) end + @doc """ + Returns the list of ammo_groups for a user and container. + + ## Examples + + iex> list_ammo_groups_for_container(%AmmoType{id: 123}, %User{id: 123}) + [%AmmoGroup{}, ...] + + """ + @spec list_ammo_groups_for_container(Container.t(), User.t()) :: [AmmoGroup.t()] + @spec list_ammo_groups_for_container(Container.t(), User.t(), include_empty :: boolean()) :: + [AmmoGroup.t()] + def list_ammo_groups_for_container(container, user, include_empty \\ false) + + def list_ammo_groups_for_container( + %Container{id: container_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), + where: ag.container_id == ^container_id, + where: ag.user_id == ^user_id, + preload: [shot_groups: sg], + order_by: ag.id + ) + end + + def list_ammo_groups_for_container( + %Container{id: container_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.container_id == ^container_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. diff --git a/lib/cannery/containers.ex b/lib/cannery/containers.ex index ef4c378..cc647f1 100644 --- a/lib/cannery/containers.ex +++ b/lib/cannery/containers.ex @@ -212,6 +212,7 @@ defmodule Cannery.Containers do container |> Repo.preload(:ammo_groups) |> Map.fetch!(:ammo_groups) + |> Enum.reject(fn %{count: count} -> count == 0 end) |> Enum.count() end diff --git a/lib/cannery_web/live/container_live/show.ex b/lib/cannery_web/live/container_live/show.ex index 2666501..5bde711 100644 --- a/lib/cannery_web/live/container_live/show.ex +++ b/lib/cannery_web/live/container_live/show.ex @@ -5,13 +5,13 @@ defmodule CanneryWeb.ContainerLive.Show do use CanneryWeb, :live_view import CanneryWeb.Components.{AmmoGroupCard, TagCard} - alias Cannery.{Accounts.User, Containers, Containers.Container, Repo, Tags} + alias Cannery.{Ammo, Accounts.User, Containers, Containers.Container, Repo, Tags} alias CanneryWeb.Endpoint alias Ecto.Changeset alias Phoenix.LiveView.Socket @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( @@ -39,7 +39,7 @@ defmodule CanneryWeb.ContainerLive.Show do container_name: container.name ) - socket |> put_flash(:info, prompt) |> render_container(container.id, current_user) + socket |> put_flash(:info, prompt) |> render_container() {:error, error_string} -> socket |> put_flash(:error, error_string) @@ -82,12 +82,23 @@ defmodule CanneryWeb.ContainerLive.Show do {:noreply, socket} end + @impl true + def handle_event("toggle_show_used", _, %{assigns: %{show_used: show_used}} = socket) do + {:noreply, socket |> assign(:show_used, !show_used) |> render_container()} + end + @spec render_container(Socket.t(), Container.id(), User.t()) :: Socket.t() - defp render_container(%{assigns: %{live_action: live_action}} = socket, id, current_user) do + defp render_container( + %{assigns: %{live_action: live_action, show_used: show_used}} = socket, + id, + current_user + ) do %{name: container_name} = container = Containers.get_container!(id, current_user) - |> Repo.preload([:ammo_groups, :tags], force: true) + |> Repo.preload([:tags], force: true) + + ammo_groups = Ammo.list_ammo_groups_for_container(container, current_user, show_used) page_title = case live_action do @@ -96,6 +107,13 @@ defmodule CanneryWeb.ContainerLive.Show do :edit_tags -> gettext("Edit %{name} tags", name: container_name) end - socket |> assign(container: container, page_title: page_title) + socket |> assign(container: container, ammo_groups: ammo_groups, page_title: page_title) + end + + @spec render_container(Socket.t()) :: Socket.t() + defp render_container( + %{assigns: %{container: %{id: container_id}, current_user: current_user}} = socket + ) do + socket |> render_container(container_id, current_user) end end diff --git a/lib/cannery_web/live/container_live/show.html.heex b/lib/cannery_web/live/container_live/show.html.heex index 11f6eea..a12e3fa 100644 --- a/lib/cannery_web/live/container_live/show.html.heex +++ b/lib/cannery_web/live/container_live/show.html.heex @@ -22,10 +22,10 @@ <% end %> - <%= unless @container.ammo_groups |> Enum.empty?() do %> + <%= unless @ammo_groups |> Enum.empty?() do %> <%= gettext("Packs:") %> - <%= @container |> Containers.get_container_ammo_group_count!() %> + <%= Enum.count(@ammo_groups) %> @@ -84,14 +84,22 @@
+
+ <.toggle_button action="toggle_show_used" value={@show_used}> + + <%= gettext("Show used") %> + + +
+
- <%= if @container.ammo_groups |> Enum.empty?() do %> + <%= if @ammo_groups |> Enum.empty?() do %>

<%= gettext("No ammo in this container") %>

<% else %>
- <%= for ammo_group <- @container.ammo_groups do %> + <%= for ammo_group <- @ammo_groups do %> <.ammo_group_card ammo_group={ammo_group} /> <% end %>
diff --git a/priv/gettext/de/LC_MESSAGES/default.po b/priv/gettext/de/LC_MESSAGES/default.po index 2f117d3..ed9724a 100644 --- a/priv/gettext/de/LC_MESSAGES/default.po +++ b/priv/gettext/de/LC_MESSAGES/default.po @@ -710,13 +710,13 @@ msgstr "Lädt..." #, elixir-autogen, elixir-format #: lib/cannery_web/live/container_live/index.ex:27 -#: lib/cannery_web/live/container_live/show.ex:95 +#: lib/cannery_web/live/container_live/show.ex:106 msgid "Edit %{name}" msgstr "%{name} bearbeiten" #, elixir-autogen, elixir-format #: lib/cannery_web/live/container_live/index.ex:46 -#: lib/cannery_web/live/container_live/show.ex:96 +#: lib/cannery_web/live/container_live/show.ex:107 msgid "Edit %{name} tags" msgstr "Editiere %{name} Tags" @@ -727,7 +727,7 @@ msgid "Rounds:" msgstr "Patronen:" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/container_live/show.ex:94 +#: lib/cannery_web/live/container_live/show.ex:105 msgid "Show %{name}" msgstr "Zeige %{name}" @@ -900,7 +900,7 @@ msgid "Move Ammo" msgstr "Munition verschieben" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/container_live/show.html.heex:90 +#: lib/cannery_web/live/container_live/show.html.heex:98 msgid "No ammo in this container" msgstr "Keine Munitionsgruppe in diesem Behälter" @@ -948,5 +948,6 @@ msgstr "Behälter" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/show.html.heex:119 +#: lib/cannery_web/live/container_live/show.html.heex:90 msgid "Show used" msgstr "" diff --git a/priv/gettext/de/LC_MESSAGES/errors.po b/priv/gettext/de/LC_MESSAGES/errors.po index 17f319d..86902ab 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:479 +#: lib/cannery/ammo.ex:524 msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 655644a..6123790 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -693,13 +693,13 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/container_live/index.ex:27 -#: lib/cannery_web/live/container_live/show.ex:95 +#: lib/cannery_web/live/container_live/show.ex:106 msgid "Edit %{name}" msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/container_live/index.ex:46 -#: lib/cannery_web/live/container_live/show.ex:96 +#: lib/cannery_web/live/container_live/show.ex:107 msgid "Edit %{name} tags" msgstr "" @@ -710,7 +710,7 @@ msgid "Rounds:" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/container_live/show.ex:94 +#: lib/cannery_web/live/container_live/show.ex:105 msgid "Show %{name}" msgstr "" @@ -883,7 +883,7 @@ msgid "Move Ammo" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/container_live/show.html.heex:90 +#: lib/cannery_web/live/container_live/show.html.heex:98 msgid "No ammo in this container" msgstr "" @@ -931,5 +931,6 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/show.html.heex:119 +#: lib/cannery_web/live/container_live/show.html.heex:90 msgid "Show used" msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index 843fc0e..0783403 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -694,13 +694,13 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/container_live/index.ex:27 -#: lib/cannery_web/live/container_live/show.ex:95 +#: lib/cannery_web/live/container_live/show.ex:106 msgid "Edit %{name}" msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/container_live/index.ex:46 -#: lib/cannery_web/live/container_live/show.ex:96 +#: lib/cannery_web/live/container_live/show.ex:107 msgid "Edit %{name} tags" msgstr "" @@ -711,7 +711,7 @@ msgid "Rounds:" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/container_live/show.ex:94 +#: lib/cannery_web/live/container_live/show.ex:105 msgid "Show %{name}" msgstr "" @@ -884,7 +884,7 @@ msgid "Move Ammo" msgstr "" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/container_live/show.html.heex:90 +#: lib/cannery_web/live/container_live/show.html.heex:98 msgid "No ammo in this container" msgstr "" @@ -932,5 +932,6 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/show.html.heex:119 +#: lib/cannery_web/live/container_live/show.html.heex:90 msgid "Show used" msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/errors.po b/priv/gettext/en/LC_MESSAGES/errors.po index fa6e7ed..1cbb6db 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:479 +#: lib/cannery/ammo.ex:524 msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/errors.pot b/priv/gettext/errors.pot index c29767f..33ff775 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:479 +#: lib/cannery/ammo.ex:524 msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/default.po b/priv/gettext/es/LC_MESSAGES/default.po index 00ba7a6..dc257d5 100644 --- a/priv/gettext/es/LC_MESSAGES/default.po +++ b/priv/gettext/es/LC_MESSAGES/default.po @@ -708,13 +708,13 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/container_live/index.ex:27 -#: lib/cannery_web/live/container_live/show.ex:95 +#: lib/cannery_web/live/container_live/show.ex:106 msgid "Edit %{name}" msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/container_live/index.ex:46 -#: lib/cannery_web/live/container_live/show.ex:96 +#: lib/cannery_web/live/container_live/show.ex:107 msgid "Edit %{name} tags" msgstr "" @@ -725,7 +725,7 @@ msgid "Rounds:" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/container_live/show.ex:94 +#: lib/cannery_web/live/container_live/show.ex:105 msgid "Show %{name}" msgstr "" @@ -898,7 +898,7 @@ msgid "Move Ammo" msgstr "" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/container_live/show.html.heex:90 +#: lib/cannery_web/live/container_live/show.html.heex:98 msgid "No ammo in this container" msgstr "" @@ -946,5 +946,6 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/show.html.heex:119 +#: lib/cannery_web/live/container_live/show.html.heex:90 msgid "Show used" msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/errors.po b/priv/gettext/es/LC_MESSAGES/errors.po index cfeeba7..b6ba6f3 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:479 +#: lib/cannery/ammo.ex:524 msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/fr/LC_MESSAGES/default.po b/priv/gettext/fr/LC_MESSAGES/default.po index 1aef84e..2e6812c 100644 --- a/priv/gettext/fr/LC_MESSAGES/default.po +++ b/priv/gettext/fr/LC_MESSAGES/default.po @@ -712,13 +712,13 @@ msgstr "Chargement en cours…" #, elixir-autogen, elixir-format #: lib/cannery_web/live/container_live/index.ex:27 -#: lib/cannery_web/live/container_live/show.ex:95 +#: lib/cannery_web/live/container_live/show.ex:106 msgid "Edit %{name}" msgstr "Éditer %{name}" #, elixir-autogen, elixir-format #: lib/cannery_web/live/container_live/index.ex:46 -#: lib/cannery_web/live/container_live/show.ex:96 +#: lib/cannery_web/live/container_live/show.ex:107 msgid "Edit %{name} tags" msgstr "Éditer les tags de %{name}" @@ -729,7 +729,7 @@ msgid "Rounds:" msgstr "Cartouches :" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/container_live/show.ex:94 +#: lib/cannery_web/live/container_live/show.ex:105 msgid "Show %{name}" msgstr "Montrer %{name}" @@ -902,7 +902,7 @@ msgid "Move Ammo" msgstr "Déplacer munition" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/container_live/show.html.heex:90 +#: lib/cannery_web/live/container_live/show.html.heex:98 msgid "No ammo in this container" msgstr "Aucun groupe de munition pour ce conteneur" @@ -950,5 +950,6 @@ msgstr "Conteneur" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/show.html.heex:119 +#: lib/cannery_web/live/container_live/show.html.heex:90 msgid "Show used" msgstr "" diff --git a/priv/gettext/fr/LC_MESSAGES/errors.po b/priv/gettext/fr/LC_MESSAGES/errors.po index 208245b..7106c37 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:479 +#: lib/cannery/ammo.ex:524 msgid "Invalid multiplier" msgstr ""