From ea0f8b86cdf01af6143cfb9819a4a87df61dabe5 Mon Sep 17 00:00:00 2001
From: Adam Piontek <adam@73k.us>
Date: Thu, 11 Mar 2021 14:05:22 -0500
Subject: [PATCH] added shift template cloning feature

---
 assets/js/app.js                                         | 1 +
 .../live/shift_template_live/form_component.ex           | 4 ++++
 lib/shift73k_web/live/shift_template_live/index.ex       | 6 ++++++
 .../live/shift_template_live/index.html.leex             | 9 ++++++++-
 lib/shift73k_web/roles.ex                                | 1 +
 lib/shift73k_web/router.ex                               | 1 +
 6 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/assets/js/app.js b/assets/js/app.js
index 72edd2eb..47873136 100644
--- a/assets/js/app.js
+++ b/assets/js/app.js
@@ -42,6 +42,7 @@ import "../node_modules/bootstrap-icons/icons/map.svg";
 import "../node_modules/bootstrap-icons/icons/geo.svg";
 import "../node_modules/bootstrap-icons/icons/justify-left.svg";
 import "../node_modules/bootstrap-icons/icons/plus-circle-dotted.svg";
+import "../node_modules/bootstrap-icons/icons/clipboard-plus.svg";
 
 // webpack automatically bundles all modules in your
 // entry points. Those entry points can be configured
diff --git a/lib/shift73k_web/live/shift_template_live/form_component.ex b/lib/shift73k_web/live/shift_template_live/form_component.ex
index b156d53c..c7969881 100644
--- a/lib/shift73k_web/live/shift_template_live/form_component.ex
+++ b/lib/shift73k_web/live/shift_template_live/form_component.ex
@@ -77,6 +77,10 @@ defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do
     end
   end
 
+  defp save_shift_template(socket, :clone, shift_template_params) do
+    save_shift_template(socket, :new, shift_template_params)
+  end
+
   defp save_shift_template(socket, :edit, shift_template_params) do
     case ShiftTemplates.update_shift_template(
            socket.assigns.shift_template,
diff --git a/lib/shift73k_web/live/shift_template_live/index.ex b/lib/shift73k_web/live/shift_template_live/index.ex
index f8818cf0..25d92ff3 100644
--- a/lib/shift73k_web/live/shift_template_live/index.ex
+++ b/lib/shift73k_web/live/shift_template_live/index.ex
@@ -34,6 +34,12 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do
     end
   end
 
+  defp apply_action(socket, :clone, %{"id" => id}) do
+    socket
+    |> assign(:page_title, "Clone Shift Template")
+    |> assign(:shift_template, ShiftTemplates.get_shift_template!(id))
+  end
+
   defp apply_action(socket, :edit, %{"id" => id}) do
     socket
     |> assign(:page_title, "Edit Shift Template")
diff --git a/lib/shift73k_web/live/shift_template_live/index.html.leex b/lib/shift73k_web/live/shift_template_live/index.html.leex
index d6ce5dd0..d47efbbc 100644
--- a/lib/shift73k_web/live/shift_template_live/index.html.leex
+++ b/lib/shift73k_web/live/shift_template_live/index.html.leex
@@ -1,4 +1,4 @@
-<%= if @live_action in [:new, :edit] do %>
+<%= if @live_action in [:new, :edit, :clone] do %>
   <%= live_modal @socket, Shift73kWeb.ShiftTemplateLive.FormComponent,
     id: @shift_template.id || :new,
     title: @page_title,
@@ -100,6 +100,13 @@
                 <% end %>
               <% end %>
 
+              <%= if Roles.can?(@current_user, shift, :clone) do %>
+                <%= live_patch to: Routes.shift_template_index_path(@socket, :clone, shift), class: "btn btn-outline-primary btn-sm text-nowrap" do %>
+                  <%= icon_div @socket, "bi-clipboard-plus", [class: "icon baseline", style: "margin-right:0.125rem;"] %>
+                  Clone
+                <% end %>
+              <% end %>
+
               <%= if Roles.can?(@current_user, shift, :delete) do %>
                 <button class="btn btn-outline-danger btn-sm text-nowrap" phx-click="delete-modal" phx-value-id="<%= shift.id %>">
                   <%= icon_div @socket, "bi-trash", [class: "icon baseline", style: "margin-right:0.125rem;"] %>
diff --git a/lib/shift73k_web/roles.ex b/lib/shift73k_web/roles.ex
index 07751894..630c4277 100644
--- a/lib/shift73k_web/roles.ex
+++ b/lib/shift73k_web/roles.ex
@@ -18,6 +18,7 @@ defmodule Shift73kWeb.Roles do
   def can?(%User{}, %ShiftTemplate{}, :new), do: true
   # def can?(%User{}, %ShiftTemplate{}, :show), do: true
   def can?(%User{id: id}, %ShiftTemplate{user_id: id}, :edit), do: true
+  def can?(%User{id: id}, %ShiftTemplate{user_id: id}, :clone), do: true
   def can?(%User{id: id}, %ShiftTemplate{user_id: id}, :delete), do: true
 
   # Accounts / User
diff --git a/lib/shift73k_web/router.ex b/lib/shift73k_web/router.ex
index aa244e45..07dd83db 100644
--- a/lib/shift73k_web/router.ex
+++ b/lib/shift73k_web/router.ex
@@ -94,6 +94,7 @@ defmodule Shift73kWeb.Router do
     live "/my_shifts", ShiftTemplateLive.Index, :index
     live "/my_shifts/new", ShiftTemplateLive.Index, :new
     live "/my_shifts/:id/edit", ShiftTemplateLive.Index, :edit
+    live "/my_shifts/:id/clone", ShiftTemplateLive.Index, :clone
   end
 
   # scope "/", Shift73kWeb do