From dceef941c76e6943a9dc6d36b655f627f216c5f0 Mon Sep 17 00:00:00 2001
From: Adam Piontek <adam@73k.us>
Date: Sun, 14 Aug 2022 12:49:25 -0400
Subject: [PATCH] fixed user delete error by correcting foreign key constraint;
 updated liveview modals to use component directly & removed deprecated
 @socket parameters

---
 lib/shift73k_web/live/live_helpers.ex         | 22 ---------------
 .../live/shift_assign_live/index.html.heex    |  9 ++++--
 .../live/shift_live/index.html.heex           | 10 +++++--
 .../live/shift_template_live/index.html.heex  | 28 +++++++++++++------
 lib/shift73k_web/live/user/settings.html.heex |  8 +++---
 .../live/user_management/delete_component.ex  | 10 +++++--
 .../live/user_management/index.html.heex      | 19 ++++++++++---
 ...814161100_fix_shifts_user_id_reference.exs | 19 +++++++++++++
 8 files changed, 80 insertions(+), 45 deletions(-)
 create mode 100644 priv/repo/migrations/20220814161100_fix_shifts_user_id_reference.exs

diff --git a/lib/shift73k_web/live/live_helpers.ex b/lib/shift73k_web/live/live_helpers.ex
index ff98ac8d..36c33170 100644
--- a/lib/shift73k_web/live/live_helpers.ex
+++ b/lib/shift73k_web/live/live_helpers.ex
@@ -1,6 +1,5 @@
 defmodule Shift73kWeb.LiveHelpers do
   import Phoenix.LiveView
-  import Phoenix.LiveView.Helpers
 
   alias Shift73k.Accounts
   alias Shift73k.Accounts.User
