add move to container button for ammo groups

This commit is contained in:
shibao 2022-02-15 18:20:12 -05:00
parent 95a08d6088
commit 3996e8413c
12 changed files with 299 additions and 90 deletions

View 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

View File

@ -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>

View File

@ -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"))

View File

@ -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,7 +117,8 @@
<% end %>
</div>
<%= if @live_action in [:new, :edit] do %>
<%= cond do %>
<% @live_action in [:new, :edit] -> %>
<.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.AmmoGroupLive.FormComponent}
@ -124,9 +130,8 @@
current_user={@current_user}
/>
</.modal>
<% end %>
<%= if @live_action in [:add_shot_group] do %>
<% @live_action == :add_shot_group -> %>
<.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.Components.AddShotGroupComponent}
@ -138,4 +143,19 @@
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 %>

View File

@ -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}
def handle_params(
%{"id" => id},
_url,
%{assigns: %{live_action: live_action, current_user: current_user}} = socket
) 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
@ -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

View File

@ -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,7 +66,8 @@
</div>
</div>
<%= if @live_action in [:edit] do %>
<%= case @live_action do %>
<% :edit -> %>
<.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}>
<.live_component
module={CanneryWeb.AmmoGroupLive.FormComponent}
@ -73,9 +79,8 @@
current_user={@current_user}
/>
</.modal>
<% end %>
<%= if @live_action in [:add_shot_group] do %>
<% :add_shot_group -> %>
<.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}>
<.live_component
module={CanneryWeb.Components.AddShotGroupComponent}
@ -87,4 +92,19 @@
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 %>

View File

@ -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>

View File

@ -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">

View File

@ -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

View File

@ -11,7 +11,7 @@ msgid ""
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/index.ex:28
#: lib/cannery_web/live/ammo_group_live/index.ex:34
msgid "Add Ammo"
msgstr ""
@ -168,3 +168,18 @@ msgstr ""
#: lib/cannery_web/live/ammo_group_live/show.html.heex:27
msgid "Ammo Details"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/components/move_ammo_group_component.html.heex:11
msgid "Add another container!"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/show.html.heex:48
msgid "Move to different container"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/components/move_ammo_group_component.html.heex:59
msgid "Select"
msgstr ""

View File

@ -33,7 +33,7 @@ msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/components/topbar.ex:47
#: lib/cannery_web/live/ammo_group_live/index.html.heex:3
#: lib/cannery_web/live/range_live/index.html.heex:53
#: lib/cannery_web/live/range_live/index.html.heex:57
msgid "Ammo"
msgstr ""
@ -100,6 +100,7 @@ msgid "Case material"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/components/move_ammo_group_component.html.heex:21
#: lib/cannery_web/live/ammo_group_live/form_component.html.heex:48
#: lib/cannery_web/live/ammo_group_live/index.html.heex:41
msgid "Container"
@ -152,8 +153,8 @@ msgid "Easy to Use:"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/index.ex:22
#: lib/cannery_web/live/ammo_group_live/show.ex:68
#: lib/cannery_web/live/ammo_group_live/index.ex:28
#: lib/cannery_web/live/ammo_group_live/show.ex:55
msgid "Edit Ammo group"
msgstr ""
@ -254,6 +255,7 @@ msgid "Listing Tags"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/components/move_ammo_group_component.html.heex:29
#: lib/cannery_web/live/container_live/form_component.html.heex:42
msgid "Location"
msgstr ""
@ -359,7 +361,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_group_live/form_component.html.heex:41
#: lib/cannery_web/live/ammo_group_live/index.html.heex:35
#: lib/cannery_web/live/range_live/form_component.html.heex:29
#: lib/cannery_web/live/range_live/index.html.heex:59
#: lib/cannery_web/live/range_live/index.html.heex:63
msgid "Notes"
msgstr ""
@ -437,7 +439,7 @@ msgid "Settings"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/show.ex:67
#: lib/cannery_web/live/ammo_group_live/show.ex:54
msgid "Show Ammo group"
msgstr ""
@ -467,7 +469,7 @@ msgid "Steel"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/show.html.heex:54
#: lib/cannery_web/live/ammo_group_live/show.html.heex:59
msgid "Stored in"
msgstr ""
@ -494,7 +496,7 @@ msgid "The self-hosted firearm tracker website"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/show.html.heex:59
#: lib/cannery_web/live/ammo_group_live/show.html.heex:64
msgid "This ammo group is not in a container"
msgstr ""
@ -505,6 +507,7 @@ msgid "Tracer"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/components/move_ammo_group_component.html.heex:25
#: lib/cannery_web/live/container_live/form_component.html.heex:35
msgid "Type"
msgstr ""
@ -561,7 +564,7 @@ msgid "Range day"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/range_live/index.html.heex:62
#: lib/cannery_web/live/range_live/index.html.heex:66
msgid "Date"
msgstr ""
@ -595,7 +598,7 @@ msgid "Staging"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/show.ex:27
#: lib/cannery_web/live/ammo_group_live/show.ex:52
msgid "Add Shot group"
msgstr ""
@ -611,7 +614,7 @@ msgid "Ammo Types"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/index.ex:33
#: lib/cannery_web/live/ammo_group_live/index.ex:39
msgid "Ammo groups"
msgstr ""
@ -642,7 +645,7 @@ msgid "Rounds left"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/range_live/index.html.heex:56
#: lib/cannery_web/live/range_live/index.html.heex:60
msgid "Rounds shot"
msgstr ""
@ -650,3 +653,24 @@ msgstr ""
#: lib/cannery_web/live/range_live/index.ex:46
msgid "Shot Records"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/index.ex:22
#: lib/cannery_web/live/ammo_group_live/show.ex:53
msgid "Move Ammo group"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/components/move_ammo_group_component.html.heex:3
msgid "Move ammo"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/components/move_ammo_group_component.html.heex:8
msgid "No other containers"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/range_live/index.html.heex:49
msgid "Shot log"
msgstr ""

View File

@ -67,8 +67,8 @@ msgid "Ammo group created successfully"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/index.ex:40
#: lib/cannery_web/live/ammo_group_live/show.ex:49
#: lib/cannery_web/live/ammo_group_live/index.ex:46
#: lib/cannery_web/live/ammo_group_live/show.ex:34
msgid "Ammo group deleted succesfully"
msgstr ""
@ -97,7 +97,7 @@ msgid "Are you sure you want to delete the invite for %{name}?"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/index.html.heex:102
#: lib/cannery_web/live/ammo_group_live/index.html.heex:107
#: lib/cannery_web/live/ammo_group_live/show.html.heex:40
#: lib/cannery_web/live/ammo_type_live/index.html.heex:104
msgid "Are you sure you want to delete this ammo?"
@ -210,7 +210,7 @@ msgid "Ammo group unstaged succesfully"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/range_live/index.html.heex:97
#: lib/cannery_web/live/range_live/index.html.heex:101
msgid "Are you sure you want to delete this shot record?"
msgstr ""
@ -228,3 +228,8 @@ msgstr ""
#: lib/cannery_web/controllers/user_confirmation_controller.ex:37
msgid "%{email} confirmed successfully."
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/components/move_ammo_group_component.ex:47
msgid "Ammo moved to %{name} successfully"
msgstr ""