fixed user delete error by correcting foreign key constraint; updated liveview modals to use component directly & removed deprecated @socket parameters

This commit is contained in:
Adam Piontek 2022-08-14 12:49:25 -04:00
parent 68d60c120d
commit dceef941c7
8 changed files with 80 additions and 45 deletions

View file

@ -1,6 +1,5 @@
defmodule Shift73kWeb.LiveHelpers do defmodule Shift73kWeb.LiveHelpers do
import Phoenix.LiveView import Phoenix.LiveView
import Phoenix.LiveView.Helpers
alias Shift73k.Accounts alias Shift73k.Accounts
alias Shift73k.Accounts.User alias Shift73k.Accounts.User
@ -19,27 +18,6 @@ defmodule Shift73kWeb.LiveHelpers do
""" """
def live_okreply(socket), do: {:ok, socket} 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 """ @doc """
Loads default assigns for liveviews Loads default assigns for liveviews
""" """

View file

@ -1,10 +1,15 @@
<%= if @delete_days_shifts do %> <%= 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}", id: "delete-days-shifts-#{@current_user.id}",
title: "Delete Shifts From Selected Days", title: "Delete Shifts From Selected Days",
delete_days_shifts: @delete_days_shifts, delete_days_shifts: @delete_days_shifts,
current_user: @current_user current_user: @current_user
%> ]}
/>
<% end %> <% end %>

View file

@ -1,8 +1,14 @@
<%= if @delete_shift do %> <%= 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, id: @delete_shift.id,
title: "Delete Shift Template", title: "Delete Shift Template",
delete_shift: @delete_shift %> delete_shift: @delete_shift
]}
/>
<% end %> <% end %>

View file

@ -1,17 +1,29 @@
<%= if @live_action in [:new, :edit, :clone] do %> <%= if @live_action in [:new, :edit, :clone] do %>
<%= live_modal @socket, Shift73kWeb.ShiftTemplateLive.FormComponent, <.live_component
id: @shift_template.id || :new, module={Shift73kWeb.ModalComponent}
title: @page_title, id="modal"
action: @live_action, component={Shift73kWeb.ShiftTemplateLive.FormComponent}
shift_template: @shift_template, opts={[
current_user: @current_user %> id: @shift_template.id || :new,
title: @page_title,
action: @live_action,
shift_template: @shift_template,
current_user: @current_user
]}
/>
<% end %> <% end %>
<%= if @delete_shift_template do %> <%= 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, id: @delete_shift_template.id,
title: "Delete Shift Template", title: "Delete Shift Template",
delete_shift_template: @delete_shift_template %> delete_shift_template: @delete_shift_template
]}
/>
<% end %> <% end %>

View file

@ -6,10 +6,10 @@
</h2> </h2>
<div class="row justify-content-center justify-content-md-start"> <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 module={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 module={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 module={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.CalendarUrl} id={"calendar_url-#{@current_user.id}"} current_user={@current_user} />
</div> </div>
</div> </div>

View file

@ -9,9 +9,13 @@ defmodule Shift73kWeb.UserManagement.DeleteComponent do
end end
@impl true @impl true
def handle_event("confirm", %{"id" => id, "email" => email}, socket) do def handle_event("confirm", %{"id" => id, "email" => email} = params, socket) do
id IO.inspect(params)
|> Accounts.get_user()
user = Accounts.get_user(id)
IO.inspect(user)
user
|> Accounts.delete_user() |> Accounts.delete_user()
|> case do |> case do
{:ok, _} -> {:ok, _} ->

View file

@ -1,18 +1,29 @@
<%= if @live_action in [:new, :edit] do %> <%= 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, id: @user.id || :new,
title: @page_title, title: @page_title,
action: @live_action, action: @live_action,
user: @user, user: @user,
current_user: @current_user %> current_user: @current_user
]}
/>
<% end %> <% end %>
<%= if @delete_user do %> <%= 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, id: @delete_user.id,
title: "Delete User", title: "Delete User",
delete_user: @delete_user delete_user: @delete_user
%> ]}
/>
<% end %> <% end %>

View file

@ -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