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

159 lines
4.9 KiB
Elixir
Raw Normal View History

2022-02-15 17:33:45 -05:00
defmodule CanneryWeb.RangeLive.Index do
@moduledoc """
2023-03-29 22:54:55 -04:00
Main page for range day mode, where `Pack`s can be used up.
2022-02-15 17:33:45 -05:00
"""
use CanneryWeb, :live_view
2023-03-30 20:43:30 -04:00
alias Cannery.{ActivityLog, ActivityLog.ShotRecord, Ammo}
2022-02-15 17:33:45 -05:00
alias Phoenix.LiveView.Socket
@impl true
2022-12-03 21:27:39 -05:00
def mount(%{"search" => search}, _session, socket) do
2023-03-30 20:43:30 -04:00
{:ok, socket |> assign(class: :all, search: search) |> display_shot_records()}
2022-12-03 21:27:39 -05:00
end
def mount(_params, _session, socket) do
2023-03-30 20:43:30 -04:00
{:ok, socket |> assign(class: :all, search: nil) |> display_shot_records()}
2022-12-03 21:27:39 -05:00
end
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,
2023-03-30 20:43:30 -04:00
:add_shot_record,
2022-02-15 17:33:45 -05:00
%{"id" => id}
) do
socket
2022-12-03 21:27:39 -05:00
|> assign(
page_title: gettext("Record Shots"),
2023-03-29 22:54:55 -04:00
pack: Ammo.get_pack!(id, current_user)
2022-12-03 21:27:39 -05:00
)
2022-02-15 17:33:45 -05:00
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
socket
2022-12-03 21:27:39 -05:00
|> assign(
2023-03-30 21:38:56 -04:00
page_title: gettext("Edit Shot Record"),
2023-03-30 20:43:30 -04:00
shot_record: ActivityLog.get_shot_record!(id, current_user)
2022-12-03 21:27:39 -05:00
)
2022-02-15 17:33:45 -05:00
end
defp apply_action(socket, :new, _params) do
socket
2022-12-03 21:27:39 -05:00
|> assign(
2023-06-05 23:36:25 -04:00
page_title: gettext("Record Shots"),
2023-03-30 20:43:30 -04:00
shot_record: %ShotRecord{}
2022-12-03 21:27:39 -05:00
)
2022-02-15 17:33:45 -05:00
end
defp apply_action(socket, :index, _params) do
socket
2022-12-03 21:27:39 -05:00
|> assign(
2023-06-05 23:36:25 -04:00
page_title: gettext("Range"),
2022-12-03 21:27:39 -05:00
search: nil,
2023-03-30 20:43:30 -04:00
shot_record: nil
2022-12-03 21:27:39 -05:00
)
2023-03-30 20:43:30 -04:00
|> display_shot_records()
2022-12-03 21:27:39 -05:00
end
defp apply_action(socket, :search, %{"search" => search}) do
socket
|> assign(
2023-06-05 23:36:25 -04:00
page_title: gettext("Range"),
2022-12-03 21:27:39 -05:00
search: search,
2023-03-30 20:43:30 -04:00
shot_record: nil
2022-12-03 21:27:39 -05:00
)
2023-03-30 20:43:30 -04:00
|> display_shot_records()
2022-02-15 17:33:45 -05:00
end
@impl true
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) 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-15 17:33:45 -05:00
prompt = dgettext("prompts", "Shot records deleted succesfully")
2023-03-30 20:43:30 -04:00
{:noreply, socket |> put_flash(:info, prompt) |> display_shot_records()}
2022-02-15 17:33:45 -05:00
end
def handle_event(
"toggle_staged",
2023-03-29 22:54:55 -04:00
%{"pack_id" => pack_id},
2022-02-15 17:33:45 -05:00
%{assigns: %{current_user: current_user}} = socket
) do
2023-03-29 22:54:55 -04:00
pack = Ammo.get_pack!(pack_id, current_user)
2022-02-15 17:33:45 -05:00
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
2022-07-01 00:23:04 -04:00
prompt = dgettext("prompts", "Ammo unstaged succesfully")
2023-03-30 20:43:30 -04:00
{:noreply, socket |> put_flash(:info, prompt) |> display_shot_records()}
2022-02-15 17:33:45 -05:00
end
2022-12-03 21:27:39 -05:00
def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
2023-04-14 23:34:11 -04:00
{:noreply, socket |> push_patch(to: ~p"/range")}
2022-12-03 21:27:39 -05:00
end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
2023-04-14 23:34:11 -04:00
{:noreply, socket |> push_patch(to: ~p"/range/search/#{search_term}")}
2022-12-03 21:27:39 -05:00
end
2023-03-30 21:53:52 -04:00
def handle_event("change_class", %{"type" => %{"class" => "rifle"}}, socket) do
2023-03-30 20:43:30 -04:00
{:noreply, socket |> assign(:class, :rifle) |> display_shot_records()}
2023-03-23 22:07:25 -04:00
end
2023-03-30 21:53:52 -04:00
def handle_event("change_class", %{"type" => %{"class" => "shotgun"}}, socket) do
2023-03-30 20:43:30 -04:00
{:noreply, socket |> assign(:class, :shotgun) |> display_shot_records()}
2023-03-23 22:07:25 -04:00
end
2023-03-30 21:53:52 -04:00
def handle_event("change_class", %{"type" => %{"class" => "pistol"}}, socket) do
2023-03-30 20:43:30 -04:00
{:noreply, socket |> assign(:class, :pistol) |> display_shot_records()}
2023-03-23 22:07:25 -04:00
end
2023-03-30 21:53:52 -04:00
def handle_event("change_class", %{"type" => %{"class" => _all}}, socket) do
2023-03-30 20:43:30 -04:00
{:noreply, socket |> assign(:class, :all) |> display_shot_records()}
2023-03-23 22:07:25 -04:00
end
2023-03-30 20:43:30 -04:00
@spec display_shot_records(Socket.t()) :: Socket.t()
defp display_shot_records(
2023-03-28 23:08:40 -04:00
%{assigns: %{class: class, search: search, current_user: current_user}} = socket
2023-03-23 22:07:25 -04:00
) do
2023-06-05 21:47:03 -04:00
shot_records = ActivityLog.list_shot_records(current_user, search: search, class: class)
2023-06-04 00:00:51 -04:00
packs = Ammo.list_packs(current_user, staged: true)
2023-03-30 20:43:30 -04:00
chart_data = shot_records |> get_chart_data_for_shot_record()
2023-03-29 22:54:55 -04:00
original_counts = packs |> Ammo.get_original_counts(current_user)
cprs = packs |> Ammo.get_cprs(current_user)
last_used_dates = packs |> ActivityLog.get_last_used_dates(current_user)
shot_record_count = ActivityLog.get_shot_record_count!(current_user)
2022-11-09 21:04:57 -05:00
socket
2022-11-09 21:04:57 -05:00
|> assign(
2023-03-29 22:54:55 -04:00
packs: packs,
original_counts: original_counts,
cprs: cprs,
last_used_dates: last_used_dates,
2022-11-09 21:04:57 -05:00
chart_data: chart_data,
2023-03-30 20:43:30 -04:00
shot_records: shot_records,
shot_record_count: shot_record_count
2022-11-09 21:04:57 -05:00
)
end
2023-03-30 20:43:30 -04:00
@spec get_chart_data_for_shot_record([ShotRecord.t()]) :: [map()]
defp get_chart_data_for_shot_record(shot_records) do
shot_records
2022-11-11 19:34:23 -05:00
|> 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-12-03 21:27:39 -05:00
|> Enum.sort_by(fn %{date: date} -> date end, Date)
2022-02-15 17:33:45 -05:00
end
end