11import { useForm } from '@inertiajs/react' ;
2- import { FormEventHandler , useRef , useState } from 'react' ;
2+ import { FormEventHandler , useCallback , useRef , useState } from 'react' ;
33
44import InputError from '@/components/input-error' ;
55import { ResponsiveModal } from '@/components/responsive-modal' ;
@@ -16,23 +16,30 @@ export default function DeleteUser() {
1616 const { data, setData, delete : destroy , processing, reset, errors, clearErrors } = useForm ( { password : '' } ) ;
1717 const [ isOpen , setIsOpen ] = useState < boolean > ( false ) ;
1818
19+ const handleModalClose = useCallback ( ( ) => {
20+ setIsOpen ( false ) ;
21+ clearErrors ( ) ;
22+ reset ( ) ;
23+ } , [ clearErrors , reset ] ) ;
24+
25+ const handleOpenChange = useCallback (
26+ ( open : boolean ) => {
27+ if ( ! open ) handleModalClose ( ) ;
28+ else setIsOpen ( true ) ;
29+ } ,
30+ [ handleModalClose ] ,
31+ ) ;
32+
1933 const deleteUser : FormEventHandler = ( e ) => {
2034 e . preventDefault ( ) ;
2135
2236 destroy ( route ( 'profile.destroy' ) , {
2337 preserveScroll : true ,
24- onSuccess : ( ) => closeModal ( ) ,
38+ onSuccess : ( ) => handleModalClose ( ) ,
2539 onError : ( ) => passwordInput . current ?. focus ( ) ,
2640 onFinish : ( ) => reset ( ) ,
2741 } ) ;
2842 } ;
29-
30- const closeModal = ( ) => {
31- setIsOpen ( false ) ;
32- clearErrors ( ) ;
33- reset ( ) ;
34- } ;
35-
3643 return (
3744 < div className = "space-y-6" >
3845 < HeadingSmall title = "Delete account" description = "Delete your account and all of its resources" />
@@ -46,7 +53,7 @@ export default function DeleteUser() {
4653 Delete account
4754 </ Button >
4855
49- < ResponsiveModal open = { isOpen } onOpenChange = { setIsOpen } >
56+ < ResponsiveModal open = { isOpen } onOpenChange = { handleOpenChange } >
5057 < div className = "p-4 sm:p-6" >
5158 < DialogTitle > Are you sure you want to delete your account?</ DialogTitle >
5259 < DialogDescription className = "mt-2 mb-4" >
@@ -74,7 +81,7 @@ export default function DeleteUser() {
7481 </ div >
7582
7683 < DialogFooter className = "gap-2" >
77- < Button variant = "secondary" onClick = { closeModal } type = 'button' >
84+ < Button variant = "secondary" onClick = { handleModalClose } >
7885 Cancel
7986 </ Button >
8087
0 commit comments