From 37dfbd86f06ea06ca293dbe45e64ff5913f1d0b7 Mon Sep 17 00:00:00 2001 From: Sebastien Dubois Date: Fri, 6 Sep 2024 16:38:25 +0200 Subject: [PATCH] feat(ui): added delete user account in user profile. Closes #298 --- .../Pages/Profile/Partials/DeleteUserForm.tsx | 106 ++++++++++++++++++ .../Partials/UpdateProfileInformationForm.tsx | 2 +- apps/knowii/src/Pages/Profile/Show.tsx | 7 +- libs/common/src/lib/constants.ts | 1 + 4 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 apps/knowii/src/Pages/Profile/Partials/DeleteUserForm.tsx diff --git a/apps/knowii/src/Pages/Profile/Partials/DeleteUserForm.tsx b/apps/knowii/src/Pages/Profile/Partials/DeleteUserForm.tsx new file mode 100644 index 00000000..72fe98fe --- /dev/null +++ b/apps/knowii/src/Pages/Profile/Partials/DeleteUserForm.tsx @@ -0,0 +1,106 @@ +import { useRoute } from 'ziggy-js'; +import { FormEventHandler, useRef, useState } from 'react'; +import { useForm } from '@inertiajs/react'; +import { DELETE_USER_URL, DESTROY_OTHER_BROWSER_SESSIONS_URL } from '@knowii/common'; +import { Button } from 'primereact/button'; +import { FaLock } from 'react-icons/fa'; +import { InputText } from 'primereact/inputtext'; +import InputError from '@/Components/InputError'; +import ActionSection from '@/Components/ActionSection'; +import { Dialog } from 'primereact/dialog'; + +interface DeleteUserFormProps { + password: string; +} + +export default function DeleteUserForm() { + const route = useRoute(); + + const [confirmingUserDeletion, setConfirmingUserDeletion] = useState(false); + + const form = useForm({ + password: '', + }); + + const passwordRef = useRef(null); + + const confirmUserDeletion: FormEventHandler = () => { + setConfirmingUserDeletion(true); + + setTimeout(() => passwordRef.current?.focus(), 250); + }; + + const deleteUser: FormEventHandler = (e) => { + e.preventDefault(); + + form.delete(route(DELETE_USER_URL), { + preserveScroll: true, + onSuccess: () => closeModal(), + onError: () => passwordRef.current?.focus(), + onFinish: () => form.reset(), + }); + }; + + const logoutOtherBrowserSessions = () => { + form.delete(route(DESTROY_OTHER_BROWSER_SESSIONS_URL), { + preserveScroll: true, + // FIXME implement + onSuccess: () => closeModal(), + onError: () => passwordRef.current?.focus(), + onFinish: () => form.reset(), + }); + }; + + const closeModal = () => { + setConfirmingUserDeletion(false); + form.reset(); + }; + + return ( + +
+ Once your account is deleted, all of its resources and data will be permanently deleted. Before deleting your account, please + download any data or information that you wish to retain. +
+ +
+ +
+ + closeModal()} + footer={ + <> + +
+ ); +} diff --git a/apps/knowii/src/Pages/Profile/Partials/UpdateProfileInformationForm.tsx b/apps/knowii/src/Pages/Profile/Partials/UpdateProfileInformationForm.tsx index 6349445f..4fbe513b 100644 --- a/apps/knowii/src/Pages/Profile/Partials/UpdateProfileInformationForm.tsx +++ b/apps/knowii/src/Pages/Profile/Partials/UpdateProfileInformationForm.tsx @@ -105,7 +105,7 @@ export default function UpdateProfileInformationForm(props: Props) { {page.props.jetstream.hasEmailVerification && props.user.email_verified_at === null ? (

- Your email address is unverified.  + Your email address is unverified.  Your user profile}> - {page.props.jetstream.canUpdateProfileInformation ? : null} + {page.props.jetstream.canUpdateProfileInformation && } - {page.props.jetstream.canUpdatePassword ? : null} + {page.props.jetstream.canUpdatePassword && } + + {page.props.jetstream.hasAccountDeletionFeatures && } ); } diff --git a/libs/common/src/lib/constants.ts b/libs/common/src/lib/constants.ts index 8fe21920..7b946b02 100644 --- a/libs/common/src/lib/constants.ts +++ b/libs/common/src/lib/constants.ts @@ -39,6 +39,7 @@ export const FORGOT_PASSWORD_URL = 'password.request'; export const EMAIL_VERIFICATION_URL = 'verification.send'; export const EMAIL_VERIFICATION_STATUS_LINK_SENT = 'verification-link-sent'; export const DESTROY_OTHER_BROWSER_SESSIONS_URL = 'other-browser-sessions.destroy'; +export const DELETE_USER_URL = 'current-user.destroy'; /** * API */