From 243ebd8aa2d1221bfca0d8cd6b834f56a3c55868 Mon Sep 17 00:00:00 2001
From: Adam Piontek <adam@73k.us>
Date: Thu, 11 Mar 2021 16:39:02 -0500
Subject: [PATCH] can now set fave shift template from shift template view

---
 assets/js/app.js                              |  2 ++
 lib/shift73k/accounts.ex                      |  9 ++------
 lib/shift73k/accounts/user.ex                 |  2 +-
 .../live/shift_template_live/index.ex         | 21 +++++++++++++++++++
 .../live/shift_template_live/index.html.leex  | 13 +++++++++---
 5 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/assets/js/app.js b/assets/js/app.js
index 47873136..326e55d7 100644
--- a/assets/js/app.js
+++ b/assets/js/app.js
@@ -43,6 +43,8 @@ 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";
+import "../node_modules/bootstrap-icons/icons/star.svg";
+import "../node_modules/bootstrap-icons/icons/star-fill.svg";
 
 // webpack automatically bundles all modules in your
 // entry points. Those entry points can be configured
diff --git a/lib/shift73k/accounts.ex b/lib/shift73k/accounts.ex
index cc3a7078..c90f2633 100644
--- a/lib/shift73k/accounts.ex
+++ b/lib/shift73k/accounts.ex
@@ -449,21 +449,16 @@ defmodule Shift73k.Accounts do
   @doc """
   Sets a shift template as a user's favorite
   """
-  def set_user_fave_shift_template(%User{id: user_id}, %ShiftTemplate{
-        id: shift_template_id,
-        user_id: user_id
-      }) do
+  def set_user_fave_shift_template(user_id, shift_template_id) do
     User
     |> where(id: ^user_id)
     |> Repo.update_all(set: [fave_shift_template_id: shift_template_id])
   end
 
-  def set_user_fave_shift_template(_, _), do: {0, nil}
-
   @doc """
   Clears a user's favorite shift template
   """
-  def unset_user_fave_shift_template(%User{id: user_id}) do
+  def unset_user_fave_shift_template(user_id) do
     User
     |> where(id: ^user_id)
     |> Repo.update_all(set: [fave_shift_template_id: nil])
diff --git a/lib/shift73k/accounts/user.ex b/lib/shift73k/accounts/user.ex
index b1934068..387f3561 100644
--- a/lib/shift73k/accounts/user.ex
+++ b/lib/shift73k/accounts/user.ex
@@ -19,7 +19,7 @@ defmodule Shift73k.Accounts.User do
 
   @derive {Inspect, except: [:password]}
   @primary_key {:id, :binary_id, autogenerate: true}
-  # @foreign_key_type :binary_id
+  @foreign_key_type :binary_id
   schema "users" do
     field(:email, :string)
     field(:password, :string, virtual: true)
diff --git a/lib/shift73k_web/live/shift_template_live/index.ex b/lib/shift73k_web/live/shift_template_live/index.ex
index 25d92ff3..053d8354 100644
--- a/lib/shift73k_web/live/shift_template_live/index.ex
+++ b/lib/shift73k_web/live/shift_template_live/index.ex
@@ -1,6 +1,7 @@
 defmodule Shift73kWeb.ShiftTemplateLive.Index do
   use Shift73kWeb, :live_view
 
+  alias Shift73k.Accounts
   alias Shift73k.ShiftTemplates
   alias Shift73k.ShiftTemplates.ShiftTemplate
   alias Shift73kWeb.Roles
@@ -76,6 +77,26 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do
     {:noreply, assign(socket, :delete_shift_template, ShiftTemplates.get_shift_template!(id))}
   end
 
+  def handle_event("set-user-fave-shift-template", %{"id" => shift_template_id}, socket) do
+    user_id = socket.assigns.current_user.id
+    Accounts.set_user_fave_shift_template(user_id, shift_template_id)
+
+    socket
+    |> assign(:current_user, Accounts.get_user!(user_id))
+    |> assign_shift_templates()
+    |> live_noreply()
+  end
+
+  def handle_event("unset-user-fave-shift-template", _params, socket) do
+    user_id = socket.assigns.current_user.id
+    Accounts.unset_user_fave_shift_template(user_id)
+
+    socket
+    |> assign(:current_user, Accounts.get_user!(user_id))
+    |> assign_shift_templates()
+    |> live_noreply()
+  end
+
   # @impl true
   # def handle_event("delete", %{"id" => id}, socket) do
   #   shift_template = ShiftTemplates.get_shift_template!(id)
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 d47efbbc..4af1f4f7 100644
--- a/lib/shift73k_web/live/shift_template_live/index.html.leex
+++ b/lib/shift73k_web/live/shift_template_live/index.html.leex
@@ -38,9 +38,14 @@
         <div class="col-12 col-lg-6">
 
           <div class="card mt-4">
-            <h5 class="card-header">
-              <span class="visually-hidden">Subject:</span>
+            <h5 class="card-header d-flex justify-content-between align-items-start">
+              <div class="visually-hidden">Subject:</div>
               <%= shift.subject %>
+              <%= if shift.id == @current_user.fave_shift_template_id do %>
+                <%= icon_div @socket, "bi-star-fill", [class: "icon baseline text-primary align-self-start ms-2", phx_click: "unset-user-fave-shift-template"] %>
+              <% else %>
+                <%= icon_div @socket, "bi-star", [class: "icon baseline text-primary align-self-start ms-2", phx_click: "set-user-fave-shift-template", phx_value_id: shift.id] %>
+              <% end %>
             </h5>
             <div class="card-body">
 
@@ -48,7 +53,7 @@
                 <tbody>
                   <tr>
                     <th scope="row" class="text-end">
-                      <%= icon_div @socket, "bi-hourglass", [class: "icon baseline text-muted"] %>
+                      <%= icon_div @socket, "bi-hourglass", [class: "icon text-muted"] %>
                       <span class="visually-hidden">Hours:</span>
                     </th>
                     <td>
@@ -107,6 +112,8 @@
                 <% end %>
               <% end %>
 
+              <%#= button "" %>
+
               <%= 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;"] %>