From 84a069e02f6c61dc646e4243291e5a6e223bb16d Mon Sep 17 00:00:00 2001 From: Matthew Alex Date: Mon, 2 Sep 2024 14:21:46 +0100 Subject: [PATCH 1/5] fix: update behaviour after deleting a company --- components/DeleteCompany.tsx | 10 ++++++++-- lib/crud/companies.ts | 8 +++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/components/DeleteCompany.tsx b/components/DeleteCompany.tsx index 0c7af257..5d8f428e 100644 --- a/components/DeleteCompany.tsx +++ b/components/DeleteCompany.tsx @@ -9,6 +9,7 @@ import { FormInModal } from "./forms/FormInModal" import { GenericFormModal } from "./modals/GenericFormModal" import { Button, Spinner, Text, TextField } from "@radix-ui/themes" +import { signOut } from "next-auth/react" import { useCallback } from "react" interface DeleteCompanyFormProps { @@ -18,8 +19,13 @@ interface DeleteCompanyFormProps { } const DeleteCompanyForm = ({ close, name, id }: DeleteCompanyFormProps) => { - const deleteCompanyWithName = async (prevState: FormPassBackState, formData: FormData) => - deleteCompany(prevState, formData, id, name) + const deleteCompanyWithName = async (prevState: FormPassBackState, formData: FormData) => { + const res = await deleteCompany(prevState, formData, id, name) + if (res.status === "success") { + signOut({ callbackUrl: "/" }) + } + return res + } return ( => { + const session = await auth() if (!name) return { message: "Server error: company name is null.", status: "error" } const enteredName = formData.get("name")?.toString().trim() @@ -256,6 +258,10 @@ export const deleteCompany = companyOnlyAction( return { message: "A database error occurred. Please try again later.", status: "error" } } - redirect("/companies") + if (session!.user.role === Role.ADMIN) { + redirect("/companies") + } else { + return { message: "Company deleted successfully.", status: "success" } + } }, ) From 110d826db420981fa27cec50c9f89cb1d931e1d2 Mon Sep 17 00:00:00 2001 From: Matthew Alex Date: Mon, 2 Sep 2024 14:31:50 +0100 Subject: [PATCH 2/5] feat: allow company users to delete themselves --- app/companies/[slug]/DeleteUserButton.tsx | 19 ++++++------------- lib/crud/users.ts | 4 ---- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/app/companies/[slug]/DeleteUserButton.tsx b/app/companies/[slug]/DeleteUserButton.tsx index baa9fa29..044f7a51 100644 --- a/app/companies/[slug]/DeleteUserButton.tsx +++ b/app/companies/[slug]/DeleteUserButton.tsx @@ -7,20 +7,22 @@ import { deleteUser } from "@/lib/crud/users" import { User } from "@prisma/client" import { Button, Dialog, Flex, Spinner } from "@radix-ui/themes" -import { useSession } from "next-auth/react" +import { signOut, useSession } from "next-auth/react" import React, { useState, useTransition } from "react" export const DeleteUserButton = ({ user }: { user: Pick }) => { + const { data: session } = useSession() const [openState, setOpenState] = useState(false) const [isPending, startTransition] = useTransition() const [serverMessage, setServerMessage] = useState("") - const { data: session } = useSession() - const handleDelete = async () => { startTransition(async () => { const { status, message } = await deleteUser(user.id, window.location.pathname) if (status === "success") { + if (user.id === session?.user.id) { + signOut({ callbackUrl: "/" }) + } setOpenState(false) } else { setServerMessage(message || "Server error") @@ -31,16 +33,7 @@ export const DeleteUserButton = ({ user }: { user: Pick }) return ( - + Are you sure? diff --git a/lib/crud/users.ts b/lib/crud/users.ts index 0ec265ef..001491ed 100644 --- a/lib/crud/users.ts +++ b/lib/crud/users.ts @@ -23,10 +23,6 @@ export const allowedToDeleteUser = async (userId: string): Promise Date: Mon, 2 Sep 2024 16:07:15 +0100 Subject: [PATCH 3/5] Update app/companies/[slug]/DeleteUserButton.tsx Co-authored-by: Alexander Biraben-Renard --- app/companies/[slug]/DeleteUserButton.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/companies/[slug]/DeleteUserButton.tsx b/app/companies/[slug]/DeleteUserButton.tsx index 044f7a51..0c081a91 100644 --- a/app/companies/[slug]/DeleteUserButton.tsx +++ b/app/companies/[slug]/DeleteUserButton.tsx @@ -21,7 +21,7 @@ export const DeleteUserButton = ({ user }: { user: Pick }) const { status, message } = await deleteUser(user.id, window.location.pathname) if (status === "success") { if (user.id === session?.user.id) { - signOut({ callbackUrl: "/" }) + await signOut({ callbackUrl: "/" }) } setOpenState(false) } else { From 839771dd6d1af19fee45c4cf77660a4414cc3ae0 Mon Sep 17 00:00:00 2001 From: Matthew Alex Date: Mon, 2 Sep 2024 16:07:21 +0100 Subject: [PATCH 4/5] Update components/DeleteCompany.tsx Co-authored-by: Alexander Biraben-Renard --- components/DeleteCompany.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/DeleteCompany.tsx b/components/DeleteCompany.tsx index 5d8f428e..6b66bc8c 100644 --- a/components/DeleteCompany.tsx +++ b/components/DeleteCompany.tsx @@ -22,7 +22,7 @@ const DeleteCompanyForm = ({ close, name, id }: DeleteCompanyFormProps) => { const deleteCompanyWithName = async (prevState: FormPassBackState, formData: FormData) => { const res = await deleteCompany(prevState, formData, id, name) if (res.status === "success") { - signOut({ callbackUrl: "/" }) + await signOut({ callbackUrl: "/" }) } return res } From b4f7b588741ce390db1e989247559af23dcebd7a Mon Sep 17 00:00:00 2001 From: Matthew Alex Date: Tue, 3 Sep 2024 10:28:35 +0100 Subject: [PATCH 5/5] chore: await signout when deleting student Co-authored-by: IlliaDerevianko --- components/DeleteStudent.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/DeleteStudent.tsx b/components/DeleteStudent.tsx index 6a69396b..c1c0aa42 100644 --- a/components/DeleteStudent.tsx +++ b/components/DeleteStudent.tsx @@ -24,7 +24,7 @@ const DeleteStudentForm = ({ close, id }: DeleteStudentFormProps) => { const res = await deleteStudent(prevState, formData, id) if (res.status === "success") { - signOut({ callbackUrl: "/" }) + await signOut({ callbackUrl: "/" }) } return res