add cloning to ammo group index

This commit is contained in:
2022-11-10 18:21:29 -05:00
parent 3dd6430105
commit 09394ea408
31 changed files with 217 additions and 138 deletions

View File

@ -73,14 +73,14 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
ammo_group_params
) do
changeset_action =
case action do
:new -> :insert
:edit -> :update
cond do
action in [:new, :clone] -> :insert
action == :edit -> :update
end
changeset =
case action do
:new ->
cond do
action in [:new, :clone] ->
ammo_type =
if ammo_group_params |> Map.has_key?("ammo_type_id"),
do: ammo_group_params |> Map.get("ammo_type_id") |> Ammo.get_ammo_type!(user),
@ -93,7 +93,7 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
ammo_group |> AmmoGroup.create_changeset(ammo_type, container, user, ammo_group_params)
:edit ->
action == :edit ->
ammo_group |> AmmoGroup.update_changeset(ammo_group_params)
end
@ -127,9 +127,10 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
defp save_ammo_group(
%{assigns: %{changeset: changeset}} = socket,
:new,
action,
%{"multiplier" => multiplier_str} = ammo_group_params
) do
)
when action in [:new, :clone] do
socket =
case multiplier_str |> Integer.parse() do
{multiplier, _remainder}

View File

@ -51,8 +51,8 @@
) %>
<%= error_tag(f, :container_id, "col-span-3 text-center") %>
<%= case @action do %>
<% :new -> %>
<%= cond do %>
<% @action in [:new, :clone] -> %>
<hr class="hr col-span-3" />
<%= label(f, :multiplier, gettext("Copies"), class: "title text-lg text-primary-600") %>
@ -69,7 +69,7 @@
) %>
<%= error_tag(f, :multiplier, "col-span-3 text-center") %>
<% :edit -> %>
<% @action == :edit -> %>
<%= submit(dgettext("actions", "Save"),
phx_disable_with: dgettext("prompts", "Saving..."),
class: "mx-auto col-span-3 btn btn-primary"

View File

@ -39,6 +39,12 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :clone, %{"id" => id}) do
socket
|> assign(:page_title, dgettext("actions", "Add Ammo"))
|> assign(:ammo_group, %{Ammo.get_ammo_group!(id, current_user) | id: nil})
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, dgettext("actions", "Add Ammo"))
@ -217,6 +223,14 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
<i class="fa-fw fa-lg fas fa-edit"></i>
</.link>
<.link
patch={Routes.ammo_group_index_path(Endpoint, :clone, @ammo_group)}
class="text-primary-600 link"
data-qa={"clone-#{@ammo_group.id}"}
>
<i class="fa-fw fa-lg fas fa-copy"></i>
</.link>
<.link
href="#"
class="text-primary-600 link"

View File

@ -61,7 +61,7 @@
</div>
<%= cond do %>
<% @live_action in [:new, :edit] -> %>
<% @live_action in [:new, :edit, :clone] -> %>
<.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.AmmoGroupLive.FormComponent}