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-03-07 19:16:23 +01:00
|
|
|
import { useMutation, useQueryClient } from 'react-query';
|
|
|
|
|
|
|
|
import { ApiError, UserOut, UsersService } from '../../client';
|
2024-02-27 15:47:42 -05:00
|
|
|
import useAuth from '../../hooks/useAuth';
|
2024-02-26 09:39:09 -05:00
|
|
|
import useCustomToast from '../../hooks/useCustomToast';
|
2024-02-16 13:38:00 -05:00
|
|
|
|
|
|
|
interface DeleteProps {
|
|
|
|
isOpen: boolean;
|
|
|
|
onClose: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DeleteConfirmation: React.FC<DeleteProps> = ({ isOpen, onClose }) => {
|
2024-03-07 19:16:23 +01:00
|
|
|
const queryClient = useQueryClient();
|
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();
|
2024-03-07 19:16:23 +01:00
|
|
|
const currentUser = queryClient.getQueryData<UserOut>('currentUser');
|
2024-02-27 15:47:42 -05:00
|
|
|
const { logout } = useAuth();
|
2024-02-16 13:38:00 -05:00
|
|
|
|
2024-03-07 19:16:23 +01:00
|
|
|
const deleteCurrentUser = async (id: number) => {
|
|
|
|
await UsersService.deleteUser({ userId: id });
|
|
|
|
}
|
|
|
|
|
|
|
|
const mutation = useMutation(deleteCurrentUser, {
|
|
|
|
onSuccess: () => {
|
|
|
|
showToast('Success', 'Your account has been successfully deleted.', 'success');
|
2024-02-27 15:47:42 -05:00
|
|
|
logout();
|
2024-02-16 13:38:00 -05:00
|
|
|
onClose();
|
2024-03-07 19:16:23 +01:00
|
|
|
},
|
|
|
|
onError: (err: ApiError) => {
|
|
|
|
const errDetail = err.body.detail;
|
2024-02-27 15:47:42 -05:00
|
|
|
showToast('Something went wrong.', `${errDetail}`, 'error');
|
2024-03-07 19:16:23 +01:00
|
|
|
},
|
|
|
|
onSettled: () => {
|
|
|
|
queryClient.invalidateQueries('currentUser');
|
2024-02-16 13:38:00 -05:00
|
|
|
}
|
2024-03-07 19:16:23 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const onSubmit = async () => {
|
|
|
|
mutation.mutate(currentUser!.id);
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|