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

41 lines
1.1 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-05 01:59:40 -05:00
import CanneryWeb.AmmoGroupLive.AmmoGroupCard
2022-02-08 22:12:22 -05:00
alias Cannery.Ammo
2021-09-02 23:31:14 -04:00
@impl true
2021-09-02 23:31:16 -04:00
def mount(_params, session, socket) do
{:ok, socket |> assign_defaults(session)}
2021-09-02 23:31:14 -04:00
end
@impl true
2022-02-08 00:21:22 -05:00
def handle_params(%{"id" => id}, _, %{assigns: %{current_user: current_user}} = socket) do
ammo_type = Ammo.get_ammo_type!(id)
ammo_groups = ammo_type |> Ammo.list_ammo_groups_for_type(current_user)
2022-02-05 01:59:40 -05:00
socket =
socket
|> assign(
page_title: page_title(socket.assigns.live_action),
ammo_type: ammo_type,
2022-02-08 00:21:22 -05:00
ammo_groups: ammo_groups,
avg_cost_per_round: ammo_type |> Ammo.get_average_cost_for_ammo_type!()
2022-02-05 01:59:40 -05:00
)
{:noreply, socket}
2021-09-02 23:31:14 -04:00
end
2022-01-31 23:20:57 -05:00
@impl true
def handle_event("delete", _, socket) do
socket.assigns.ammo_type |> Ammo.delete_ammo_type!()
{:noreply, socket |> push_redirect(to: Routes.ammo_type_index_path(socket, :index))}
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