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

@ -4,8 +4,8 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
"""
use CanneryWeb, :live_view
alias Cannery.Ammo
alias Cannery.Ammo.AmmoGroup
alias Cannery.{Ammo, Ammo.AmmoGroup, Repo}
alias CanneryWeb.Endpoint
@impl true
def mount(_params, session, socket) do
@ -42,7 +42,22 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
{:noreply, socket |> put_flash(:info, prompt) |> display_ammo_groups()}
end
@impl true
def handle_event(
"toggle_staged",
%{"ammo_group_id" => id},
%{assigns: %{current_user: current_user}} = socket
) do
ammo_group = Ammo.get_ammo_group!(id, current_user)
{:ok, _ammo_group} =
ammo_group |> Ammo.update_ammo_group(%{"staged" => !ammo_group.staged}, current_user)
{:noreply, socket |> display_ammo_groups()}
end
defp display_ammo_groups(%{assigns: %{current_user: current_user}} = socket) do
socket |> assign(:ammo_groups, Ammo.list_ammo_groups(current_user))
ammo_groups = Ammo.list_ammo_groups(current_user) |> Repo.preload([:ammo_type, :container])
socket |> assign(:ammo_groups, ammo_groups)
end
end

View File

@ -22,6 +22,9 @@
<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 type") %>
</th>
<th class="p-2">
<%= gettext("Count") %>
</th>
@ -31,6 +34,12 @@
<th class="p-2">
<%= gettext("Notes") %>
</th>
<th class="p-2">
<%= gettext("Staging") %>
</th>
<th class="p-2">
<%= gettext("Container") %>
</th>
<th class="p-2"></th>
</tr>
@ -38,6 +47,13 @@
<tbody id="ammo_groups">
<%= for ammo_group <- @ammo_groups do %>
<tr id={"ammo_group-#{ammo_group.id}"}>
<td class="p-2">
<%= live_patch(ammo_group.ammo_type.name,
to: Routes.ammo_type_show_path(Endpoint, :show, ammo_group.ammo_type),
class: "link"
) %>
</td>
<td class="p-2">
<%= ammo_group.count %>
</td>
@ -52,23 +68,41 @@
<%= ammo_group.notes %>
</td>
<td class="p-2">
<button
type="button"
class="btn btn-primary"
phx-click="toggle_staged"
phx-value-ammo_group_id={ammo_group.id}
>
<%= if ammo_group.staged, do: gettext("Unstage from range"), else: gettext("Stage for range") %>
</button>
</td>
<td class="p-2">
<%= if ammo_group.container, do: ammo_group.container.name %>
</td>
<td class="p-2 w-full h-full space-x-2 flex justify-center items-center">
<%= live_redirect(dgettext("actions", "View"),
to: Routes.ammo_group_show_path(@socket, :show, ammo_group)
) %>
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
<%= live_redirect to: Routes.ammo_group_show_path(@socket, :show, ammo_group),
class: "text-primary-500 link" do %>
<i class="fa-fw fa-lg fas fa-eye"></i>
<% end %>
<%= live_patch to: Routes.ammo_group_index_path(@socket, :edit, ammo_group),
class: "text-primary-500 link" do %>
<i class="fa-fw fa-lg fas fa-edit"></i>
<% end %>
<%= live_patch to: Routes.ammo_group_index_path(@socket, :edit, ammo_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: ammo_group.id,
data: [confirm: dgettext("prompts", "Are you sure you want to delete this ammo?")] do %>
<i class="fa-fw fa-lg fas fa-trash"></i>
<% end %>
<%= link to: "#",
class: "text-primary-500 link",
phx_click: "delete",
phx_value_id: ammo_group.id,
data: [confirm: dgettext("prompts", "Are you sure you want to delete this ammo?")] do %>
<i class="fa-fw fa-lg fas fa-trash"></i>
<% end %>
</div>
</td>
</tr>
<% end %>
@ -79,14 +113,28 @@
</div>
<%= if @live_action in [:new, :edit] do %>
<.modal return_to={Routes.ammo_group_index_path(@socket, :index)}>
<.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.AmmoGroupLive.FormComponent}
id={@ammo_group.id || :new}
title={@page_title}
action={@live_action}
ammo_group={@ammo_group}
return_to={Routes.ammo_group_index_path(@socket, :index)}
return_to={Routes.ammo_group_index_path(Endpoint, :index)}
current_user={@current_user}
/>
</.modal>
<% end %>
<%= if @live_action in [:add_shot_group] do %>
<.modal return_to={Routes.ammo_group_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.ammo_group_index_path(Endpoint, :index)}
current_user={@current_user}
/>
</.modal>

View File

@ -6,6 +6,7 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
use CanneryWeb, :live_view
import CanneryWeb.Components.ContainerCard
alias Cannery.{Ammo, Repo}
alias CanneryWeb.Endpoint
@impl true
def mount(_params, session, socket) do
@ -13,11 +14,26 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
end
@impl true
def handle_params(
%{"id" => id},
_,
%{assigns: %{live_action: live_action, current_user: current_user}} = socket
) do
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
socket |> assign(page_title: page_title(live_action)) |> apply_action(live_action, params)
end
defp apply_action(
%{assigns: %{current_user: current_user}} = socket,
:add_shot_group,
%{"id" => id}
) do
socket
|> assign(:page_title, gettext("Add Shot group"))
|> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
end
defp apply_action(
%{assigns: %{live_action: live_action, current_user: current_user}} = socket,
action,
%{"id" => id}
)
when action == :edit or action == :show do
ammo_group = Ammo.get_ammo_group!(id, current_user) |> Repo.preload([:container, :ammo_type])
{:noreply, socket |> assign(page_title: page_title(live_action), ammo_group: ammo_group)}
end
@ -36,6 +52,18 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
{:noreply, socket |> put_flash(:info, prompt) |> push_redirect(to: redirect_to)}
end
@impl true
def handle_event(
"toggle_staged",
_,
%{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket
) do
{:ok, ammo_group} =
ammo_group |> Ammo.update_ammo_group(%{"staged" => !ammo_group.staged}, current_user)
{:noreply, socket |> assign(ammo_group: ammo_group)}
end
defp page_title(:show), do: gettext("Show Ammo group")
defp page_title(:edit), do: gettext("Edit Ammo group")
end

View File

@ -24,6 +24,11 @@
</div>
<div class="flex space-x-4 justify-center items-center text-primary-500">
<%= live_patch(dgettext("actions", "Ammo Details"),
to: Routes.ammo_type_show_path(@socket, :show, @ammo_group.ammo_type),
class: "btn btn-primary"
) %>
<%= live_patch to: Routes.ammo_group_show_path(@socket, :edit, @ammo_group),
class: "text-primary-500 link" do %>
<i class="fa-fw fa-lg fas fa-edit"></i>
@ -35,6 +40,10 @@
data: [confirm: dgettext("prompts", "Are you sure you want to delete this ammo?")] do %>
<i class="fa-fw fa-lg fas fa-trash"></i>
<% end %>
<button type="button" class="btn btn-primary" phx-click="toggle_staged">
<%= if @ammo_group.staged, do: gettext("Unstage from range"), else: gettext("Stage for range") %>
</button>
</div>
<hr class="mb-4 w-full">
@ -53,14 +62,28 @@
</div>
<%= if @live_action in [:edit] do %>
<.modal return_to={Routes.ammo_group_show_path(@socket, :show, @ammo_group)}>
<.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}>
<.live_component
module={CanneryWeb.AmmoGroupLive.FormComponent}
id={@ammo_group.id}
title={@page_title}
action={@live_action}
ammo_group={@ammo_group}
return_to={Routes.ammo_group_show_path(@socket, :show, @ammo_group)}
return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}
current_user={@current_user}
/>
</.modal>
<% end %>
<%= if @live_action in [:add_shot_group] do %>
<.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}>
<.live_component
module={CanneryWeb.Components.AddShotGroupComponent}
id={:new}
title={@page_title}
action={@live_action}
ammo_group={@ammo_group}
return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}
current_user={@current_user}
/>
</.modal>