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-03-04 21:57:22 -05:00
|
|
|
alias Cannery.{ActivityLog, ActivityLog.ShotGroup, Ammo, Ammo.AmmoGroup, Repo}
|
2022-02-15 17:33:45 -05:00
|
|
|
alias CanneryWeb.Endpoint
|
2022-02-23 20:45:58 -05:00
|
|
|
alias Phoenix.LiveView.Socket
|
2021-09-02 23:31:14 -04:00
|
|
|
|
|
|
|
@impl true
|
2022-05-05 23:26:29 -04:00
|
|
|
def mount(_params, _session, socket), do: {:ok, socket}
|
2021-09-02 23:31:14 -04:00
|
|
|
|
|
|
|
@impl true
|
2022-02-15 18:20:12 -05:00
|
|
|
def handle_params(
|
2022-02-23 20:45:58 -05:00
|
|
|
%{"id" => id, "shot_group_id" => shot_group_id},
|
2022-02-15 18:20:12 -05:00
|
|
|
_url,
|
|
|
|
%{assigns: %{live_action: live_action, current_user: current_user}} = socket
|
|
|
|
) do
|
2022-02-23 20:45:58 -05:00
|
|
|
shot_group = ActivityLog.get_shot_group!(shot_group_id, current_user)
|
|
|
|
|
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> assign(page_title: page_title(live_action), shot_group: shot_group)
|
|
|
|
|> display_ammo_group(id)
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_params(%{"id" => id}, _url, %{assigns: %{live_action: live_action}} = socket) do
|
|
|
|
{:noreply, socket |> assign(page_title: page_title(live_action)) |> display_ammo_group(id)}
|
2022-02-05 01:59:40 -05:00
|
|
|
end
|
|
|
|
|
2022-02-23 20:45:58 -05:00
|
|
|
defp page_title(:add_shot_group), do: gettext("Record Shots")
|
|
|
|
defp page_title(:edit_shot_group), do: gettext("Edit Shot Records")
|
2022-07-01 00:23:04 -04:00
|
|
|
defp page_title(:move), do: gettext("Move Ammo")
|
|
|
|
defp page_title(:show), do: gettext("Show Ammo")
|
|
|
|
defp page_title(:edit), do: gettext("Edit Ammo")
|
2022-02-23 20:45:58 -05:00
|
|
|
|
2022-02-05 01:59:40 -05:00
|
|
|
@impl true
|
2022-02-10 00:57:56 -05:00
|
|
|
def handle_event(
|
|
|
|
"delete",
|
2022-03-28 23:05:12 -04:00
|
|
|
_params,
|
2022-02-10 00:57:56 -05:00
|
|
|
%{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket
|
|
|
|
) do
|
|
|
|
ammo_group |> Ammo.delete_ammo_group!(current_user)
|
|
|
|
|
2022-07-01 00:23:04 -04:00
|
|
|
prompt = dgettext("prompts", "Ammo deleted succesfully")
|
2022-02-10 00:57:56 -05:00
|
|
|
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",
|
2022-03-28 23:05:12 -04:00
|
|
|
_params,
|
2022-02-15 17:33:45 -05:00
|
|
|
%{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)
|
|
|
|
|
2022-02-23 20:45:58 -05:00
|
|
|
{:noreply, socket |> display_ammo_group(ammo_group)}
|
2022-02-15 17:33:45 -05:00
|
|
|
end
|
|
|
|
|
2022-02-23 20:45:58 -05:00
|
|
|
@impl true
|
|
|
|
def handle_event(
|
|
|
|
"delete_shot_group",
|
|
|
|
%{"id" => id},
|
|
|
|
%{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket
|
|
|
|
) do
|
|
|
|
{:ok, _} =
|
|
|
|
ActivityLog.get_shot_group!(id, current_user)
|
|
|
|
|> ActivityLog.delete_shot_group(current_user)
|
|
|
|
|
|
|
|
prompt = dgettext("prompts", "Shot records deleted succesfully")
|
|
|
|
{:noreply, socket |> put_flash(:info, prompt) |> display_ammo_group(ammo_group)}
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec display_ammo_group(Socket.t(), AmmoGroup.t() | AmmoGroup.id()) :: Socket.t()
|
|
|
|
defp display_ammo_group(socket, %AmmoGroup{} = ammo_group) do
|
|
|
|
ammo_group = ammo_group |> Repo.preload([:container, :ammo_type, :shot_groups], force: true)
|
2022-03-04 21:57:22 -05:00
|
|
|
|
|
|
|
columns = [
|
2022-11-07 01:36:28 -05:00
|
|
|
%{label: gettext("Rounds shot"), key: :count},
|
|
|
|
%{label: gettext("Notes"), key: :notes},
|
|
|
|
%{label: gettext("Date"), key: :date},
|
|
|
|
%{label: nil, key: :actions, sortable: false}
|
2022-03-04 21:57:22 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
rows =
|
|
|
|
ammo_group.shot_groups
|
|
|
|
|> Enum.map(fn shot_group ->
|
|
|
|
ammo_group |> get_table_row_for_shot_group(shot_group, columns)
|
|
|
|
end)
|
|
|
|
|
|
|
|
socket |> assign(ammo_group: ammo_group, columns: columns, rows: rows)
|
2022-02-23 20:45:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
defp display_ammo_group(%{assigns: %{current_user: current_user}} = socket, id),
|
|
|
|
do: display_ammo_group(socket, Ammo.get_ammo_group!(id, current_user))
|
2022-03-04 21:57:22 -05:00
|
|
|
|
|
|
|
@spec get_table_row_for_shot_group(AmmoGroup.t(), ShotGroup.t(), [map()]) :: [map()]
|
|
|
|
defp get_table_row_for_shot_group(ammo_group, %{date: date} = shot_group, columns) do
|
|
|
|
assigns = %{ammo_group: ammo_group, shot_group: shot_group}
|
|
|
|
|
|
|
|
columns
|
|
|
|
|> Enum.into(%{}, fn %{key: key} ->
|
|
|
|
value =
|
|
|
|
case key do
|
2022-11-07 01:36:28 -05:00
|
|
|
:date ->
|
2022-03-04 21:57:22 -05:00
|
|
|
{date, date |> display_date()}
|
|
|
|
|
2022-11-07 01:36:28 -05:00
|
|
|
:actions ->
|
2022-03-04 21:57:22 -05:00
|
|
|
~H"""
|
|
|
|
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
|
|
|
|
<%= live_patch to: Routes.ammo_group_show_path(Endpoint, :edit_shot_group, @ammo_group, shot_group),
|
|
|
|
class: "text-primary-600 link",
|
|
|
|
data: [qa: "edit-#{shot_group.id}"] do %>
|
|
|
|
<i class="fa-fw fa-lg fas fa-edit"></i>
|
|
|
|
<% end %>
|
|
|
|
|
|
|
|
<%= link to: "#",
|
|
|
|
class: "text-primary-600 link",
|
|
|
|
phx_click: "delete_shot_group",
|
|
|
|
phx_value_id: shot_group.id,
|
|
|
|
data: [
|
|
|
|
confirm: dgettext("prompts", "Are you sure you want to delete this shot record?"),
|
|
|
|
qa: "delete-#{shot_group.id}"
|
|
|
|
] do %>
|
|
|
|
<i class="fa-fw fa-lg fas fa-trash"></i>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
|
|
|
key ->
|
2022-11-07 01:36:28 -05:00
|
|
|
shot_group |> Map.get(key)
|
2022-03-04 21:57:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
{key, value}
|
|
|
|
end)
|
|
|
|
end
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|