2024-03-08 14:58:36 +01:00
|
|
|
import {
|
|
|
|
AlertDialog,
|
|
|
|
AlertDialogBody,
|
|
|
|
AlertDialogContent,
|
|
|
|
AlertDialogFooter,
|
|
|
|
AlertDialogHeader,
|
|
|
|
AlertDialogOverlay,
|
|
|
|
Button,
|
2024-03-17 17:28:45 +01:00
|
|
|
} from "@chakra-ui/react"
|
2024-04-08 15:49:22 -05:00
|
|
|
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
2024-03-17 17:28:45 +01:00
|
|
|
import React from "react"
|
|
|
|
import { useForm } from "react-hook-form"
|
2024-03-08 14:58:36 +01:00
|
|
|
|
2024-04-06 18:26:12 -05:00
|
|
|
import { type ApiError, type UserPublic, UsersService } from "../../client"
|
2024-03-17 17:28:45 +01:00
|
|
|
import useAuth from "../../hooks/useAuth"
|
|
|
|
import useCustomToast from "../../hooks/useCustomToast"
|
2024-02-16 13:38:00 -05:00
|
|
|
|
|
|
|
interface DeleteProps {
|
2024-03-08 14:58:36 +01:00
|
|
|
isOpen: boolean
|
|
|
|
onClose: () => void
|
2024-02-16 13:38:00 -05:00
|
|
|
}
|
|
|
|
|
2024-03-28 20:22:28 -05:00
|
|
|
const DeleteConfirmation = ({ isOpen, onClose }: DeleteProps) => {
|
2024-03-08 14:58:36 +01:00
|
|
|
const queryClient = useQueryClient()
|
|
|
|
const showToast = useCustomToast()
|
|
|
|
const cancelRef = React.useRef<HTMLButtonElement | null>(null)
|
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
formState: { isSubmitting },
|
|
|
|
} = useForm()
|
2024-04-06 18:26:12 -05:00
|
|
|
const currentUser = queryClient.getQueryData<UserPublic>(["currentUser"])
|
2024-03-08 14:58:36 +01:00
|
|
|
const { logout } = useAuth()
|
|
|
|
|
2024-04-04 16:30:42 +02:00
|
|
|
const mutation = useMutation({
|
|
|
|
mutationFn: (id: number) => UsersService.deleteUser({ userId: id }),
|
|
|
|
onSuccess: () => {
|
|
|
|
showToast(
|
|
|
|
"Success",
|
|
|
|
"Your account has been successfully deleted.",
|
|
|
|
"success",
|
|
|
|
)
|
|
|
|
logout()
|
|
|
|
onClose()
|
2024-03-08 14:58:36 +01:00
|
|
|
},
|
2024-04-04 16:30:42 +02:00
|
|
|
onError: (err: ApiError) => {
|
|
|
|
const errDetail = (err.body as any)?.detail
|
|
|
|
showToast("Something went wrong.", `${errDetail}`, "error")
|
|
|
|
},
|
|
|
|
onSettled: () => {
|
|
|
|
queryClient.invalidateQueries({ queryKey: ["currentUser"] })
|
|
|
|
},
|
|
|
|
})
|
2024-03-08 14:58:36 +01:00
|
|
|
|
|
|
|
const onSubmit = async () => {
|
|
|
|
mutation.mutate(currentUser!.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<AlertDialog
|
|
|
|
isOpen={isOpen}
|
|
|
|
onClose={onClose}
|
|
|
|
leastDestructiveRef={cancelRef}
|
2024-03-17 17:28:45 +01:00
|
|
|
size={{ base: "sm", md: "md" }}
|
2024-03-08 14:58:36 +01:00
|
|
|
isCentered
|
|
|
|
>
|
|
|
|
<AlertDialogOverlay>
|
|
|
|
<AlertDialogContent as="form" onSubmit={handleSubmit(onSubmit)}>
|
|
|
|
<AlertDialogHeader>Confirmation Required</AlertDialogHeader>
|
|
|
|
|
|
|
|
<AlertDialogBody>
|
2024-03-17 17:28:45 +01:00
|
|
|
All your account data will be{" "}
|
2024-03-08 14:58:36 +01:00
|
|
|
<strong>permanently deleted.</strong> If you are sure, please
|
2024-03-09 21:13:31 +01:00
|
|
|
click <strong>"Confirm"</strong> to proceed. This action cannot be
|
|
|
|
undone.
|
2024-03-08 14:58:36 +01:00
|
|
|
</AlertDialogBody>
|
|
|
|
|
|
|
|
<AlertDialogFooter gap={3}>
|
2024-03-11 16:50:46 +01:00
|
|
|
<Button variant="danger" type="submit" isLoading={isSubmitting}>
|
2024-03-08 14:58:36 +01:00
|
|
|
Confirm
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
ref={cancelRef}
|
|
|
|
onClick={onClose}
|
|
|
|
isDisabled={isSubmitting}
|
|
|
|
>
|
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
</AlertDialogFooter>
|
|
|
|
</AlertDialogContent>
|
|
|
|
</AlertDialogOverlay>
|
|
|
|
</AlertDialog>
|
|
|
|
</>
|
|
|
|
)
|
2024-02-16 13:38:00 -05:00
|
|
|
}
|
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
export default DeleteConfirmation
|