add range mode

This commit is contained in:
2022-02-15 17:33:45 -05:00
parent d9dd61b1a5
commit e4ef22184e
30 changed files with 1394 additions and 132 deletions

View File

@ -0,0 +1,64 @@
defmodule CanneryWeb.RangeLive.FormComponent do
@moduledoc """
Livecomponent that can update or create a ShotGroup
"""
use CanneryWeb, :live_component
alias Cannery.{Accounts.User, ActivityLog, ActivityLog.ShotGroup, Ammo, Ammo.AmmoGroup}
alias Phoenix.LiveView.Socket
@impl true
@spec update(
%{
required(:shot_group) => ShotGroup.t(),
required(:current_user) => User.t(),
optional(:ammo_group) => AmmoGroup.t(),
optional(any()) => any()
},
Socket.t()
) :: {:ok, Socket.t()}
def update(
%{
shot_group: %ShotGroup{ammo_group_id: ammo_group_id} = shot_group,
current_user: current_user
} = assigns,
socket
) do
changeset = shot_group |> ActivityLog.change_shot_group()
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
{:ok, socket |> assign(assigns) |> assign(ammo_group: ammo_group, changeset: changeset)}
end
@impl true
def handle_event(
"validate",
%{"shot_group" => shot_group_params},
%{assigns: %{shot_group: shot_group}} = socket
) do
changeset =
shot_group
|> ActivityLog.change_shot_group(shot_group_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event(
"save",
%{"shot_group" => shot_group_params},
%{assigns: %{shot_group: shot_group, 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} ->
prompt = dgettext("prompts", "Shot records updated successfully")
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Ecto.Changeset{} = changeset} ->
socket |> assign(:changeset, changeset)
end
{:noreply, socket}
end
end

View File

@ -0,0 +1,45 @@
<div>
<h2 class="text-center title text-xl text-primary-500">
<%= @title %>
</h2>
<.form
let={f}
for={@changeset}
id="shot-group-form"
class="grid grid-cols-3 justify-center items-center space-y-4"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<%= unless @changeset.valid? do %>
<div class="invalid-feedback col-span-3 text-center">
<%= changeset_errors(@changeset) %>
</div>
<% end %>
<%= label(f, :count, gettext("Shots fired"), class: "title text-lg text-primary-500") %>
<%= number_input(f, :count,
min: 1,
max: @shot_group.count + @ammo_group.count,
class: "input input-primary col-span-2"
) %>
<%= error_tag(f, :count, "col-span-3") %>
<%= label(f, :notes, gettext("Notes"), class: "title text-lg text-primary-500") %>
<%= textarea(f, :notes,
class: "input input-primary col-span-2",
phx_hook: "MaintainAttrs"
) %>
<%= error_tag(f, :notes, "col-span-3") %>
<%= label(f, :date, gettext("Date (UTC)"), class: "title text-lg text-primary-500") %>
<%= date_input(f, :date, class: "input input-primary col-span-2") %>
<%= error_tag(f, :notes, "col-span-3") %>
<%= submit(dgettext("actions", "Save"),
class: "mx-auto btn btn-primary col-span-3",
phx_disable_with: dgettext("prompts", "Saving...")
) %>
</.form>
</div>

View File

@ -0,0 +1,82 @@
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
def mount(_params, session, socket) do
{:ok, socket |> assign_defaults(session) |> display_shot_groups()}
end
@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
|> assign(:page_title, gettext("Record shots"))
|> 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)
prompt = dgettext("prompts", "Ammo group unstaged succesfully")
{: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)
socket |> assign(shot_groups: shot_groups, ammo_groups: ammo_groups)
end
end

View File

@ -0,0 +1,135 @@
<div class="mx-8 flex flex-col space-y-8 justify-center items-center">
<h1 class="title text-2xl title-primary-500">
<%= gettext("Range day") %>
</h1>
<%= if @ammo_groups |> Enum.empty?() do %>
<h1 class="title text-xl text-primary-500">
<%= gettext("No ammo staged") %> 😔
</h1>
<%= live_patch(dgettext("actions", "Why not get some ready to shoot?"),
to: Routes.ammo_group_index_path(Endpoint, :index),
class: "btn btn-primary"
) %>
<% else %>
<%= live_patch(dgettext("actions", "Stage ammo"),
to: Routes.ammo_group_index_path(Endpoint, :index),
class: "btn btn-primary"
) %>
<%= for ammo_group <- @ammo_groups do %>
<.ammo_group_card ammo_group={ammo_group}>
<button
type="button"
class="btn btn-primary"
phx-click="toggle_staged"
phx-value-ammo_group_id={ammo_group.id}
data-confirm={"#{dgettext("prompts", "Are you sure you want to unstage this ammo?")}"}
>
<%= if ammo_group.staged, do: gettext("Unstage from range"), else: gettext("Stage for range") %>
</button>
<%= live_patch(dgettext("actions", "Record shots"),
to: Routes.range_index_path(Endpoint, :add_shot_group, ammo_group),
class: "btn btn-primary"
) %>
</.ammo_group_card>
<% end %>
<% end %>
<hr class="hr">
<%= if @shot_groups |> Enum.empty?() do %>
<h1 class="title text-xl text-primary-500">
<%= gettext("No shots recorded") %> 😔
</h1>
<% else %>
<div class="w-full overflow-x-auto border border-gray-600 rounded-lg shadow-lg bg-black">
<table class="min-w-full table-auto text-center bg-white">
<thead class="border-b border-primary-600">
<tr>
<th class="p-2">
<%= gettext("Ammo") %>
</th>
<th class="p-2">
<%= gettext("Rounds shot") %>
</th>
<th class="p-2">
<%= gettext("Notes") %>
</th>
<th class="p-2">
<%= gettext("Date") %>
</th>
<th class="p-2"></th>
</tr>
</thead>
<tbody id="shot_groups">
<%= for shot_group <- @shot_groups do %>
<tr id={"shot_group-#{shot_group.id}"}>
<td class="p-2">
<%= live_patch(shot_group.ammo_group.ammo_type.name,
to: Routes.ammo_group_show_path(Endpoint, :show, shot_group.ammo_group),
class: "link"
) %>
</td>
<td class="p-2">
<%= shot_group.count %>
</td>
<td class="p-2">
<%= shot_group.notes %>
</td>
<td class="p-2">
<%= shot_group.date |> display_date() %>
</td>
<td class="p-2 w-full h-full space-x-2 flex justify-center items-center">
<%= live_patch to: Routes.range_index_path(Endpoint, :edit, shot_group),
class: "text-primary-500 link" do %>
<i class="fa-fw fa-lg fas fa-edit"></i>
<% end %>
<%= link to: "#",
class: "text-primary-500 link",
phx_click: "delete",
phx_value_id: shot_group.id,
data: [confirm: dgettext("prompts", "Are you sure you want to delete this shot record?")] do %>
<i class="fa-fw fa-lg fas fa-trash"></i>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% end %>
</div>
<%= if @live_action in [:edit] do %>
<.modal return_to={Routes.range_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.RangeLive.FormComponent}
id={@shot_group.id}
title={@page_title}
action={@live_action}
shot_group={@shot_group}
return_to={Routes.range_index_path(Endpoint, :index)}
current_user={@current_user}
/>
</.modal>
<% end %>
<%= if @live_action in [:add_shot_group] do %>
<.modal return_to={Routes.range_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.Components.AddShotGroupComponent}
id={:new}
title={@page_title}
action={@live_action}
ammo_group={@ammo_group}
return_to={Routes.range_index_path(Endpoint, :index)}
current_user={@current_user}
/>
</.modal>
<% end %>