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

70 lines
2.1 KiB
Elixir
Raw Normal View History

2021-09-02 23:31:14 -04:00
defmodule CanneryWeb.AmmoGroupLive.Show do
2022-01-22 21:40:29 -05:00
@moduledoc """
Liveview for showing and editing an Cannery.Ammo.AmmoGroup
"""
2021-09-02 23:31:14 -04:00
use CanneryWeb, :live_view
2022-02-11 22:47:33 -05:00
import CanneryWeb.Components.ContainerCard
2022-02-05 01:59:40 -05:00
alias Cannery.{Ammo, Repo}
2022-02-15 17:33:45 -05:00
alias CanneryWeb.Endpoint
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-15 17:33:45 -05:00
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
socket |> assign(page_title: page_title(live_action)) |> apply_action(live_action, params)
end
defp apply_action(
%{assigns: %{current_user: current_user}} = socket,
:add_shot_group,
%{"id" => id}
) do
socket
|> assign(:page_title, gettext("Add Shot group"))
|> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
end
defp apply_action(
%{assigns: %{live_action: live_action, current_user: current_user}} = socket,
action,
%{"id" => id}
)
when action == :edit or action == :show do
ammo_group = Ammo.get_ammo_group!(id, current_user) |> Repo.preload([:container, :ammo_type])
{:noreply, socket |> assign(page_title: page_title(live_action), ammo_group: ammo_group)}
2022-02-05 01:59:40 -05:00
end
@impl true
def handle_event(
"delete",
_,
%{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket
) do
ammo_group |> Ammo.delete_ammo_group!(current_user)
prompt = dgettext("prompts", "Ammo group deleted succesfully")
redirect_to = Routes.ammo_group_index_path(socket, :index)
{:noreply, socket |> put_flash(:info, prompt) |> push_redirect(to: redirect_to)}
2021-09-02 23:31:14 -04:00
end
2022-02-15 17:33:45 -05:00
@impl true
def handle_event(
"toggle_staged",
_,
%{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket
) do
{:ok, ammo_group} =
ammo_group |> Ammo.update_ammo_group(%{"staged" => !ammo_group.staged}, current_user)
{:noreply, socket |> assign(ammo_group: ammo_group)}
end
2022-02-09 00:20:04 -05:00
defp page_title(:show), do: gettext("Show Ammo group")
defp page_title(:edit), do: gettext("Edit Ammo group")
2021-09-02 23:31:14 -04:00
end