shot groups to shot records

This commit is contained in:
2023-03-30 20:43:30 -04:00
parent 32801828fa
commit 5f8d1a917f
41 changed files with 585 additions and 707 deletions

View File

@ -1,10 +1,10 @@
defmodule CanneryWeb.RangeLive.FormComponent do
@moduledoc """
Livecomponent that can update a ShotGroup
Livecomponent that can update a ShotRecord
"""
use CanneryWeb, :live_component
alias Cannery.{Accounts.User, ActivityLog, ActivityLog.ShotGroup, Ammo, Ammo.Pack}
alias Cannery.{Accounts.User, ActivityLog, ActivityLog.ShotRecord, Ammo, Ammo.Pack}
alias Ecto.Changeset
alias Phoenix.LiveView.Socket
@ -14,7 +14,7 @@ defmodule CanneryWeb.RangeLive.FormComponent do
@impl true
@spec update(
%{
required(:shot_group) => ShotGroup.t(),
required(:shot_record) => ShotRecord.t(),
required(:current_user) => User.t(),
optional(:pack) => Pack.t(),
optional(any()) => any()
@ -23,7 +23,7 @@ defmodule CanneryWeb.RangeLive.FormComponent do
) :: {:ok, Socket.t()}
def update(
%{
shot_group: %ShotGroup{pack_id: pack_id},
shot_record: %ShotRecord{pack_id: pack_id},
current_user: current_user
} = assigns,
socket
@ -33,24 +33,24 @@ defmodule CanneryWeb.RangeLive.FormComponent do
{:ok, socket |> assign(assigns) |> assign(:pack, pack) |> assign_changeset(%{})}
end
def update(%{shot_group: %ShotGroup{}} = assigns, socket) do
def update(%{shot_record: %ShotRecord{}} = assigns, socket) do
{:ok, socket |> assign(assigns) |> assign_changeset(%{})}
end
@impl true
def handle_event("validate", %{"shot_group" => shot_group_params}, socket) do
{:noreply, socket |> assign_changeset(shot_group_params, :validate)}
def handle_event("validate", %{"shot_record" => shot_record_params}, socket) do
{:noreply, socket |> assign_changeset(shot_record_params, :validate)}
end
def handle_event(
"save",
%{"shot_group" => shot_group_params},
%{assigns: %{shot_group: shot_group, current_user: current_user, return_to: return_to}} =
%{"shot_record" => shot_record_params},
%{assigns: %{shot_record: shot_record, current_user: current_user, return_to: return_to}} =
socket
) do
socket =
case ActivityLog.update_shot_group(shot_group, shot_group_params, current_user) do
{:ok, _shot_group} ->
case ActivityLog.update_shot_record(shot_record, shot_record_params, current_user) do
{:ok, _shot_record} ->
prompt = dgettext("prompts", "Shot records updated successfully")
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
@ -67,22 +67,22 @@ defmodule CanneryWeb.RangeLive.FormComponent do
action: live_action,
current_user: user,
pack: pack,
shot_group: shot_group
shot_record: shot_record
}
} = socket,
shot_group_params,
shot_record_params,
action \\ nil
) do
default_action =
case live_action do
:add_shot_group -> :insert
editing when editing in [:edit, :edit_shot_group] -> :update
:add_shot_record -> :insert
editing when editing in [:edit, :edit_shot_record] -> :update
end
changeset =
case default_action do
:insert -> shot_group |> ShotGroup.create_changeset(user, pack, shot_group_params)
:update -> shot_group |> ShotGroup.update_changeset(user, shot_group_params)
:insert -> shot_record |> ShotRecord.create_changeset(user, pack, shot_record_params)
:update -> shot_record |> ShotRecord.update_changeset(user, shot_record_params)
end
changeset =

View File

@ -22,7 +22,7 @@
<%= label(f, :count, gettext("Shots fired"), class: "title text-lg text-primary-600") %>
<%= number_input(f, :count,
min: 1,
max: @shot_group.count + @pack.count,
max: @shot_record.count + @pack.count,
class: "input input-primary col-span-2"
) %>
<%= error_tag(f, :count, "col-span-3") %>

