add used filtering to ammo type show page

This commit is contained in:
2022-11-07 00:25:34 -05:00
parent 2e0bb861f1
commit 36a0a1c6c8
21 changed files with 165 additions and 44 deletions

View File

@ -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

View File

@ -113,6 +113,14 @@
<hr class="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 @ammo_groups |> Enum.empty?() do %>
<h2 class="mx-8 my-4 title text-lg text-primary-600">