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

106 lines
3.3 KiB
Elixir
Raw Normal View History

2021-09-02 23:31:14 -04:00
defmodule CanneryWeb.AmmoGroupLive.Index do
2022-01-22 21:40:29 -05:00
@moduledoc """
Liveview to show a Cannery.Ammo.AmmoGroup index
"""
2021-09-02 23:31:14 -04:00
use CanneryWeb, :live_view
alias Cannery.{Ammo, Ammo.AmmoGroup, Containers, Repo}
2022-02-15 17:33:45 -05:00
alias CanneryWeb.Endpoint
2021-09-02 23:31:14 -04:00
@impl true
2022-05-05 23:26:29 -04:00
def mount(_params, _session, socket) do
2022-11-10 21:45:50 -05:00
{:ok, socket |> assign(show_used: false)}
2021-09-02 23:31:14 -04:00
end
@impl true
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
2022-11-10 21:45:50 -05:00
{:noreply, apply_action(socket, live_action, params) |> display_ammo_groups()}
2021-09-02 23:31:14 -04:00
end
defp apply_action(
%{assigns: %{current_user: current_user}} = socket,
:add_shot_group,
%{"id" => id}
) do
socket
|> assign(:page_title, gettext("Record shots"))
|> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :move, %{"id" => id}) do
socket
|> assign(:page_title, gettext("Move Ammo group"))
|> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
2021-09-02 23:31:14 -04:00
socket
2022-02-09 00:20:04 -05:00
|> assign(:page_title, gettext("Edit Ammo group"))
|> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
2021-09-02 23:31:14 -04:00
end
2022-11-10 18:21:29 -05:00
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :clone, %{"id" => id}) do
socket
|> assign(:page_title, dgettext("actions", "Add Ammo"))
|> assign(:ammo_group, %{Ammo.get_ammo_group!(id, current_user) | id: nil})
end
2021-09-02 23:31:14 -04:00
defp apply_action(socket, :new, _params) do
socket
2022-02-09 00:20:04 -05:00
|> assign(:page_title, dgettext("actions", "Add Ammo"))
2021-09-02 23:31:14 -04:00
|> assign(:ammo_group, %AmmoGroup{})
end
defp apply_action(socket, :index, _params) do
2022-11-10 21:45:50 -05:00
socket
|> assign(:page_title, gettext("Ammo groups"))
|> assign(:ammo_group, nil)
2021-09-02 23:31:14 -04:00
end
@impl true
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
Ammo.get_ammo_group!(id, current_user) |> Ammo.delete_ammo_group!(current_user)
prompt = dgettext("prompts", "Ammo group deleted succesfully")
{:noreply, socket |> put_flash(:info, prompt) |> display_ammo_groups()}
2021-09-02 23:31:14 -04:00
end
2022-02-15 17:33:45 -05:00
@impl true
def handle_event(
"toggle_staged",
%{"ammo_group_id" => id},
%{assigns: %{current_user: current_user}} = socket
) do
ammo_group = Ammo.get_ammo_group!(id, current_user)
{:ok, _ammo_group} =
ammo_group |> Ammo.update_ammo_group(%{"staged" => !ammo_group.staged}, current_user)
{:noreply, socket |> display_ammo_groups()}
end
2022-11-07 00:47:22 -05:00
@impl true
2022-11-07 01:36:28 -05:00
def handle_event("toggle_show_used", _params, %{assigns: %{show_used: show_used}} = socket) do
2022-11-07 00:47:22 -05:00
{:noreply, socket |> assign(:show_used, !show_used) |> display_ammo_groups()}
end
defp display_ammo_groups(
%{assigns: %{current_user: current_user, show_used: show_used}} = socket
) do
ammo_groups =
2022-11-10 21:45:50 -05:00
Ammo.list_ammo_groups(current_user, show_used)
|> Repo.preload([:ammo_type, :container], force: true)
2022-11-07 00:47:22 -05:00
ammo_types_count = Ammo.get_ammo_types_count!(current_user)
containers_count = Containers.get_containers_count!(current_user)
socket
|> assign(
ammo_groups: ammo_groups,
ammo_types_count: ammo_types_count,
containers_count: containers_count
)
2021-09-02 23:31:14 -04:00
end
end