add move to container button for ammo groups
This commit is contained in:
57
lib/cannery_web/components/move_ammo_group_component.ex
Normal file
57
lib/cannery_web/components/move_ammo_group_component.ex
Normal file
@ -0,0 +1,57 @@
|
||||
defmodule CanneryWeb.Components.MoveAmmoGroupComponent do
|
||||
@moduledoc """
|
||||
Livecomponent that can move an ammo group to another container
|
||||
"""
|
||||
|
||||
use CanneryWeb, :live_component
|
||||
alias Cannery.{Accounts.User, Ammo, Ammo.AmmoGroup, Containers}
|
||||
alias Phoenix.LiveView.Socket
|
||||
|
||||
@impl true
|
||||
@spec update(
|
||||
%{
|
||||
required(:current_user) => User.t(),
|
||||
required(:ammo_group) => AmmoGroup.t(),
|
||||
optional(any()) => any()
|
||||
},
|
||||
Socket.t()
|
||||
) :: {:ok, Socket.t()}
|
||||
def update(
|
||||
%{ammo_group: %{container_id: container_id} = ammo_group, current_user: current_user} =
|
||||
assigns,
|
||||
socket
|
||||
) do
|
||||
changeset = Ammo.change_ammo_group(ammo_group)
|
||||
|
||||
containers =
|
||||
Containers.list_containers(current_user)
|
||||
|> Enum.reject(fn %{id: id} -> id == container_id end)
|
||||
|
||||
{:ok, socket |> assign(assigns) |> assign(changeset: changeset, containers: containers)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event(
|
||||
"move",
|
||||
%{"container_id" => container_id},
|
||||
%{assigns: %{ammo_group: ammo_group, current_user: current_user, return_to: return_to}} =
|
||||
socket
|
||||
) do
|
||||
%{name: container_name} = Containers.get_container!(container_id, current_user)
|
||||
|
||||
socket =
|
||||
ammo_group
|
||||
|> Ammo.update_ammo_group(%{"container_id" => container_id}, current_user)
|
||||
|> case do
|
||||
{:ok, _ammo_group} ->
|
||||
prompt = dgettext("prompts", "Ammo moved to %{name} successfully", name: container_name)
|
||||
|
||||
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
socket |> assign(changeset: changeset)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
@ -0,0 +1,69 @@
|
||||
<div class="w-full flex flex-col space-y-8 justify-center items-center">
|
||||
<h2 class="text-center title text-xl text-primary-500">
|
||||
<%= gettext("Move ammo") %>
|
||||
</h2>
|
||||
|
||||
<%= if @containers |> Enum.empty?() do %>
|
||||
<h2 class="title text-xl text-primary-500">
|
||||
<%= gettext("No other containers") %> 😔
|
||||
</h2>
|
||||
|
||||
<%= live_patch(dgettext("actions", "Add another container!"),
|
||||
to: Routes.container_index_path(@socket, :new),
|
||||
class: "btn btn-primary"
|
||||
) %>
|
||||
<% 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("Container") %>
|
||||
</th>
|
||||
|
||||
<th class="p-2">
|
||||
<%= gettext("Type") %>
|
||||
</th>
|
||||
|
||||
<th class="p-2">
|
||||
<%= gettext("Location") %>
|
||||
</th>
|
||||
|
||||
<th class="p-2"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="containers">
|
||||
<%= for container <- @containers do %>
|
||||
<tr id={"container-#{container.id}"}>
|
||||
<td class="p-2">
|
||||
<%= container.name %>
|
||||
</td>
|
||||
|
||||
<td class="p-2">
|
||||
<%= container.type %>
|
||||
</td>
|
||||
|
||||
<td class="p-2">
|
||||
<%= container.location %>
|
||||
</td>
|
||||
|
||||
<td class="p-2">
|
||||
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
phx-click="move"
|
||||
phx-target={@myself}
|
||||
phx-value-container_id={container.id}
|
||||
>
|
||||
<%= dgettext("actions", "Select") %>
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
@ -17,6 +17,12 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|
||||
{:noreply, apply_action(socket, live_action, params)}
|
||||
end
|
||||
|
||||
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :move, %{"id" => id}) do
|
||||
socket
|
||||
|> assign(:page_title, gettext("Move Ammo group"))
|
||||
|> 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 Ammo group"))
|
||||
|
@ -80,7 +80,12 @@
|
||||
</td>
|
||||
|
||||
<td class="p-2">
|
||||
<%= if ammo_group.container, do: ammo_group.container.name %>
|
||||
<%= if ammo_group.container do %>
|
||||
<%= live_patch(ammo_group.container.name,
|
||||
to: Routes.ammo_group_index_path(@socket, :move, ammo_group),
|
||||
class: "btn btn-primary"
|
||||
) %>
|
||||
<% end %>
|
||||
</td>
|
||||
|
||||
<td class="p-2 w-full h-full space-x-2 flex justify-center items-center">
|
||||
@ -112,30 +117,45 @@
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<%= if @live_action in [:new, :edit] do %>
|
||||
<.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(Endpoint, :index)}
|
||||
current_user={@current_user}
|
||||
/>
|
||||
</.modal>
|
||||
<% end %>
|
||||
<%= cond do %>
|
||||
<% @live_action in [:new, :edit] -> %>
|
||||
<.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(Endpoint, :index)}
|
||||
current_user={@current_user}
|
||||
/>
|
||||
</.modal>
|
||||
|
||||
<%= 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>
|
||||
<% @live_action == :add_shot_group -> %>
|
||||
<.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>
|
||||
|
||||
<% @live_action == :move -> %>
|
||||
<.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}>
|
||||
<.live_component
|
||||
module={CanneryWeb.Components.MoveAmmoGroupComponent}
|
||||
id={@ammo_group.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
ammo_group={@ammo_group}
|
||||
return_to={Routes.ammo_group_index_path(Endpoint, :index)}
|
||||
current_user={@current_user}
|
||||
/>
|
||||
</.modal>
|
||||
|
||||
<% true -> %>
|
||||
<% end %>
|
||||
|
@ -14,26 +14,11 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
|
||||
end
|
||||
|
||||
@impl true
|
||||
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
|
||||
def handle_params(
|
||||
%{"id" => id},
|
||||
_url,
|
||||
%{assigns: %{live_action: live_action, current_user: current_user}} = socket
|
||||
) 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
|
||||
@ -64,6 +49,8 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
|
||||
{:noreply, socket |> assign(ammo_group: ammo_group)}
|
||||
end
|
||||
|
||||
defp page_title(:add_shot_group), do: gettext("Add Shot group")
|
||||
defp page_title(:move), do: gettext("Move Ammo group")
|
||||
defp page_title(:show), do: gettext("Show Ammo group")
|
||||
defp page_title(:edit), do: gettext("Edit Ammo group")
|
||||
end
|
||||
|
@ -25,11 +25,11 @@
|
||||
|
||||
<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),
|
||||
to: Routes.ammo_type_show_path(Endpoint, :show, @ammo_group.ammo_type),
|
||||
class: "btn btn-primary"
|
||||
) %>
|
||||
|
||||
<%= live_patch to: Routes.ammo_group_show_path(@socket, :edit, @ammo_group),
|
||||
<%= live_patch to: Routes.ammo_group_show_path(Endpoint, :edit, @ammo_group),
|
||||
class: "text-primary-500 link" do %>
|
||||
<i class="fa-fw fa-lg fas fa-edit"></i>
|
||||
<% end %>
|
||||
@ -44,6 +44,11 @@
|
||||
<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>
|
||||
|
||||
<%= live_patch(dgettext("actions", "Move to different container"),
|
||||
to: Routes.ammo_group_show_path(Endpoint, :move, @ammo_group),
|
||||
class: "btn btn-primary"
|
||||
) %>
|
||||
</div>
|
||||
|
||||
<hr class="mb-4 w-full">
|
||||
@ -61,30 +66,45 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<%= if @live_action in [:edit] do %>
|
||||
<.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(Endpoint, :show, @ammo_group)}
|
||||
current_user={@current_user}
|
||||
/>
|
||||
</.modal>
|
||||
<% end %>
|
||||
<%= case @live_action do %>
|
||||
<% :edit -> %>
|
||||
<.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(Endpoint, :show, @ammo_group)}
|
||||
current_user={@current_user}
|
||||
/>
|
||||
</.modal>
|
||||
|
||||
<%= 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>
|
||||
<% :add_shot_group -> %>
|
||||
<.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>
|
||||
|
||||
<% :move -> %>
|
||||
<.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}>
|
||||
<.live_component
|
||||
module={CanneryWeb.Components.MoveAmmoGroupComponent}
|
||||
id={@ammo_group.id}
|
||||
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>
|
||||
|
||||
<% _show -> %>
|
||||
<% end %>
|
||||
|
@ -63,7 +63,7 @@ defmodule CanneryWeb.LiveHelpers do
|
||||
<i class="fa-fw fa-lg fas fa-times"></i>
|
||||
<% end %>
|
||||
|
||||
<div class="p-8 flex flex-col space-y-4 justify-start items-center">
|
||||
<div class="w-full p-8 flex flex-col space-y-4 justify-start items-center">
|
||||
<%= render_slot(@inner_block) %>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -45,6 +45,10 @@
|
||||
<%= gettext("No shots recorded") %> 😔
|
||||
</h1>
|
||||
<% else %>
|
||||
<h1 class="title text-2xl text-primary-500">
|
||||
<%= gettext("Shot log") %>
|
||||
</h1>
|
||||
|
||||
<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">
|
||||
|
@ -73,10 +73,12 @@ defmodule CanneryWeb.Router do
|
||||
live "/ammo_groups/new", AmmoGroupLive.Index, :new
|
||||
live "/ammo_groups/:id/edit", AmmoGroupLive.Index, :edit
|
||||
live "/ammo_groups/:id/add_shot_group", AmmoGroupLive.Index, :add_shot_group
|
||||
live "/ammo_groups/:id/move", AmmoGroupLive.Index, :move
|
||||
|
||||
live "/ammo_groups/:id", AmmoGroupLive.Show, :show
|
||||
live "/ammo_groups/:id/show/edit", AmmoGroupLive.Show, :edit
|
||||
live "/ammo_groups/:id/show/add_shot_group", AmmoGroupLive.Show, :add_shot_group
|
||||
live "/ammo_groups/:id/show/move", AmmoGroupLive.Show, :move
|
||||
|
||||
live "/range", RangeLive.Index, :index
|
||||
live "/range/:id/edit", RangeLive.Index, :edit
|
||||
|
Reference in New Issue
Block a user