forked from shibao/cannery
		
	add used filtering to container show page
This commit is contained in:
		@@ -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.
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -22,10 +22,10 @@
 | 
			
		||||
    </span>
 | 
			
		||||
  <% end %>
 | 
			
		||||
 | 
			
		||||
  <%= unless @container.ammo_groups |> Enum.empty?() do %>
 | 
			
		||||
  <%= unless @ammo_groups |> Enum.empty?() do %>
 | 
			
		||||
    <span class="rounded-lg title text-lg">
 | 
			
		||||
      <%= gettext("Packs:") %>
 | 
			
		||||
      <%= @container |> Containers.get_container_ammo_group_count!() %>
 | 
			
		||||
      <%= Enum.count(@ammo_groups) %>
 | 
			
		||||
    </span>
 | 
			
		||||
 | 
			
		||||
    <span class="rounded-lg title text-lg">
 | 
			
		||||
@@ -84,14 +84,22 @@
 | 
			
		||||
 | 
			
		||||
  <hr class="mb-4 hr" />
 | 
			
		||||
 | 
			
		||||
  <div class="flex flex-col justify-center items-center">
 | 
			
		||||
    <.toggle_button action="toggle_show_used" value={@show_used}>
 | 
			
		||||
      <span class="title text-lg text-primary-600">
 | 
			
		||||
        <%= gettext("Show used") %>
 | 
			
		||||
      </span>
 | 
			
		||||
    </.toggle_button>
 | 
			
		||||
  </div>
 | 
			
		||||
 | 
			
		||||
  <div>
 | 
			
		||||
    <%= if @container.ammo_groups |> Enum.empty?() do %>
 | 
			
		||||
    <%= if @ammo_groups |> Enum.empty?() do %>
 | 
			
		||||
      <h2 class="mx-8 my-4 title text-lg text-primary-600">
 | 
			
		||||
        <%= gettext("No ammo in this container") %>
 | 
			
		||||
      </h2>
 | 
			
		||||
    <% else %>
 | 
			
		||||
      <div class="flex flex-wrap justify-center items-center">
 | 
			
		||||
        <%= for ammo_group <- @container.ammo_groups do %>
 | 
			
		||||
        <%= for ammo_group <- @ammo_groups do %>
 | 
			
		||||
          <.ammo_group_card ammo_group={ammo_group} />
 | 
			
		||||
        <% end %>
 | 
			
		||||
      </div>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user