move staging to container

This commit is contained in:
2025-01-31 23:44:11 -05:00
parent 2e6e26006d
commit 3eda522903
48 changed files with 948 additions and 899 deletions

View File

@ -4,6 +4,7 @@ defmodule CanneryWeb.Components.ContainerTableComponent do
"""
use CanneryWeb, :live_component
alias Cannery.{Accounts.User, Ammo, Containers.Container}
alias CanneryWeb.Components.TableComponent
alias Ecto.UUID
alias Phoenix.LiveView.{Rendered, Socket}
@ -13,6 +14,7 @@ defmodule CanneryWeb.Components.ContainerTableComponent do
required(:id) => UUID.t(),
required(:current_user) => User.t(),
optional(:containers) => [Container.t()],
optional(:range) => Rendered.t(),
optional(:tag_actions) => Rendered.t(),
optional(:actions) => Rendered.t(),
optional(any()) => any()
@ -23,6 +25,7 @@ defmodule CanneryWeb.Components.ContainerTableComponent do
socket =
socket
|> assign(assigns)
|> assign_new(:range, fn -> [] end)
|> assign_new(:tag_actions, fn -> [] end)
|> assign_new(:actions, fn -> [] end)
|> display_containers()
@ -35,6 +38,7 @@ defmodule CanneryWeb.Components.ContainerTableComponent do
assigns: %{
containers: containers,
current_user: current_user,
range: range,
tag_actions: tag_actions,
actions: actions
}
@ -62,13 +66,22 @@ defmodule CanneryWeb.Components.ContainerTableComponent do
end)
|> Enum.concat([
%{label: gettext("Packs"), key: :packs, type: :integer},
%{label: gettext("Rounds"), key: :rounds, type: :integer},
%{label: gettext("Tags"), key: :tags, type: :tags},
%{label: gettext("Actions"), key: :actions, sortable: false, type: :actions}
%{label: gettext("Rounds"), key: :rounds, type: :integer}
])
|> Enum.concat(
[
%{label: gettext("Tags"), key: :tags, type: :tags},
%{label: gettext("Actions"), key: :actions, sortable: false, type: :actions}
]
|> TableComponent.maybe_compose_columns(
%{label: gettext("Range"), key: :range},
range != []
)
)
extra_data = %{
current_user: current_user,
range: range,
tag_actions: tag_actions,
actions: actions,
pack_count:
@ -136,6 +149,15 @@ defmodule CanneryWeb.Components.ContainerTableComponent do
round_count |> Map.get(container_id, 0)
end
defp get_value_for_key(:range, %{staged: staged} = container, %{range: range}) do
assigns = %{range: range, container: container}
{staged,
~H"""
<%= render_slot(@range, @container) %>
"""}
end
defp get_value_for_key(:tags, container, %{tag_actions: tag_actions}) do
assigns = %{tag_actions: tag_actions, container: container}

View File

@ -201,13 +201,12 @@ defmodule CanneryWeb.Components.PackTableComponent do
"""}
end
defp get_value_for_key(:range, %{staged: staged} = pack, %{range: range}) do
defp get_value_for_key(:range, pack, %{range: range}) do
assigns = %{range: range, pack: pack}
{staged,
~H"""
<%= render_slot(@range, @pack) %>
"""}
~H"""
<%= render_slot(@range, @pack) %>
"""
end
defp get_value_for_key(

View File

@ -112,6 +112,20 @@ defmodule CanneryWeb.ContainerLive.Index do
{:noreply, socket |> push_patch(to: ~p"/containers/search/#{search_term}")}
end
def handle_event(
"toggle_staged",
%{"container_id" => id},
%{assigns: %{current_user: current_user}} = socket
) do
container = Containers.get_container!(id, current_user)
{:ok, _container} =
container
|> Containers.update_container(current_user, %{"staged" => !container.staged})
{:noreply, socket |> display_containers()}
end
defp display_containers(%{assigns: %{search: search, current_user: current_user}} = socket) do
socket |> assign(:containers, Containers.list_containers(current_user, search: search))
end

View File

@ -1,10 +1,10 @@
<div class="flex flex-col space-y-8 justify-center items-center">
<h1 class="title text-2xl title-primary-500">
<div class="flex flex-col justify-center items-center space-y-8">
<h1 class="text-2xl title title-primary-500">
<%= gettext("Containers") %>
</h1>
<%= if @containers |> Enum.empty?() and @search |> is_nil() do %>
<h2 class="title text-xl text-primary-600">
<h2 class="text-xl title text-primary-600">
<%= gettext("No containers") %>
<%= display_emoji("😔") %>
</h2>
@ -17,14 +17,14 @@
<%= dgettext("actions", "New Container") %>
</.link>
<div class="w-full flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 max-w-2xl">
<div class="flex flex-col justify-center items-center space-y-4 w-full max-w-2xl sm:flex-row sm:space-y-0 sm:space-x-4">
<.form
:let={f}
for={%{}}
as={:search}
phx-change="search"
phx-submit="search"
class="grow flex items-center"
class="flex items-center grow"
>
<%= text_input(f, :search_term,
class: "grow input input-primary",
@ -36,14 +36,14 @@
</.form>
<.toggle_button action="toggle_table" value={@view_table}>
<span class="title text-lg text-primary-600">
<span class="text-lg title text-primary-600">
<%= gettext("View as table") %>
</span>
</.toggle_button>
</div>
<%= if @containers |> Enum.empty?() do %>
<h2 class="title text-xl text-primary-600">
<h2 class="text-xl title text-primary-600">
<%= gettext("No containers") %>
<%= display_emoji("😔") %>
</h2>
@ -56,6 +56,20 @@
containers={@containers}
current_user={@current_user}
>
<:range :let={container}>
<div class="flex justify-center items-center px-4 py-2 h-full min-w-20 flex-wrap">
<button
type="button"
class="mx-2 my-1 text-sm btn btn-primary"
phx-click="toggle_staged"
phx-value-container_id={container.id}
>
<%= if container.staged,
do: dgettext("actions", "Unstage"),
else: dgettext("actions", "Stage") %>
</button>
</div>
</:range>
<:tag_actions :let={container}>
<div class="mx-4 my-2">
<.link
@ -109,7 +123,7 @@
</:actions>
</.live_component>
<% else %>
<div class="w-full flex flex-row flex-wrap justify-center items-stretch">
<div class="flex flex-row flex-wrap justify-center items-stretch w-full">
<.container_card
:for={container <- @containers}
container={container}

View File

@ -78,6 +78,18 @@ defmodule CanneryWeb.ContainerLive.Show do
{:noreply, socket}
end
def handle_event(
"toggle_staged",
_params,
%{assigns: %{container: container, current_user: current_user}} = socket
) do
{:ok, _container} =
container
|> Containers.update_container(current_user, %{"staged" => !container.staged})
{:noreply, socket |> render_container()}
end
def handle_event("toggle_table", _params, %{assigns: %{view_table: view_table}} = socket) do
{:noreply, socket |> assign(:view_table, !view_table) |> render_container()}
end

View File

@ -1,34 +1,34 @@
<div class="space-y-4 flex flex-col justify-center items-center">
<h1 class="title text-2xl title-primary-500">
<div class="flex flex-col justify-center items-center space-y-4">
<h1 class="text-2xl title title-primary-500">
<%= @container.name %>
</h1>
<span :if={@container.desc} class="rounded-lg title text-lg">
<span :if={@container.desc} class="text-lg rounded-lg title">
<%= gettext("Description:") %>
<%= @container.desc %>
</span>
<span class="rounded-lg title text-lg">
<span class="text-lg rounded-lg title">
<%= gettext("Type:") %>
<%= @container.type %>
</span>
<span :if={@container.location} class="rounded-lg title text-lg">
<span :if={@container.location} class="text-lg rounded-lg title">
<%= gettext("Location:") %>
<%= @container.location %>
</span>
<span class="rounded-lg title text-lg">
<span class="text-lg rounded-lg title">
<%= gettext("Packs:") %>
<%= @packs_count %>
</span>
<span class="rounded-lg title text-lg">
<span class="text-lg rounded-lg title">
<%= gettext("Rounds:") %>
<%= @round_count %>
</span>
<div class="flex space-x-4 justify-center items-center text-primary-600">
<div class="flex justify-center items-center space-x-4 text-primary-600">
<.link
patch={~p"/container/edit/#{@container}"}
class="text-primary-600 link"
@ -52,11 +52,19 @@
</.link>
</div>
<div class="flex flex-wrap justify-center items-center text-primary-600">
<button type="button" class="mx-4 my-2 btn btn-primary" phx-click="toggle_staged">
<%= if @container.staged,
do: dgettext("actions", "Unstage from range"),
else: dgettext("actions", "Stage for range") %>
</button>
</div>
<hr class="mb-4 hr" />
<%= if @container.tags |> Enum.empty?() do %>
<div class="flex flex-row justify-center items-center space-x-4">
<h2 class="title text-lg text-primary-600">
<h2 class="text-lg title text-primary-600">
<%= gettext("No tags for this container") %>
<%= display_emoji("😔") %>
</h2>
@ -105,15 +113,15 @@
</.form>
<.toggle_button action="toggle_table" value={@view_table}>
<span class="title text-lg text-primary-600">
<span class="text-lg title text-primary-600">
<%= gettext("View as table") %>
</span>
</.toggle_button>
</div>
<div class="w-full p-4">
<div class="p-4 w-full">
<%= if @packs |> Enum.empty?() do %>
<h2 class="mx-4 title text-lg text-primary-600 text-center">
<h2 class="mx-4 text-lg text-center title text-primary-600">
<%= gettext("No ammo in this container") %>
</h2>
<% else %>
@ -131,7 +139,7 @@
</.link>
</:type>
<:actions :let={%{count: pack_count} = pack}>
<div class="py-2 px-4 h-full space-x-4 flex justify-center items-center">
<div class="flex justify-center items-center px-4 py-2 space-x-4 h-full">
<.link
navigate={~p"/ammo/show/#{pack}"}
class="text-primary-600 link"

View File

@ -96,18 +96,6 @@ defmodule CanneryWeb.PackLive.Index do
{:noreply, socket |> put_flash(:info, prompt) |> display_packs()}
end
def handle_event(
"toggle_staged",
%{"pack_id" => id},
%{assigns: %{current_user: current_user}} = socket
) do
pack = Ammo.get_pack!(id, current_user)
{:ok, _pack} = pack |> Ammo.update_pack(%{"staged" => !pack.staged}, current_user)
{:noreply, socket |> display_packs()}
end
def handle_event("toggle_show_used", _params, %{assigns: %{show_used: show_used}} = socket) do
{:noreply, socket |> assign(:show_used, !show_used) |> display_packs()}
end

View File

@ -1,5 +1,5 @@
<div class="flex flex-col space-y-8 justify-center items-center">
<h1 class="title text-2xl title-primary-500">
<div class="flex flex-col justify-center items-center space-y-8">
<h1 class="text-2xl title title-primary-500">
<%= gettext("Ammo") %>
</h1>
@ -25,7 +25,7 @@
</.link>
</div>
<% @packs_count == 0 -> %>
<h2 class="title text-xl text-primary-600">
<h2 class="text-xl title text-primary-600">
<%= gettext("No ammo") %>
<%= display_emoji("😔") %>
</h2>
@ -38,7 +38,7 @@
<%= dgettext("actions", "Add Ammo") %>
</.link>
<div class="w-full flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 max-w-2xl">
<div class="flex flex-col justify-center items-center space-y-4 w-full max-w-2xl sm:flex-row sm:space-y-0 sm:space-x-4">
<.form
:let={f}
for={%{}}
@ -71,7 +71,7 @@
as={:search}
phx-change="search"
phx-submit="search"
class="grow flex items-center"
class="flex items-center grow"
>
<%= text_input(f, :search_term,
class: "grow input input-primary",
@ -83,14 +83,14 @@
</.form>
<.toggle_button action="toggle_show_used" value={@show_used}>
<span class="title text-lg text-primary-600">
<span class="text-lg title text-primary-600">
<%= gettext("Show used") %>
</span>
</.toggle_button>
</div>
<%= if @packs |> Enum.empty?() do %>
<h2 class="title text-xl text-primary-600">
<h2 class="text-xl title text-primary-600">
<%= gettext("No Ammo") %>
<%= display_emoji("😔") %>
</h2>
@ -108,18 +108,7 @@
</.link>
</:type>
<:range :let={pack}>
<div class="min-w-20 py-2 px-4 h-full flex flew-wrap justify-center items-center">
<button
type="button"
class="mx-2 my-1 text-sm btn btn-primary"
phx-click="toggle_staged"
phx-value-pack_id={pack.id}
>
<%= if pack.staged,
do: dgettext("actions", "Unstage"),
else: dgettext("actions", "Stage") %>
</button>
<div class="flex flex-wrap justify-center items-center px-4 py-2 h-full min-w-20">
<.link
patch={~p"/ammo/add_shot_record/#{pack}"}
class="mx-2 my-1 text-sm btn btn-primary"
@ -129,7 +118,7 @@
</div>
</:range>
<:container :let={{pack, %{name: container_name} = container}}>
<div class="min-w-20 py-2 px-4 h-full flex flew-wrap justify-center items-center">
<div class="flex flex-wrap justify-center items-center px-4 py-2 h-full min-w-20">
<.link navigate={~p"/container/#{container}"} class="mx-2 my-1 link">
<%= container_name %>
</.link>
@ -140,7 +129,7 @@
</div>
</:container>
<:actions :let={%{count: pack_count} = pack}>
<div class="py-2 px-4 h-full space-x-4 flex justify-center items-center">
<div class="flex justify-center items-center px-4 py-2 space-x-4 h-full">
<.link
navigate={~p"/ammo/show/#{pack}"}
class="text-primary-600 link"

View File

@ -57,16 +57,6 @@ defmodule CanneryWeb.PackLive.Show do
{:noreply, socket |> put_flash(:info, prompt) |> push_navigate(to: redirect_to)}
end
def handle_event(
"toggle_staged",
_params,
%{assigns: %{pack: pack, current_user: current_user}} = socket
) do
{:ok, pack} = pack |> Ammo.update_pack(%{"staged" => !pack.staged}, current_user)
{:noreply, socket |> display_pack(pack)}
end
def handle_event(
"delete_shot_record",
%{"id" => id},

View File

@ -1,43 +1,43 @@
<div class="mx-auto space-y-4 max-w-3xl flex flex-col justify-center items-center">
<h1 class="title text-2xl title-primary-500">
<div class="flex flex-col justify-center items-center mx-auto space-y-4 max-w-3xl">
<h1 class="text-2xl title title-primary-500">
<%= @pack.type.name %>
</h1>
<div class="space-y-2 flex flex-col justify-center items-center">
<span class="rounded-lg title text-lg">
<div class="flex flex-col justify-center items-center space-y-2">
<span class="text-lg rounded-lg title">
<%= gettext("Count:") %>
<%= @pack.count %>
</span>
<span class="rounded-lg title text-lg">
<span class="text-lg rounded-lg title">
<%= gettext("Original count:") %>
<%= @original_count %>
</span>
<span class="rounded-lg title text-lg">
<span class="text-lg rounded-lg title">
<%= gettext("Percentage left:") %>
<%= gettext("%{percentage}%", percentage: @percentage_remaining) %>
</span>
<%= if @pack.notes do %>
<span class="rounded-lg title text-lg">
<span class="text-lg rounded-lg title">
<%= gettext("Notes:") %>
<%= @pack.notes %>
</span>
<% end %>
<span class="rounded-lg title text-lg">
<span class="text-lg rounded-lg title">
<%= gettext("Purchased on:") %>
<.date id={"#{@pack.id}-purchased-on"} date={@pack.purchased_on} />
</span>
<%= if @pack.price_paid do %>
<span class="rounded-lg title text-lg">
<span class="text-lg rounded-lg title">
<%= gettext("Original cost:") %>
<%= gettext("$%{amount}", amount: display_currency(@pack.price_paid)) %>
</span>
<span class="rounded-lg title text-lg">
<span class="text-lg rounded-lg title">
<%= gettext("Current value:") %>
<%= gettext("$%{amount}",
amount: display_currency(@pack.price_paid * @percentage_remaining / 100)
@ -76,12 +76,6 @@
</div>
<div class="flex flex-wrap justify-center items-center text-primary-600">
<button type="button" class="mx-4 my-2 btn btn-primary" phx-click="toggle_staged">
<%= if @pack.staged,
do: dgettext("actions", "Unstage from range"),
else: dgettext("actions", "Stage for range") %>
</button>
<.link patch={~p"/ammo/show/move/#{@pack}"} class="btn btn-primary">
<%= dgettext("actions", "Move ammo") %>
</.link>
@ -96,7 +90,7 @@
<div>
<%= if @container do %>
<h1 class="mb-4 px-4 py-2 text-center rounded-lg title text-xl">
<h1 class="px-4 py-2 mb-4 text-xl text-center rounded-lg title">
<%= gettext("Stored in") %>
</h1>
@ -109,7 +103,7 @@
<%= unless @shot_records |> Enum.empty?() do %>
<hr class="mb-4 w-full" />
<h1 class="mb-4 px-4 py-2 text-center rounded-lg title text-xl">
<h1 class="px-4 py-2 mb-4 text-xl text-center rounded-lg title">
<%= gettext("Rounds used") %>
</h1>

View File

@ -4,7 +4,8 @@ defmodule CanneryWeb.RangeLive.Index do
"""
use CanneryWeb, :live_view
alias Cannery.{ActivityLog, ActivityLog.ShotRecord, Ammo}
alias Cannery.{ActivityLog, ActivityLog.ShotRecord}
alias Cannery.{Ammo, Containers}
alias Phoenix.LiveView.Socket
@impl true
@ -101,14 +102,16 @@ defmodule CanneryWeb.RangeLive.Index do
def handle_event(
"toggle_staged",
%{"pack_id" => pack_id},
%{"container_id" => container_id},
%{assigns: %{current_user: current_user}} = socket
) do
pack = Ammo.get_pack!(pack_id, current_user)
container = Containers.get_container!(container_id, current_user)
{:ok, _pack} = pack |> Ammo.update_pack(%{"staged" => !pack.staged}, current_user)
{:ok, _container} =
container
|> Containers.update_container(current_user, %{"staged" => !container.staged})
prompt = dgettext("prompts", "Ammo unstaged succesfully")
prompt = dgettext("prompts", "Container unstaged succesfully")
{:noreply, socket |> put_flash(:info, prompt) |> display_shot_records()}
end
@ -175,6 +178,10 @@ defmodule CanneryWeb.RangeLive.Index do
start_date: start_date
)
containers =
Containers.list_containers(current_user, staged: true)
|> Map.new(fn container = %{id: container_id} -> {container_id, container} end)
packs = Ammo.list_packs(current_user, staged: true)
chart_data = shot_records |> get_chart_data_for_shot_record()
original_counts = packs |> Ammo.get_original_counts(current_user)
@ -184,6 +191,7 @@ defmodule CanneryWeb.RangeLive.Index do
socket
|> assign(
containers: containers,
packs: packs,
original_counts: original_counts,
cprs: cprs,

View File

@ -3,18 +3,18 @@
<%= gettext("Range day") %>
</h1>
<%= if @packs |> Enum.empty?() do %>
<%= if @containers |> Enum.empty?() do %>
<h1 class="text-xl title text-primary-600">
<%= gettext("No ammo staged") %>
<%= gettext("No containers staged") %>
<%= display_emoji("😔") %>
</h1>
<.link navigate={~p"/ammo"} class="btn btn-primary">
<.link navigate={~p"/containers"} class="btn btn-primary">
<%= dgettext("actions", "Why not get some ready to shoot?") %>
</.link>
<% else %>
<.link navigate={~p"/ammo"} class="btn btn-primary">
<%= dgettext("actions", "Stage ammo") %>
<.link navigate={~p"/containers"} class="btn btn-primary">
<%= dgettext("actions", "Stage containers") %>
</.link>
<div class="flex flex-row flex-wrap justify-center items-stretch w-full">
@ -42,25 +42,14 @@
<div class="flex flex-row flex-wrap justify-center items-stretch w-full">
<.pack_card
:for={%{id: pack_id} = pack <- @packs}
:for={%{id: pack_id, container_id: container_id} = pack <- @packs}
pack={pack}
original_count={Map.fetch!(@original_counts, pack_id)}
cpr={Map.get(@cprs, pack_id)}
last_used_date={Map.get(@last_used_dates, pack_id)}
current_user={@current_user}
container={Map.fetch!(@containers, container_id)}
>
<button
type="button"
class="btn btn-primary"
phx-click="toggle_staged"
phx-value-pack_id={pack.id}
data-confirm={"#{dgettext("prompts", "Are you sure you want to unstage this ammo?")}"}
>
<%= if pack.staged,
do: dgettext("actions", "Unstage from range"),
else: dgettext("actions", "Stage for range") %>
</button>
<.link patch={~p"/range/add_shot_record/#{pack}"} class="btn btn-primary">
<%= dgettext("actions", "Record shots") %>
</.link>