2021-09-02 23:31:14 -04:00
|
|
|
defmodule CanneryWeb.ContainerLive.Show do
|
2022-01-22 21:40:29 -05:00
|
|
|
@moduledoc """
|
|
|
|
Liveview for showing and editing a Cannery.Containers.Container
|
|
|
|
"""
|
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
use CanneryWeb, :live_view
|
2023-03-18 21:06:00 -04:00
|
|
|
alias Cannery.{Accounts.User, ActivityLog, Ammo, Containers, Containers.Container}
|
2022-02-13 21:14:48 -05:00
|
|
|
alias CanneryWeb.Endpoint
|
2022-02-11 00:33:51 -05:00
|
|
|
alias Ecto.Changeset
|
2022-02-13 21:14:48 -05:00
|
|
|
alias Phoenix.LiveView.Socket
|
2021-09-02 23:31:14 -04:00
|
|
|
|
|
|
|
@impl true
|
2022-12-03 20:07:51 -05:00
|
|
|
def mount(_params, _session, socket),
|
2023-03-28 23:08:40 -04:00
|
|
|
do: {:ok, socket |> assign(class: :all, view_table: true)}
|
2021-09-02 23:31:14 -04:00
|
|
|
|
|
|
|
@impl true
|
2022-12-03 20:07:51 -05:00
|
|
|
def handle_params(%{"id" => id}, _session, %{assigns: %{current_user: current_user}} = socket) do
|
2022-11-12 13:52:24 -05:00
|
|
|
socket =
|
2022-11-19 14:05:29 -05:00
|
|
|
socket
|
2023-03-28 23:08:40 -04:00
|
|
|
|> assign(:view_table, true)
|
2022-11-19 14:05:29 -05:00
|
|
|
|> render_container(id, current_user)
|
2022-11-12 13:52:24 -05:00
|
|
|
|
|
|
|
{:noreply, socket}
|
2022-02-13 21:14:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event(
|
|
|
|
"delete_tag",
|
|
|
|
%{"tag-id" => tag_id},
|
|
|
|
%{assigns: %{container: container, current_user: current_user}} = socket
|
|
|
|
) do
|
2022-01-31 21:56:02 -05:00
|
|
|
socket =
|
2023-03-18 21:06:00 -04:00
|
|
|
case Containers.get_tag(tag_id, current_user) do
|
2022-02-13 21:14:48 -05:00
|
|
|
{:ok, tag} ->
|
|
|
|
_count = Containers.remove_tag!(container, tag, current_user)
|
|
|
|
|
|
|
|
prompt =
|
|
|
|
dgettext("prompts", "%{tag_name} has been removed from %{container_name}",
|
|
|
|
tag_name: tag.name,
|
|
|
|
container_name: container.name
|
|
|
|
)
|
|
|
|
|
2022-11-07 00:37:53 -05:00
|
|
|
socket |> put_flash(:info, prompt) |> render_container()
|
2022-02-13 21:14:48 -05:00
|
|
|
|
2023-03-18 21:06:00 -04:00
|
|
|
{:error, :not_found} ->
|
|
|
|
socket |> put_flash(:error, dgettext("errors", "Tag not found"))
|
2022-02-13 21:14:48 -05:00
|
|
|
end
|
2022-01-31 21:56:02 -05:00
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2022-02-11 00:33:51 -05:00
|
|
|
def handle_event(
|
2022-02-13 21:14:48 -05:00
|
|
|
"delete_container",
|
2022-03-28 23:05:12 -04:00
|
|
|
_params,
|
2022-02-11 00:33:51 -05:00
|
|
|
%{assigns: %{container: container, current_user: current_user}} = socket
|
|
|
|
) do
|
2022-02-07 23:58:29 -05:00
|
|
|
socket =
|
2022-02-11 00:33:51 -05:00
|
|
|
Containers.delete_container(container, current_user)
|
2022-02-07 23:58:29 -05:00
|
|
|
|> case do
|
2022-02-11 00:33:51 -05:00
|
|
|
{:ok, %{name: container_name}} ->
|
|
|
|
prompt = dgettext("prompts", "%{name} has been deleted", name: container_name)
|
|
|
|
|
2022-02-07 23:58:29 -05:00
|
|
|
socket
|
2022-02-11 00:33:51 -05:00
|
|
|
|> put_flash(:info, prompt)
|
2022-11-07 22:36:38 -05:00
|
|
|
|> push_navigate(to: Routes.container_index_path(socket, :index))
|
2022-02-07 23:58:29 -05:00
|
|
|
|
|
|
|
{:error, %{action: :delete, errors: [ammo_groups: _error], valid?: false} = changeset} ->
|
|
|
|
ammo_groups_error = changeset |> changeset_errors(:ammo_groups) |> Enum.join(", ")
|
2022-02-09 00:49:47 -05:00
|
|
|
|
2022-02-11 00:33:51 -05:00
|
|
|
prompt =
|
|
|
|
dgettext("errors", "Could not delete %{name}: %{error}",
|
|
|
|
name: changeset |> Changeset.get_field(:name, "container"),
|
|
|
|
error: ammo_groups_error
|
|
|
|
)
|
|
|
|
|
|
|
|
socket |> put_flash(:error, prompt)
|
2022-02-07 23:58:29 -05:00
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
socket |> put_flash(:error, changeset |> changeset_errors())
|
|
|
|
end
|
|
|
|
|
|
|
|
{:noreply, socket}
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
2022-12-03 20:07:51 -05:00
|
|
|
def handle_event("toggle_table", _params, %{assigns: %{view_table: view_table}} = socket) do
|
|
|
|
{:noreply, socket |> assign(:view_table, !view_table) |> render_container()}
|
2022-11-12 13:52:24 -05:00
|
|
|
end
|
|
|
|
|
2023-03-28 23:08:40 -04:00
|
|
|
def handle_event("change_class", %{"ammo_type" => %{"class" => "rifle"}}, socket) do
|
|
|
|
{:noreply, socket |> assign(:class, :rifle) |> render_container()}
|
2023-03-23 22:07:25 -04:00
|
|
|
end
|
|
|
|
|
2023-03-28 23:08:40 -04:00
|
|
|
def handle_event("change_class", %{"ammo_type" => %{"class" => "shotgun"}}, socket) do
|
|
|
|
{:noreply, socket |> assign(:class, :shotgun) |> render_container()}
|
2023-03-23 22:07:25 -04:00
|
|
|
end
|
|
|
|
|
2023-03-28 23:08:40 -04:00
|
|
|
def handle_event("change_class", %{"ammo_type" => %{"class" => "pistol"}}, socket) do
|
|
|
|
{:noreply, socket |> assign(:class, :pistol) |> render_container()}
|
2023-03-23 22:07:25 -04:00
|
|
|
end
|
|
|
|
|
2023-03-28 23:08:40 -04:00
|
|
|
def handle_event("change_class", %{"ammo_type" => %{"class" => _all}}, socket) do
|
|
|
|
{:noreply, socket |> assign(:class, :all) |> render_container()}
|
2023-03-23 22:07:25 -04:00
|
|
|
end
|
|
|
|
|
2022-02-13 21:14:48 -05:00
|
|
|
@spec render_container(Socket.t(), Container.id(), User.t()) :: Socket.t()
|
2022-11-07 00:37:53 -05:00
|
|
|
defp render_container(
|
2023-03-28 23:08:40 -04:00
|
|
|
%{assigns: %{class: class, live_action: live_action}} = socket,
|
2022-11-07 00:37:53 -05:00
|
|
|
id,
|
|
|
|
current_user
|
|
|
|
) do
|
2023-03-18 21:06:00 -04:00
|
|
|
%{name: container_name} = container = Containers.get_container!(id, current_user)
|
2023-03-28 23:08:40 -04:00
|
|
|
ammo_groups = Ammo.list_ammo_groups_for_container(container, class, current_user)
|
2023-03-18 21:06:00 -04:00
|
|
|
original_counts = ammo_groups |> Ammo.get_original_counts(current_user)
|
|
|
|
cprs = ammo_groups |> Ammo.get_cprs(current_user)
|
|
|
|
last_used_dates = ammo_groups |> ActivityLog.get_last_used_dates(current_user)
|
2022-02-13 21:14:48 -05:00
|
|
|
|
2022-02-18 22:56:46 -05:00
|
|
|
page_title =
|
|
|
|
case live_action do
|
2023-03-19 13:37:28 -04:00
|
|
|
:show -> container_name
|
2022-02-18 22:56:46 -05:00
|
|
|
:edit -> gettext("Edit %{name}", name: container_name)
|
|
|
|
:edit_tags -> gettext("Edit %{name} tags", name: container_name)
|
|
|
|
end
|
|
|
|
|
2023-03-18 21:06:00 -04:00
|
|
|
socket
|
|
|
|
|> assign(
|
|
|
|
container: container,
|
|
|
|
round_count: Ammo.get_round_count_for_container!(container, current_user),
|
2023-03-23 22:07:25 -04:00
|
|
|
ammo_groups_count: Ammo.get_ammo_groups_count_for_container!(container, current_user),
|
2023-03-18 21:06:00 -04:00
|
|
|
ammo_groups: ammo_groups,
|
|
|
|
original_counts: original_counts,
|
|
|
|
cprs: cprs,
|
|
|
|
last_used_dates: last_used_dates,
|
|
|
|
page_title: page_title
|
|
|
|
)
|
2022-11-07 00:37:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec render_container(Socket.t()) :: Socket.t()
|
|
|
|
defp render_container(
|
|
|
|
%{assigns: %{container: %{id: container_id}, current_user: current_user}} = socket
|
|
|
|
) do
|
|
|
|
socket |> render_container(container_id, current_user)
|
2022-02-13 21:14:48 -05:00
|
|
|
end
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|