← full-stack-fastapi-template  /  frontend/src/components/Admin/DeleteUser.tsx

1
import { useMutation, useQueryClient } from "@tanstack/react-query"
2
import { Trash2 } from "lucide-react"
3
import { useState } from "react"
4
import { useForm } from "react-hook-form"
5
6
import { UsersService } from "@/client"
7
import { Button } from "@/components/ui/button"
8
import {
9
  Dialog,
10
  DialogClose,
11
  DialogContent,
12
  DialogDescription,
13
  DialogFooter,
14
  DialogHeader,
15
  DialogTitle,
16
} from "@/components/ui/dialog"
17
import { DropdownMenuItem } from "@/components/ui/dropdown-menu"
18
import { LoadingButton } from "@/components/ui/loading-button"
19
import useCustomToast from "@/hooks/useCustomToast"
20
import { handleError } from "@/utils"
21
22
interface DeleteUserProps {
23
  id: string
24
  onSuccess: () => void
25
}
26
27
const DeleteUser = ({ id, onSuccess }: DeleteUserProps) => {
28
  const [isOpen, setIsOpen] = useState(false)
29
  const queryClient = useQueryClient()
30
  const { showSuccessToast, showErrorToast } = useCustomToast()
31
  const { handleSubmit } = useForm()
32
33
  const deleteUser = async (id: string) => {
34
    await UsersService.deleteUser({ userId: id })
35
  }
36
37
  const mutation = useMutation({
38
    mutationFn: deleteUser,
39
    onSuccess: () => {
40
      showSuccessToast("The user was deleted successfully")
41
      setIsOpen(false)
42
      onSuccess()
43
    },
44
    onError: handleError.bind(showErrorToast),
45
    onSettled: () => {
46
      queryClient.invalidateQueries()
47
    },
48
  })
49
50
  const onSubmit = async () => {
51
    mutation.mutate(id)
52
  }
53
54
  return (
55
    <Dialog open={isOpen} onOpenChange={setIsOpen}>
56
      <DropdownMenuItem
57
        variant="destructive"
58
        onSelect={(e) => e.preventDefault()}
59
        onClick={() => setIsOpen(true)}
60
      >
61
        <Trash2 />
62
        Delete User
63
      </DropdownMenuItem>
64
      <DialogContent className="sm:max-w-md">
65
        <form onSubmit={handleSubmit(onSubmit)}>
66
          <DialogHeader>
67
            <DialogTitle>Delete User</DialogTitle>
68
            <DialogDescription>
69
              All items associated with this user will also be{" "}
70
              <strong>permanently deleted.</strong> Are you sure? You will not
71
              be able to undo this action.
72
            </DialogDescription>
73
          </DialogHeader>
74
75
          <DialogFooter className="mt-4">
76
            <DialogClose asChild>
77
              <Button variant="outline" disabled={mutation.isPending}>
78
                Cancel
79
              </Button>
80
            </DialogClose>
81
            <LoadingButton
82
              variant="destructive"
83
              type="submit"
84
              loading={mutation.isPending}
85
            >
86
              Delete
87
            </LoadingButton>
88
          </DialogFooter>
89
        </form>
90
      </DialogContent>
91
    </Dialog>
92
  )
93
}
94
95
export default DeleteUser
96