add cloning to ammo group index
This commit is contained in:
		| @@ -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} | ||||
|   | ||||
| @@ -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" | ||||
|   | ||||
| @@ -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" | ||||
|   | ||||
| @@ -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} | ||||
|   | ||||
| @@ -85,6 +85,7 @@ defmodule CanneryWeb.Router do | ||||
|     live "/ammo", AmmoGroupLive.Index, :index | ||||
|     live "/ammo/new", AmmoGroupLive.Index, :new | ||||
|     live "/ammo/:id/edit", AmmoGroupLive.Index, :edit | ||||
|     live "/ammo/:id/clone", AmmoGroupLive.Index, :clone | ||||
|     live "/ammo/:id/add_shot_group", AmmoGroupLive.Index, :add_shot_group | ||||
|     live "/ammo/:id/move", AmmoGroupLive.Index, :move | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user