2024-02-28 18:30:59 -05:00
|
|
|
import React from 'react';
|
2024-02-16 13:38:00 -05:00
|
|
|
|
2024-02-26 09:39:09 -05:00
|
|
|
import { AlertDialog, AlertDialogBody, AlertDialogContent, AlertDialogFooter, AlertDialogHeader, AlertDialogOverlay, Button } from '@chakra-ui/react';
|
2024-02-16 13:38:00 -05:00
|
|
|
import { useForm } from 'react-hook-form';
|
2024-02-27 15:47:42 -05:00
|
|
|
import { ApiError } from '../../client';
|
|
|
|
import useAuth from '../../hooks/useAuth';
|
2024-02-26 09:39:09 -05:00
|
|
|
import useCustomToast from '../../hooks/useCustomToast';
|
2024-02-27 15:47:42 -05:00
|
|
|
import { useUserStore } from '../../store/user-store';
|
2024-02-16 13:38:00 -05:00
|
|
|
|
|
|
|
interface DeleteProps {
|
|
|
|
isOpen: boolean;
|
|
|
|
onClose: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DeleteConfirmation: React.FC<DeleteProps> = ({ isOpen, onClose }) => {
|
2024-02-26 09:39:09 -05:00
|
|
|
const showToast = useCustomToast();
|
2024-02-16 13:38:00 -05:00
|
|
|
const cancelRef = React.useRef<HTMLButtonElement | null>(null);
|
2024-02-27 15:47:42 -05:00
|
|
|
const { handleSubmit, formState: { isSubmitting } } = useForm();
|
|
|
|
const { user, deleteUser } = useUserStore();
|
|
|
|
const { logout } = useAuth();
|
2024-02-16 13:38:00 -05:00
|
|
|
|
|
|
|
const onSubmit = async () => {
|
|
|
|
try {
|
2024-02-27 15:47:42 -05:00
|
|
|
await deleteUser(user!.id);
|
|
|
|
logout();
|
2024-02-16 13:38:00 -05:00
|
|
|
onClose();
|
2024-02-27 15:47:42 -05:00
|
|
|
showToast('Success', 'Your account has been successfully deleted.', 'success');
|
2024-02-16 13:38:00 -05:00
|
|
|
} catch (err) {
|
2024-02-27 15:47:42 -05:00
|
|
|
const errDetail = (err as ApiError).body.detail;
|
|
|
|
showToast('Something went wrong.', `${errDetail}`, 'error');
|
2024-02-16 13:38:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<AlertDialog
|
|
|
|
isOpen={isOpen}
|
|
|
|
onClose={onClose}
|
|
|
|
leastDestructiveRef={cancelRef}
|
2024-02-26 09:39:09 -05:00
|
|
|
size={{ base: 'sm', md: 'md' }}
|
2024-02-16 13:38:00 -05:00
|
|
|
isCentered
|
|
|
|
>
|
|
|
|
<AlertDialogOverlay>
|
2024-02-26 09:39:09 -05:00
|
|
|
<AlertDialogContent as='form' onSubmit={handleSubmit(onSubmit)}>
|
2024-02-16 13:38:00 -05:00
|
|
|
<AlertDialogHeader>
|
|
|
|
Confirmation Required
|
|
|
|
</AlertDialogHeader>
|
|
|
|
|
|
|
|
<AlertDialogBody>
|
2024-02-29 09:47:13 -05:00
|
|
|
All your account data will be <strong>permanently deleted.</strong> If you are sure, please click <strong>'Confirm'</strong> to proceed.
|
2024-02-16 13:38:00 -05:00
|
|
|
</AlertDialogBody>
|
|
|
|
|
|
|
|
<AlertDialogFooter gap={3}>
|
2024-02-27 15:47:42 -05:00
|
|
|
<Button bg='ui.danger' color='white' _hover={{ opacity: 0.8 }} type='submit' isLoading={isSubmitting}>
|
2024-02-16 13:38:00 -05:00
|
|
|
Confirm
|
|
|
|
</Button>
|
2024-02-28 18:30:59 -05:00
|
|
|
<Button ref={cancelRef} onClick={onClose} isDisabled={isSubmitting}>
|
2024-02-16 13:38:00 -05:00
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
</AlertDialogFooter>
|
|
|
|
</AlertDialogContent>
|
|
|
|
</AlertDialogOverlay>
|
|
|
|
</AlertDialog >
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DeleteConfirmation;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|