cannery/lib/cannery_web/live/ammo_type_live/show.ex

65 lines
2.0 KiB
Elixir
Raw Normal View History

2021-09-02 23:31:14 -04:00
defmodule CanneryWeb.AmmoTypeLive.Show do
2022-01-22 21:40:29 -05:00
@moduledoc """
Liveview for showing and editing an Cannery.Ammo.AmmoType
"""
2021-09-02 23:31:14 -04:00
use CanneryWeb, :live_view
2022-02-11 22:47:33 -05:00
import CanneryWeb.Components.AmmoGroupCard
2022-02-08 22:12:22 -05:00
alias Cannery.Ammo
2022-02-14 01:26:51 -05:00
alias CanneryWeb.Endpoint
2021-09-02 23:31:14 -04:00
@impl true
def mount(_params, _session, socket), do: {:ok, socket |> assign(show_used: false)}
2021-09-02 23:31:14 -04:00
@impl true
2022-03-28 23:05:12 -04:00
def handle_params(%{"id" => id}, _params, %{assigns: %{current_user: current_user}} = socket) do
ammo_type = Ammo.get_ammo_type!(id, current_user)
{:noreply, socket |> display_ammo_type(ammo_type)}
2021-09-02 23:31:14 -04:00
end
2022-01-31 23:20:57 -05:00
@impl true
def handle_event(
"delete",
2022-03-28 23:05:12 -04:00
_params,
%{assigns: %{ammo_type: ammo_type, current_user: current_user}} = socket
) do
%{name: ammo_type_name} = ammo_type |> Ammo.delete_ammo_type!(current_user)
prompt = dgettext("prompts", "%{name} deleted succesfully", name: ammo_type_name)
redirect_to = Routes.ammo_type_index_path(socket, :index)
{:noreply, socket |> put_flash(:info, prompt) |> push_redirect(to: redirect_to)}
2022-01-31 23:20:57 -05:00
end
@impl true
2022-11-07 01:36:28 -05:00
def handle_event("toggle_show_used", _params, %{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
2022-02-09 00:39:27 -05:00
defp page_title(:show), do: gettext("Show Ammo type")
defp page_title(:edit), do: gettext("Edit Ammo type")
2021-09-02 23:31:14 -04:00
end