add used filtering to container show page
This commit is contained in:
		| @@ -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