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

View File

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

View File

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

View File

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

View File

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

View File

@ -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, _} ->

View File

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

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