2023-03-29 22:54:55 -04:00
|
|
|
defmodule CanneryWeb.PackLive.Show do
|
2022-01-22 21:40:29 -05:00
|
|
|
@moduledoc """
|
2023-03-29 22:54:55 -04:00
|
|
|
Liveview for showing and editing an Cannery.Ammo.Pack
|
2022-01-22 21:40:29 -05:00
|
|
|
"""
|
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
use CanneryWeb, :live_view
|
2023-03-30 20:43:30 -04:00
|
|
|
alias Cannery.{ActivityLog, ActivityLog.ShotRecord}
|
2023-03-29 22:54:55 -04:00
|
|
|
alias Cannery.{Ammo, Ammo.Pack}
|
2023-03-19 11:19:55 -04:00
|
|
|
alias Cannery.{ComparableDate, Containers}
|
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(
|
2023-03-30 20:43:30 -04:00
|
|
|
%{"id" => id, "shot_record_id" => shot_record_id},
|
2022-02-15 18:20:12 -05:00
|
|
|
_url,
|
|
|
|
%{assigns: %{live_action: live_action, current_user: current_user}} = socket
|
|
|
|
) do
|
2023-03-30 20:43:30 -04:00
|
|
|
shot_record = ActivityLog.get_shot_record!(shot_record_id, current_user)
|
2022-02-23 20:45:58 -05:00
|
|
|
|
|
|
|
socket =
|
|
|
|
socket
|
2023-03-30 20:43:30 -04:00
|
|
|
|> assign(page_title: page_title(live_action), shot_record: shot_record)
|
2023-03-29 22:54:55 -04:00
|
|
|
|> display_pack(id)
|
2022-02-23 20:45:58 -05:00
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_params(%{"id" => id}, _url, %{assigns: %{live_action: live_action}} = socket) do
|
2022-11-19 14:41:53 -05:00
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> assign(page_title: page_title(live_action))
|
2023-03-29 22:54:55 -04:00
|
|
|
|> display_pack(id)
|
2022-11-19 14:41:53 -05:00
|
|
|
|
|
|
|
{:noreply, socket}
|
2022-02-05 01:59:40 -05:00
|
|
|
end
|
|
|
|
|
2023-03-30 20:43:30 -04:00
|
|
|
defp page_title(:add_shot_record), do: gettext("Record Shots")
|
|
|
|
defp page_title(:edit_shot_record), 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,
|
2023-03-29 22:54:55 -04:00
|
|
|
%{assigns: %{pack: pack, current_user: current_user}} = socket
|
2022-02-10 00:57:56 -05:00
|
|
|
) do
|
2023-03-29 22:54:55 -04:00
|
|
|
pack |> Ammo.delete_pack!(current_user)
|
2022-02-10 00:57:56 -05:00
|
|
|
|
2022-07-01 00:23:04 -04:00
|
|
|
prompt = dgettext("prompts", "Ammo deleted succesfully")
|
2023-03-29 22:54:55 -04:00
|
|
|
redirect_to = Routes.pack_index_path(socket, :index)
|
2022-02-10 00:57:56 -05:00
|
|
|
|
2022-11-07 22:36:38 -05:00
|
|
|
{:noreply, socket |> put_flash(:info, prompt) |> push_navigate(to: redirect_to)}
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
2022-02-15 17:33:45 -05:00
|
|
|
def handle_event(
|
|
|
|
"toggle_staged",
|
2022-03-28 23:05:12 -04:00
|
|
|
_params,
|
2023-03-29 22:54:55 -04:00
|
|
|
%{assigns: %{pack: pack, current_user: current_user}} = socket
|
2022-02-15 17:33:45 -05:00
|
|
|
) do
|
2023-03-29 22:54:55 -04:00
|
|
|
{:ok, pack} = pack |> Ammo.update_pack(%{"staged" => !pack.staged}, current_user)
|
2022-02-15 17:33:45 -05:00
|
|
|
|
2023-03-29 22:54:55 -04:00
|
|
|
{:noreply, socket |> display_pack(pack)}
|
2022-02-15 17:33:45 -05:00
|
|
|
end
|
|
|
|
|
2022-02-23 20:45:58 -05:00
|
|
|
def handle_event(
|
2023-03-30 20:43:30 -04:00
|
|
|
"delete_shot_record",
|
2022-02-23 20:45:58 -05:00
|
|
|
%{"id" => id},
|
2023-03-29 22:54:55 -04:00
|
|
|
%{assigns: %{pack: %{id: pack_id}, current_user: current_user}} = socket
|
2022-02-23 20:45:58 -05:00
|
|
|
) do
|
|
|
|
{:ok, _} =
|
2023-03-30 20:43:30 -04:00
|
|
|
ActivityLog.get_shot_record!(id, current_user)
|
|
|
|
|> ActivityLog.delete_shot_record(current_user)
|
2022-02-23 20:45:58 -05:00
|
|
|
|
|
|
|
prompt = dgettext("prompts", "Shot records deleted succesfully")
|
2023-03-29 22:54:55 -04:00
|
|
|
{:noreply, socket |> put_flash(:info, prompt) |> display_pack(pack_id)}
|
2022-02-23 20:45:58 -05:00
|
|
|
end
|
|
|
|
|
2023-03-29 22:54:55 -04:00
|
|
|
@spec display_pack(Socket.t(), Pack.t() | Pack.id()) :: Socket.t()
|
|
|
|
defp display_pack(
|
2023-03-18 21:06:00 -04:00
|
|
|
%{assigns: %{current_user: current_user}} = socket,
|
2023-03-29 22:54:55 -04:00
|
|
|
%Pack{container_id: container_id} = pack
|
2023-03-18 21:06:00 -04:00
|
|
|
) do
|
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},
|
2023-03-19 11:19:55 -04:00
|
|
|
%{label: gettext("Date"), key: :date, type: ComparableDate},
|
2023-03-19 12:35:26 -04:00
|
|
|
%{label: gettext("Actions"), key: :actions, sortable: false}
|
2022-03-04 21:57:22 -05:00
|
|
|
]
|
|
|
|
|
2023-03-30 20:43:30 -04:00
|
|
|
shot_records = ActivityLog.list_shot_records_for_pack(pack, current_user)
|
2023-03-18 21:06:00 -04:00
|
|
|
|
2022-03-04 21:57:22 -05:00
|
|
|
rows =
|
2023-03-30 20:43:30 -04:00
|
|
|
shot_records
|
|
|
|
|> Enum.map(fn shot_record ->
|
|
|
|
pack |> get_table_row_for_shot_record(shot_record, columns)
|
2022-03-04 21:57:22 -05:00
|
|
|
end)
|
|
|
|
|
2023-03-18 21:06:00 -04:00
|
|
|
socket
|
|
|
|
|> assign(
|
2023-03-29 22:54:55 -04:00
|
|
|
pack: pack,
|
|
|
|
original_count: Ammo.get_original_count(pack, current_user),
|
|
|
|
percentage_remaining: Ammo.get_percentage_remaining(pack, current_user),
|
2023-03-18 21:06:00 -04:00
|
|
|
container: container_id && Containers.get_container!(container_id, current_user),
|
2023-03-30 20:43:30 -04:00
|
|
|
shot_records: shot_records,
|
2023-03-18 21:06:00 -04:00
|
|
|
columns: columns,
|
|
|
|
rows: rows
|
|
|
|
)
|
2022-02-23 20:45:58 -05:00
|
|
|
end
|
|
|
|
|
2023-03-29 22:54:55 -04:00
|
|
|
defp display_pack(%{assigns: %{current_user: current_user}} = socket, id),
|
|
|
|
do: display_pack(socket, Ammo.get_pack!(id, current_user))
|
2022-03-04 21:57:22 -05:00
|
|
|
|
2023-03-18 21:06:00 -04:00
|
|
|
@spec display_currency(float()) :: String.t()
|
|
|
|
defp display_currency(float), do: :erlang.float_to_binary(float, decimals: 2)
|
|
|
|
|
2023-03-30 20:43:30 -04:00
|
|
|
@spec get_table_row_for_shot_record(Pack.t(), ShotRecord.t(), [map()]) :: map()
|
|
|
|
defp get_table_row_for_shot_record(pack, %{id: id, date: date} = shot_record, columns) do
|
|
|
|
assigns = %{pack: pack, shot_record: shot_record}
|
2022-03-04 21:57:22 -05:00
|
|
|
|
|
|
|
columns
|
2022-11-12 13:52:24 -05:00
|
|
|
|> Map.new(fn %{key: key} ->
|
2022-03-04 21:57:22 -05:00
|
|
|
value =
|
|
|
|
case key do
|
2022-11-07 01:36:28 -05:00
|
|
|
:date ->
|
2023-03-18 21:06:00 -04:00
|
|
|
assigns = %{id: id, date: date}
|
2023-01-26 00:39:16 -05:00
|
|
|
|
|
|
|
{date,
|
|
|
|
~H"""
|
2023-03-18 21:06:00 -04:00
|
|
|
<.date id={"#{@id}-date"} date={@date} />
|
2023-01-26 00:39:16 -05:00
|
|
|
"""}
|
2022-03-04 21:57:22 -05:00
|
|
|
|
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">
|
2022-11-07 22:36:38 -05:00
|
|
|
<.link
|
2023-03-30 20:43:30 -04:00
|
|
|
patch={Routes.pack_show_path(Endpoint, :edit_shot_record, @pack, @shot_record)}
|
2022-11-07 22:36:38 -05:00
|
|
|
class="text-primary-600 link"
|
2023-03-15 00:45:08 -04:00
|
|
|
aria-label={
|
2023-03-30 20:43:30 -04:00
|
|
|
dgettext("actions", "Edit shot record of %{shot_record_count} shots",
|
|
|
|
shot_record_count: @shot_record.count
|
2023-03-17 21:00:24 -04:00
|
|
|
)
|
2023-03-15 00:45:08 -04:00
|
|
|
}
|
2022-11-07 22:36:38 -05:00
|
|
|
>
|
2022-03-04 21:57:22 -05:00
|
|
|
<i class="fa-fw fa-lg fas fa-edit"></i>
|
2022-11-07 22:36:38 -05:00
|
|
|
</.link>
|
|
|
|
|
|
|
|
<.link
|
|
|
|
href="#"
|
|
|
|
class="text-primary-600 link"
|
2023-03-30 20:43:30 -04:00
|
|
|
phx-click="delete_shot_record"
|
|
|
|
phx-value-id={@shot_record.id}
|
2022-11-07 22:36:38 -05:00
|
|
|
data-confirm={dgettext("prompts", "Are you sure you want to delete this shot record?")}
|
2023-03-15 00:45:08 -04:00
|
|
|
aria-label={
|
2023-03-30 20:43:30 -04:00
|
|
|
dgettext("actions", "Delete shot record of %{shot_record_count} shots",
|
|
|
|
shot_record_count: @shot_record.count
|
2023-03-15 00:45:08 -04:00
|
|
|
)
|
|
|
|
}
|
2022-11-07 22:36:38 -05:00
|
|
|
>
|
2022-03-04 21:57:22 -05:00
|
|
|
<i class="fa-fw fa-lg fas fa-trash"></i>
|
2022-11-07 22:36:38 -05:00
|
|
|
</.link>
|
2022-03-04 21:57:22 -05:00
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
|
|
|
key ->
|
2023-03-30 20:43:30 -04:00
|
|
|
shot_record |> 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
|