← full-stack-fastapi-template / frontend/src/components/UserSettings/DeleteConfirmation.tsx
| 1 | import { useMutation, useQueryClient } from "@tanstack/react-query" |
| 2 | import { useForm } from "react-hook-form" |
| 3 | |
| 4 | import { UsersService } from "@/client" |
| 5 | import { Button } from "@/components/ui/button" |
| 6 | import { |
| 7 | Dialog, |
| 8 | DialogClose, |
| 9 | DialogContent, |
| 10 | DialogDescription, |
| 11 | DialogFooter, |
| 12 | DialogHeader, |
| 13 | DialogTitle, |
| 14 | DialogTrigger, |
| 15 | } from "@/components/ui/dialog" |
| 16 | import { LoadingButton } from "@/components/ui/loading-button" |
| 17 | import useAuth from "@/hooks/useAuth" |
| 18 | import useCustomToast from "@/hooks/useCustomToast" |
| 19 | import { handleError } from "@/utils" |
| 20 | |
| 21 | const DeleteConfirmation = () => { |
| 22 | const queryClient = useQueryClient() |
| 23 | const { showSuccessToast, showErrorToast } = useCustomToast() |
| 24 | const { handleSubmit } = useForm() |
| 25 | const { logout } = useAuth() |
| 26 | |
| 27 | const mutation = useMutation({ |
| 28 | mutationFn: () => UsersService.deleteUserMe(), |
| 29 | onSuccess: () => { |
| 30 | showSuccessToast("Your account has been successfully deleted") |
| 31 | logout() |
| 32 | }, |
| 33 | onError: handleError.bind(showErrorToast), |
| 34 | onSettled: () => { |
| 35 | queryClient.invalidateQueries({ queryKey: ["currentUser"] }) |
| 36 | }, |
| 37 | }) |
| 38 | |
| 39 | const onSubmit = async () => { |
| 40 | mutation.mutate() |
| 41 | } |
| 42 | |
| 43 | return ( |
| 44 | <Dialog> |
| 45 | <DialogTrigger asChild> |
| 46 | <Button variant="destructive" className="mt-3"> |
| 47 | Delete Account |
| 48 | </Button> |
| 49 | </DialogTrigger> |
| 50 | <DialogContent> |
| 51 | <form onSubmit={handleSubmit(onSubmit)}> |
| 52 | <DialogHeader> |
| 53 | <DialogTitle>Confirmation Required</DialogTitle> |
| 54 | <DialogDescription> |
| 55 | All your account data will be{" "} |
| 56 | <strong>permanently deleted.</strong> If you are sure, please |
| 57 | click <strong>"Confirm"</strong> to proceed. This action cannot be |
| 58 | undone. |
| 59 | </DialogDescription> |
| 60 | </DialogHeader> |
| 61 | |
| 62 | <DialogFooter className="mt-4"> |
| 63 | <DialogClose asChild> |
| 64 | <Button variant="outline" disabled={mutation.isPending}> |
| 65 | Cancel |
| 66 | </Button> |
| 67 | </DialogClose> |
| 68 | <LoadingButton |
| 69 | variant="destructive" |
| 70 | type="submit" |
| 71 | loading={mutation.isPending} |
| 72 | > |
| 73 | Delete |
| 74 | </LoadingButton> |
| 75 | </DialogFooter> |
| 76 | </form> |
| 77 | </DialogContent> |
| 78 | </Dialog> |
| 79 | ) |
| 80 | } |
| 81 | |
| 82 | export default DeleteConfirmation |
| 83 |