forked from shibao/cannery
move staging to container
This commit is contained in:
@ -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,
|
||||
|
@ -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>
|
||||
|
Reference in New Issue
Block a user