import { AlertDialog, AlertDialogBody, AlertDialogContent, AlertDialogFooter, AlertDialogHeader, AlertDialogOverlay, Button, } from "@chakra-ui/react" import { useMutation, useQueryClient } from "@tanstack/react-query" import React from "react" import { useForm } from "react-hook-form" import { type ApiError, type UserPublic, UsersService } from "../../client" import useAuth from "../../hooks/useAuth" import useCustomToast from "../../hooks/useCustomToast" interface DeleteProps { isOpen: boolean onClose: () => void } const DeleteConfirmation = ({ isOpen, onClose }: DeleteProps) => { const queryClient = useQueryClient() const showToast = useCustomToast() const cancelRef = React.useRef(null) const { handleSubmit, formState: { isSubmitting }, } = useForm() const currentUser = queryClient.getQueryData(["currentUser"]) const { logout } = useAuth() const mutation = useMutation({ mutationFn: (id: number) => UsersService.deleteUser({ userId: id }), onSuccess: () => { showToast( "Success", "Your account has been successfully deleted.", "success", ) logout() onClose() }, onError: (err: ApiError) => { const errDetail = (err.body as any)?.detail showToast("Something went wrong.", `${errDetail}`, "error") }, onSettled: () => { queryClient.invalidateQueries({ queryKey: ["currentUser"] }) }, }) const onSubmit = async () => { mutation.mutate(currentUser!.id) } return ( <> Confirmation Required All your account data will be{" "} permanently deleted. If you are sure, please click "Confirm" to proceed. This action cannot be undone. ) } export default DeleteConfirmation