add cloning to ammo type index

This commit is contained in:
2022-11-10 19:10:28 -05:00
parent 0d6f6de7df
commit 8fa75e2559
18 changed files with 258 additions and 183 deletions

View File

@ -35,15 +35,15 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
ammo_type_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 -> ammo_type |> AmmoType.create_changeset(user, ammo_type_params)
:edit -> ammo_type |> AmmoType.update_changeset(ammo_type_params)
cond do
action in [:new, :clone] -> ammo_type |> AmmoType.create_changeset(user, ammo_type_params)
action == :edit -> ammo_type |> AmmoType.update_changeset(ammo_type_params)
end
changeset =
@ -76,9 +76,10 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
defp save_ammo_type(
%{assigns: %{current_user: current_user, return_to: return_to}} = socket,
:new,
action,
ammo_type_params
) do
)
when action in [:new, :clone] do
socket =
case Ammo.create_ammo_type(ammo_type_params, current_user) do
{:ok, %{name: ammo_type_name}} ->

View File

@ -24,6 +24,12 @@ defmodule CanneryWeb.AmmoTypeLive.Index do
|> assign(:ammo_type, Ammo.get_ammo_type!(id, current_user))
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :clone, %{"id" => id}) do
socket
|> assign(:page_title, gettext("New Ammo type"))
|> assign(:ammo_type, %{Ammo.get_ammo_type!(id, current_user) | id: nil})
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, gettext("New Ammo type"))
@ -154,6 +160,14 @@ defmodule CanneryWeb.AmmoTypeLive.Index do
<i class="fa-fw fa-lg fas fa-edit"></i>
</.link>
<.link
patch={Routes.ammo_type_index_path(Endpoint, :clone, @ammo_type)}
class="text-primary-600 link"
data-qa={"clone-#{@ammo_type.id}"}
>
<i class="fa-fw fa-lg fas fa-copy"></i>
</.link>
<.link
href="#"
class="text-primary-600 link"

View File

@ -27,7 +27,7 @@
<% end %>
</div>
<%= if @live_action in [:new, :edit] do %>
<%= if @live_action in [:new, :edit, :clone] do %>
<.modal return_to={Routes.ammo_type_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.AmmoTypeLive.FormComponent}

View File

@ -68,6 +68,7 @@ defmodule CanneryWeb.Router do
live "/catalog", AmmoTypeLive.Index, :index
live "/catalog/new", AmmoTypeLive.Index, :new
live "/catalog/:id/clone", AmmoTypeLive.Index, :clone
live "/catalog/:id/edit", AmmoTypeLive.Index, :edit
live "/catalog/:id", AmmoTypeLive.Show, :show