2022-02-15 17:33:45 -05:00
|
|
|
defmodule CanneryWeb.RangeLive.Index do
|
|
|
|
@moduledoc """
|
|
|
|
Main page for range day mode, where `AmmoGroup`s can be used up.
|
|
|
|
"""
|
|
|
|
|
|
|
|
use CanneryWeb, :live_view
|
|
|
|
import CanneryWeb.Components.AmmoGroupCard
|
|
|
|
alias Cannery.{ActivityLog, ActivityLog.ShotGroup, Ammo, Repo}
|
|
|
|
alias CanneryWeb.Endpoint
|
|
|
|
alias Phoenix.LiveView.Socket
|
|
|
|
|
|
|
|
@impl true
|
2022-05-05 23:26:29 -04:00
|
|
|
def mount(_params, _session, socket), do: {:ok, socket |> display_shot_groups()}
|
2022-02-15 17:33:45 -05:00
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
|
|
|
|
{:noreply, apply_action(socket, live_action, params)}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(
|
|
|
|
%{assigns: %{current_user: current_user}} = socket,
|
|
|
|
:add_shot_group,
|
|
|
|
%{"id" => id}
|
|
|
|
) do
|
|
|
|
socket
|
2022-02-23 20:45:58 -05:00
|
|
|
|> assign(:page_title, gettext("Record Shots"))
|
2022-02-15 17:33:45 -05:00
|
|
|
|> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
|
|
|
|
socket
|
|
|
|
|> assign(:page_title, gettext("Edit Shot Records"))
|
|
|
|
|> assign(:shot_group, ActivityLog.get_shot_group!(id, current_user))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(socket, :new, _params) do
|
|
|
|
socket
|
|
|
|
|> assign(:page_title, gettext("New Shot Records"))
|
|
|
|
|> assign(:shot_group, %ShotGroup{})
|
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(socket, :index, _params) do
|
|
|
|
socket
|
|
|
|
|> assign(:page_title, gettext("Shot Records"))
|
|
|
|
|> assign(:shot_group, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("delete", %{"id" => id}, %{assigns: %{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_shot_groups()}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event(
|
|
|
|
"toggle_staged",
|
|
|
|
%{"ammo_group_id" => ammo_group_id},
|
|
|
|
%{assigns: %{current_user: current_user}} = socket
|
|
|
|
) do
|
|
|
|
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
|
|
|
|
|
|
|
|
{:ok, _ammo_group} =
|
|
|
|
ammo_group |> Ammo.update_ammo_group(%{"staged" => !ammo_group.staged}, current_user)
|
|
|
|
|
2022-07-01 00:23:04 -04:00
|
|
|
prompt = dgettext("prompts", "Ammo unstaged succesfully")
|
2022-02-15 17:33:45 -05:00
|
|
|
{:noreply, socket |> put_flash(:info, prompt) |> display_shot_groups()}
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec display_shot_groups(Socket.t()) :: Socket.t()
|
|
|
|
defp display_shot_groups(%{assigns: %{current_user: current_user}} = socket) do
|
|
|
|
shot_groups =
|
|
|
|
ActivityLog.list_shot_groups(current_user) |> Repo.preload(ammo_group: :ammo_type)
|
|
|
|
|
|
|
|
ammo_groups = Ammo.list_staged_ammo_groups(current_user)
|
2022-03-04 22:06:01 -05:00
|
|
|
|
|
|
|
columns = [
|
2022-11-07 01:36:28 -05:00
|
|
|
%{label: gettext("Ammo"), key: :name},
|
|
|
|
%{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 22:06:01 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
rows =
|
|
|
|
shot_groups
|
2022-03-04 22:27:09 -05:00
|
|
|
|> Enum.map(fn shot_group -> shot_group |> get_row_data_for_shot_group(columns) end)
|
2022-03-04 22:06:01 -05:00
|
|
|
|
2022-11-11 19:34:23 -05:00
|
|
|
chart_data = shot_groups |> get_chart_data_for_shot_group()
|
2022-11-09 21:04:57 -05:00
|
|
|
|
2022-03-04 22:06:01 -05:00
|
|
|
socket
|
2022-11-09 21:04:57 -05:00
|
|
|
|> assign(
|
|
|
|
ammo_groups: ammo_groups,
|
|
|
|
columns: columns,
|
|
|
|
rows: rows,
|
|
|
|
chart_data: chart_data,
|
|
|
|
shot_groups: shot_groups
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-11-11 19:34:23 -05:00
|
|
|
@spec get_chart_data_for_shot_group([ShotGroup.t()]) :: [map()]
|
|
|
|
defp get_chart_data_for_shot_group(shot_groups) do
|
|
|
|
shot_groups
|
|
|
|
|> Repo.preload(ammo_group: :ammo_type)
|
|
|
|
|> Enum.group_by(fn %{date: date} -> date end, fn %{count: count} -> count end)
|
|
|
|
|> Enum.map(fn {date, rounds} ->
|
|
|
|
sum = Enum.sum(rounds)
|
|
|
|
|
|
|
|
%{
|
|
|
|
date: date,
|
|
|
|
count: sum,
|
|
|
|
label: gettext("Rounds shot: %{count}", count: sum)
|
|
|
|
}
|
2022-11-09 21:04:57 -05:00
|
|
|
end)
|
2022-11-11 19:34:23 -05:00
|
|
|
|> Enum.sort_by(fn %{date: date} -> date end)
|
2022-02-15 17:33:45 -05:00
|
|
|
end
|
2022-03-04 22:27:09 -05:00
|
|
|
|
2022-11-09 21:04:57 -05:00
|
|
|
@spec get_row_data_for_shot_group(ShotGroup.t(), [map()]) :: map()
|
2022-03-04 22:27:09 -05:00
|
|
|
defp get_row_data_for_shot_group(%{date: date} = shot_group, columns) do
|
|
|
|
shot_group = shot_group |> Repo.preload(ammo_group: :ammo_type)
|
|
|
|
assigns = %{shot_group: shot_group}
|
|
|
|
|
|
|
|
columns
|
2022-11-09 21:04:57 -05:00
|
|
|
|> Map.new(fn %{key: key} ->
|
2022-03-04 22:27:09 -05:00
|
|
|
value =
|
|
|
|
case key do
|
2022-11-07 01:36:28 -05:00
|
|
|
:name ->
|
2022-03-04 22:27:09 -05:00
|
|
|
{shot_group.ammo_group.ammo_type.name,
|
2022-11-07 22:36:38 -05:00
|
|
|
~H"""
|
2022-11-09 23:47:11 -05:00
|
|
|
<.link
|
|
|
|
navigate={Routes.ammo_group_show_path(Endpoint, :show, @shot_group.ammo_group)}
|
|
|
|
class="link"
|
|
|
|
>
|
2022-11-07 22:36:38 -05:00
|
|
|
<%= @shot_group.ammo_group.ammo_type.name %>
|
|
|
|
</.link>
|
|
|
|
"""}
|
2022-03-04 22:27:09 -05:00
|
|
|
|
2022-11-07 01:36:28 -05:00
|
|
|
:date ->
|
2022-03-04 22:27:09 -05:00
|
|
|
date |> display_date()
|
|
|
|
|
2022-11-07 01:36:28 -05:00
|
|
|
:actions ->
|
2022-03-04 22:27:09 -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
|
|
|
|
patch={Routes.range_index_path(Endpoint, :edit, @shot_group)}
|
|
|
|
class="text-primary-600 link"
|
|
|
|
data-qa={"edit-#{@shot_group.id}"}
|
|
|
|
>
|
2022-03-04 22:27:09 -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"
|
|
|
|
phx-click="delete"
|
|
|
|
phx-value-id={@shot_group.id}
|
|
|
|
data-confirm={dgettext("prompts", "Are you sure you want to delete this shot record?")}
|
|
|
|
data-qa={"delete-#{@shot_group.id}"}
|
|
|
|
>
|
2022-03-04 22:27:09 -05:00
|
|
|
<i class="fa-fw fa-lg fas fa-trash"></i>
|
2022-11-07 22:36:38 -05:00
|
|
|
</.link>
|
2022-03-04 22:27:09 -05:00
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
|
|
|
key ->
|
2022-11-07 01:36:28 -05:00
|
|
|
shot_group |> Map.get(key)
|
2022-03-04 22:27:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
{key, value}
|
|
|
|
end)
|
|
|
|
end
|
2022-02-15 17:33:45 -05:00
|
|
|
end
|