View File

@ -4,17 +4,17 @@ defmodule CanneryWeb.RangeLive.Index do
"""
use CanneryWeb, :live_view
alias Cannery.{ActivityLog, ActivityLog.ShotGroup, Ammo}
alias Cannery.{ActivityLog, ActivityLog.ShotRecord, Ammo}
alias CanneryWeb.Endpoint
alias Phoenix.LiveView.Socket
@impl true
def mount(%{"search" => search}, _session, socket) do
{:ok, socket |> assign(class: :all, search: search) |> display_shot_groups()}
{:ok, socket |> assign(class: :all, search: search) |> display_shot_records()}
end
def mount(_params, _session, socket) do
{:ok, socket |> assign(class: :all, search: nil) |> display_shot_groups()}
{:ok, socket |> assign(class: :all, search: nil) |> display_shot_records()}
end
@impl true
@ -24,7 +24,7 @@ defmodule CanneryWeb.RangeLive.Index do
defp apply_action(
%{assigns: %{current_user: current_user}} = socket,
:add_shot_group,
:add_shot_record,
%{"id" => id}
) do
socket
@ -38,7 +38,7 @@ defmodule CanneryWeb.RangeLive.Index do
socket
|> assign(
page_title: gettext("Edit Shot Records"),
shot_group: ActivityLog.get_shot_group!(id, current_user)
shot_record: ActivityLog.get_shot_record!(id, current_user)
)
end
@ -46,7 +46,7 @@ defmodule CanneryWeb.RangeLive.Index do
socket
|> assign(
page_title: gettext("New Shot Records"),
shot_group: %ShotGroup{}
shot_record: %ShotRecord{}
)
end
@ -55,9 +55,9 @@ defmodule CanneryWeb.RangeLive.Index do
|> assign(
page_title: gettext("Shot Records"),
search: nil,
shot_group: nil
shot_record: nil
)
|> display_shot_groups()
|> display_shot_records()
end
defp apply_action(socket, :search, %{"search" => search}) do
@ -65,19 +65,19 @@ defmodule CanneryWeb.RangeLive.Index do
|> assign(
page_title: gettext("Shot Records"),
search: search,
shot_group: nil
shot_record: nil
)
|> display_shot_groups()
|> display_shot_records()
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)
ActivityLog.get_shot_record!(id, current_user)
|> ActivityLog.delete_shot_record(current_user)
prompt = dgettext("prompts", "Shot records deleted succesfully")
{:noreply, socket |> put_flash(:info, prompt) |> display_shot_groups()}
{:noreply, socket |> put_flash(:info, prompt) |> display_shot_records()}
end
def handle_event(
@ -90,7 +90,7 @@ defmodule CanneryWeb.RangeLive.Index do
{:ok, _pack} = pack |> Ammo.update_pack(%{"staged" => !pack.staged}, current_user)
prompt = dgettext("prompts", "Ammo unstaged succesfully")
{:noreply, socket |> put_flash(:info, prompt) |> display_shot_groups()}
{:noreply, socket |> put_flash(:info, prompt) |> display_shot_records()}
end
def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
@ -102,28 +102,28 @@ defmodule CanneryWeb.RangeLive.Index do
end
def handle_event("change_class", %{"ammo_type" => %{"class" => "rifle"}}, socket) do
{:noreply, socket |> assign(:class, :rifle) |> display_shot_groups()}
{:noreply, socket |> assign(:class, :rifle) |> display_shot_records()}
end
def handle_event("change_class", %{"ammo_type" => %{"class" => "shotgun"}}, socket) do
{:noreply, socket |> assign(:class, :shotgun) |> display_shot_groups()}
{:noreply, socket |> assign(:class, :shotgun) |> display_shot_records()}
end
def handle_event("change_class", %{"ammo_type" => %{"class" => "pistol"}}, socket) do
{:noreply, socket |> assign(:class, :pistol) |> display_shot_groups()}
{:noreply, socket |> assign(:class, :pistol) |> display_shot_records()}
end
def handle_event("change_class", %{"ammo_type" => %{"class" => _all}}, socket) do
{:noreply, socket |> assign(:class, :all) |> display_shot_groups()}
{:noreply, socket |> assign(:class, :all) |> display_shot_records()}
end
@spec display_shot_groups(Socket.t()) :: Socket.t()
defp display_shot_groups(
@spec display_shot_records(Socket.t()) :: Socket.t()
defp display_shot_records(
%{assigns: %{class: class, search: search, current_user: current_user}} = socket
) do
shot_groups = ActivityLog.list_shot_groups(search, class, current_user)
shot_records = ActivityLog.list_shot_records(search, class, current_user)
packs = Ammo.list_staged_packs(current_user)
chart_data = shot_groups |> get_chart_data_for_shot_group()
chart_data = shot_records |> get_chart_data_for_shot_record()
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)
@ -136,14 +136,14 @@ defmodule CanneryWeb.RangeLive.Index do
cprs: cprs,
last_used_dates: last_used_dates,
chart_data: chart_data,
shot_groups: shot_groups,
shot_records: shot_records,
shot_record_count: shot_record_count
)
end
@spec get_chart_data_for_shot_group([ShotGroup.t()]) :: [map()]
defp get_chart_data_for_shot_group(shot_groups) do
shot_groups
@spec get_chart_data_for_shot_record([ShotRecord.t()]) :: [map()]
defp get_chart_data_for_shot_record(shot_records) do
shot_records
|> Enum.group_by(fn %{date: date} -> date end, fn %{count: count} -> count end)
|> Enum.map(fn {date, rounds} ->
sum = Enum.sum(rounds)

View File

@ -39,7 +39,7 @@
</button>
<.link
patch={Routes.range_index_path(Endpoint, :add_shot_group, pack)}
patch={Routes.range_index_path(Endpoint, :add_shot_record, pack)}
class="btn btn-primary"
>
<%= dgettext("actions", "Record shots") %>
@ -117,26 +117,26 @@
</.form>
</div>
<%= if @shot_groups |> Enum.empty?() do %>
<%= if @shot_records |> Enum.empty?() do %>
<h1 class="title text-xl text-primary-600">
<%= gettext("No shots recorded") %>
<%= display_emoji("😔") %>
</h1>
<% else %>
<.live_component
module={CanneryWeb.Components.ShotGroupTableComponent}
id="shot_groups_index_table"
shot_groups={@shot_groups}
module={CanneryWeb.Components.ShotRecordTableComponent}
id="shot_records_index_table"
shot_records={@shot_records}
current_user={@current_user}
>
<:actions :let={shot_group}>
<:actions :let={shot_record}>
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
<.link
patch={Routes.range_index_path(Endpoint, :edit, shot_group)}
patch={Routes.range_index_path(Endpoint, :edit, shot_record)}
class="text-primary-600 link"
aria-label={
dgettext("actions", "Edit shot record of %{shot_group_count} shots",
shot_group_count: shot_group.count
dgettext("actions", "Edit shot record of %{shot_record_count} shots",
shot_record_count: shot_record.count
)
}
>
@ -147,13 +147,13 @@
href="#"
class="text-primary-600 link"
phx-click="delete"
phx-value-id={shot_group.id}
phx-value-id={shot_record.id}
data-confirm={
dgettext("prompts", "Are you sure you want to delete this shot record?")
}
aria-label={
dgettext("actions", "Delete shot record of %{shot_group_count} shots",
shot_group_count: shot_group.count
dgettext("actions", "Delete shot record of %{shot_record_count} shots",
shot_record_count: shot_record.count
)
}
>
@ -171,18 +171,18 @@
<.modal return_to={Routes.range_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.RangeLive.FormComponent}
id={@shot_group.id}
id={@shot_record.id}
title={@page_title}
action={@live_action}
shot_group={@shot_group}
shot_record={@shot_record}
return_to={Routes.range_index_path(Endpoint, :index)}
current_user={@current_user}
/>
</.modal>
<% :add_shot_group -> %>
<% :add_shot_record -> %>
<.modal return_to={Routes.range_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.Components.AddShotGroupComponent}
module={CanneryWeb.Components.AddShotRecordComponent}
id={:new}
title={@page_title}
action={@live_action}