@@ -19,27 +18,6 @@ defmodule Shift73kWeb.LiveHelpers do
   """
   def live_okreply(socket), do: {:ok, socket}
 
-  @doc """
-  Renders a component inside the `Shift73kWeb.ModalComponent` component.
-
-  The rendered modal receives a `:return_to` option to properly update
-  the URL when the modal is closed.
-
-  ## Examples
-
-      <%= live_modal @socket, Shift73kWeb.PropertyLive.FormComponent,
-        id: @property.id || :new,
-        action: @live_action,
-        property: @property,
-        return_to: Routes.property_index_path(@socket, :index) %>
-  """
-  def live_modal(socket, component, opts) do
-    modal_opts = [id: :modal, component: component, opts: opts]
-    # dirty little workaround for elixir complaining about socket being unused
-    _socket = socket
-    live_component(socket, Shift73kWeb.ModalComponent, modal_opts)
-  end
-
   @doc """
   Loads default assigns for liveviews
   """
diff --git a/lib/shift73k_web/live/shift_assign_live/index.html.heex b/lib/shift73k_web/live/shift_assign_live/index.html.heex
index ce1d37a4..ce656afe 100644
--- a/lib/shift73k_web/live/shift_assign_live/index.html.heex
+++ b/lib/shift73k_web/live/shift_assign_live/index.html.heex
@@ -1,10 +1,15 @@
 <%= if @delete_days_shifts do %>
-  <%= live_modal @socket, Shift73kWeb.ShiftAssignLive.DeleteComponent,
+  <.live_component
+    module={Shift73kWeb.ModalComponent}
+    id="modal"
+    component={Shift73kWeb.ShiftAssignLive.DeleteComponent}
+    opts={[
       id: "delete-days-shifts-#{@current_user.id}",
       title: "Delete Shifts From Selected Days",
       delete_days_shifts: @delete_days_shifts,
       current_user: @current_user
-      %>
+    ]}
+    />
 <% end %>
 
 
diff --git a/lib/shift73k_web/live/shift_live/index.html.heex b/lib/shift73k_web/live/shift_live/index.html.heex
index bc9cdf08..b599bd8e 100644
--- a/lib/shift73k_web/live/shift_live/index.html.heex
+++ b/lib/shift73k_web/live/shift_live/index.html.heex
@@ -1,8 +1,14 @@
 <%= if @delete_shift do %>
-  <%= live_modal @socket, Shift73kWeb.ShiftLive.DeleteComponent,
+  <.live_component
+    module={Shift73kWeb.ModalComponent}
+    id="modal"
+    component={Shift73kWeb.ShiftLive.DeleteComponent}
+    opts={[
       id: @delete_shift.id,
       title: "Delete Shift Template",
-      delete_shift: @delete_shift %>
+      delete_shift: @delete_shift
+    ]}
+    />
 <% end %>
 
 
diff --git a/lib/shift73k_web/live/shift_template_live/index.html.heex b/lib/shift73k_web/live/shift_template_live/index.html.heex
index 45faa445..62abcdec 100644
--- a/lib/shift73k_web/live/shift_template_live/index.html.heex
+++ b/lib/shift73k_web/live/shift_template_live/index.html.heex
@@ -1,17 +1,29 @@
 <%= if @live_action in [:new, :edit, :clone] do %>
-  <%= live_modal @socket, Shift73kWeb.ShiftTemplateLive.FormComponent,
-    id: @shift_template.id || :new,
-    title: @page_title,
-    action: @live_action,
-    shift_template: @shift_template,
-    current_user: @current_user %>
+  <.live_component
+    module={Shift73kWeb.ModalComponent}
+    id="modal"
+    component={Shift73kWeb.ShiftTemplateLive.FormComponent}
+    opts={[
+      id: @shift_template.id || :new,
+      title: @page_title,
+      action: @live_action,
+      shift_template: @shift_template,
+      current_user: @current_user
+    ]}
+    />
 <% end %>
 
 <%= if @delete_shift_template do %>
-  <%= live_modal @socket, Shift73kWeb.ShiftTemplateLive.DeleteComponent,
+  <.live_component
+    module={Shift73kWeb.ModalComponent}
+    id="modal"
+    component={Shift73kWeb.ShiftTemplateLive.DeleteComponent}
+    opts={[
       id: @delete_shift_template.id,
       title: "Delete Shift Template",
-      delete_shift_template: @delete_shift_template %>
+      delete_shift_template: @delete_shift_template
+    ]}
+    />
 <% end %>
 
 
diff --git a/lib/shift73k_web/live/user/settings.html.heex b/lib/shift73k_web/live/user/settings.html.heex
index 40e08afe..e769ec43 100644
--- a/lib/shift73k_web/live/user/settings.html.heex
+++ b/lib/shift73k_web/live/user/settings.html.heex
@@ -6,10 +6,10 @@
     </h2>
 
     <div class="row justify-content-center justify-content-md-start">
-      <%= live_component Shift73kWeb.UserLive.Settings.Email, id: "email-#{@current_user.id}", current_user: @current_user %>
-      <%= live_component Shift73kWeb.UserLive.Settings.Password, id: "password-#{@current_user.id}", current_user: @current_user %>
-      <%= live_component Shift73kWeb.UserLive.Settings.WeekStart, id: "week_start-#{@current_user.id}", current_user: @current_user %>
-      <%= live_component Shift73kWeb.UserLive.Settings.CalendarUrl, id: "calendar_url-#{@current_user.id}", current_user: @current_user %>
+      <.live_component module={Shift73kWeb.UserLive.Settings.Email} id={"email-#{@current_user.id}"} current_user={@current_user} />
+      <.live_component module={Shift73kWeb.UserLive.Settings.Password} id={"password-#{@current_user.id}"} current_user={@current_user} />
+      <.live_component module={Shift73kWeb.UserLive.Settings.WeekStart} id={"week_start-#{@current_user.id}"} current_user={@current_user} />
+      <.live_component module={Shift73kWeb.UserLive.Settings.CalendarUrl} id={"calendar_url-#{@current_user.id}"} current_user={@current_user} />
     </div>
 
   </div>
diff --git a/lib/shift73k_web/live/user_management/delete_component.ex b/lib/shift73k_web/live/user_management/delete_component.ex
index b1c48aae..ee73804f 100644
--- a/lib/shift73k_web/live/user_management/delete_component.ex
+++ b/lib/shift73k_web/live/user_management/delete_component.ex
@@ -9,9 +9,13 @@ defmodule Shift73kWeb.UserManagement.DeleteComponent do
   end
 
   @impl true
-  def handle_event("confirm", %{"id" => id, "email" => email}, socket) do
-    id
-    |> Accounts.get_user()
+  def handle_event("confirm", %{"id" => id, "email" => email} = params, socket) do
+    IO.inspect(params)
+
+    user = Accounts.get_user(id)
+    IO.inspect(user)
+
+    user
     |> Accounts.delete_user()
     |> case do
       {:ok, _} ->
diff --git a/lib/shift73k_web/live/user_management/index.html.heex b/lib/shift73k_web/live/user_management/index.html.heex
index 912bb88b..110cd7de 100644
--- a/lib/shift73k_web/live/user_management/index.html.heex
+++ b/lib/shift73k_web/live/user_management/index.html.heex
@@ -1,18 +1,29 @@
 <%= if @live_action in [:new, :edit] do %>
-  <%= live_modal @socket, Shift73kWeb.UserManagement.FormComponent,
+  <.live_component
+    module={Shift73kWeb.ModalComponent}
+    id="modal"
+    component={Shift73kWeb.UserManagement.FormComponent}
+    opts={[
       id: @user.id || :new,
       title: @page_title,
       action: @live_action,
       user: @user,
-      current_user: @current_user %>
+      current_user: @current_user
+    ]}
+    />
 <% end %>
 
 <%= if @delete_user do %>
-  <%= live_modal @socket, Shift73kWeb.UserManagement.DeleteComponent,
+  <.live_component
+    module={Shift73kWeb.ModalComponent}
+    id="modal"
+    component={Shift73kWeb.UserManagement.DeleteComponent}
+    opts={[
       id: @delete_user.id,
       title: "Delete User",
       delete_user: @delete_user
-    %>
+    ]}
+    />
 <% end %>
 
 
diff --git a/priv/repo/migrations/20220814161100_fix_shifts_user_id_reference.exs b/priv/repo/migrations/20220814161100_fix_shifts_user_id_reference.exs
new file mode 100644
index 00000000..c4222e3a
--- /dev/null
+++ b/priv/repo/migrations/20220814161100_fix_shifts_user_id_reference.exs
@@ -0,0 +1,19 @@
+defmodule Shift73k.Repo.Migrations.FixShiftsUserIdReference do
+  use Ecto.Migration
+
+  def up do
+    execute("ALTER TABLE shifts DROP CONSTRAINT shifts_user_id_fkey")
+
+    alter table(:shifts) do
+      modify :user_id, references(:users, on_delete: :delete_all, type: :binary_id)
+    end
+  end
+
+  def down do
+    execute("ALTER TABLE shifts DROP CONSTRAINT shifts_user_id_fkey")
+
+    alter table(:shifts) do
+      modify :user_id, references(:users, on_delete: :nothing, type: :binary_id)
+    end
+  end
+end