1- import { Button , DialogTitle , Text } from "@chakra-ui/react"
21import { useMutation , useQueryClient } from "@tanstack/react-query"
2+ import { Trash2 } from "lucide-react"
33import { useState } from "react"
44import { useForm } from "react-hook-form"
5- import { FiTrash2 } from "react-icons/fi"
65
76import { UsersService } from "@/client"
7+ import { Button } from "@/components/ui/button"
88import {
9- DialogActionTrigger ,
10- DialogBody ,
11- DialogCloseTrigger ,
9+ Dialog ,
10+ DialogClose ,
1211 DialogContent ,
12+ DialogDescription ,
1313 DialogFooter ,
1414 DialogHeader ,
15- DialogRoot ,
16- DialogTrigger ,
15+ DialogTitle ,
1716} from "@/components/ui/dialog"
17+ import { DropdownMenuItem } from "@/components/ui/dropdown-menu"
18+ import { LoadingButton } from "@/components/ui/loading-button"
1819import useCustomToast from "@/hooks/useCustomToast"
20+ import { handleError } from "@/utils"
1921
20- const DeleteUser = ( { id } : { id : string } ) => {
22+ interface DeleteUserProps {
23+ id : string
24+ onSuccess : ( ) => void
25+ }
26+
27+ const DeleteUser = ( { id, onSuccess } : DeleteUserProps ) => {
2128 const [ isOpen , setIsOpen ] = useState ( false )
2229 const queryClient = useQueryClient ( )
2330 const { showSuccessToast, showErrorToast } = useCustomToast ( )
2431 const {
2532 handleSubmit,
26- formState : { isSubmitting } ,
2733 } = useForm ( )
2834
2935 const deleteUser = async ( id : string ) => {
@@ -35,10 +41,9 @@ const DeleteUser = ({ id }: { id: string }) => {
3541 onSuccess : ( ) => {
3642 showSuccessToast ( "The user was deleted successfully" )
3743 setIsOpen ( false )
44+ onSuccess ( )
3845 } ,
39- onError : ( ) => {
40- showErrorToast ( "An error occurred while deleting the user" )
41- } ,
46+ onError : handleError . bind ( showErrorToast ) ,
4247 onSettled : ( ) => {
4348 queryClient . invalidateQueries ( )
4449 } ,
@@ -49,55 +54,43 @@ const DeleteUser = ({ id }: { id: string }) => {
4954 }
5055
5156 return (
52- < DialogRoot
53- size = { { base : "xs" , md : "md" } }
54- placement = "center"
55- role = "alertdialog"
56- open = { isOpen }
57- onOpenChange = { ( { open } ) => setIsOpen ( open ) }
58- >
59- < DialogTrigger asChild >
60- < Button variant = "ghost" size = "sm" colorPalette = "red" >
61- < FiTrash2 fontSize = "16px" />
62- Delete User
63- </ Button >
64- </ DialogTrigger >
65- < DialogContent >
57+ < Dialog open = { isOpen } onOpenChange = { setIsOpen } >
58+ < DropdownMenuItem
59+ variant = "destructive"
60+ onSelect = { ( e ) => e . preventDefault ( ) }
61+ onClick = { ( ) => setIsOpen ( true ) }
62+ >
63+ < Trash2 />
64+ Delete User
65+ </ DropdownMenuItem >
66+ < DialogContent className = "sm:max-w-md" >
6667 < form onSubmit = { handleSubmit ( onSubmit ) } >
6768 < DialogHeader >
6869 < DialogTitle > Delete User</ DialogTitle >
69- </ DialogHeader >
70- < DialogBody >
71- < Text mb = { 4 } >
70+ < DialogDescription >
7271 All items associated with this user will also be{ " " }
7372 < strong > permanently deleted.</ strong > Are you sure? You will not
7473 be able to undo this action.
75- </ Text >
76- </ DialogBody >
74+ </ DialogDescription >
75+ </ DialogHeader >
7776
78- < DialogFooter gap = { 2 } >
79- < DialogActionTrigger asChild >
80- < Button
81- variant = "subtle"
82- colorPalette = "gray"
83- disabled = { isSubmitting }
84- >
77+ < DialogFooter className = "mt-4" >
78+ < DialogClose asChild >
79+ < Button variant = "outline" disabled = { mutation . isPending } >
8580 Cancel
8681 </ Button >
87- </ DialogActionTrigger >
88- < Button
89- variant = "solid"
90- colorPalette = "red"
82+ </ DialogClose >
83+ < LoadingButton
84+ variant = "destructive"
9185 type = "submit"
92- loading = { isSubmitting }
86+ loading = { mutation . isPending }
9387 >
9488 Delete
95- </ Button >
89+ </ LoadingButton >
9690 </ DialogFooter >
97- < DialogCloseTrigger />
9891 </ form >
9992 </ DialogContent >
100- </ DialogRoot >
93+ </ Dialog >
10194 )
10295}
10396
0 commit comments