22
33import styled from '@emotion/styled' ;
44import { zodResolver } from '@hookform/resolvers/zod' ;
5- import { Save } from '@mui/icons-material' ;
5+ import { NextPlan , Save } from '@mui/icons-material' ;
66import { Box , Button , FormHelperText , TextField } from '@mui/material' ;
77import React , { useEffect , useState } from 'react' ;
88import { Controller , SubmitHandler , useForm } from 'react-hook-form' ;
99import { z } from 'zod' ;
1010
11+ import useConfirmationDialog from '@/hooks/useConfirmDialog' ;
12+
1113import { AlertBar , AlertBarProps } from '@/components/shared/AlertBar' ;
14+ import SubmitButton from '@/components/shared/SubmitButton' ;
1215
1316import { consoleLog } from '@/utils/shared/console-log' ;
1417import { getApiResponse } from '@/utils/shared/get-api-response' ;
@@ -37,22 +40,27 @@ type FormValues = z.infer<typeof zodSchema>;
3740const ReactHookForm : React . FC = ( ) => {
3841 const apiEndpoint = '/api/test' ;
3942 const [ apiResult , setApiResult ] = React . useState < FormValues > ( ) ;
43+ const [ isSubmitting , setIsSubmitting ] = React . useState ( false ) ;
4044
4145 const [ alertBarProps , setAlertBarProps ] = useState < AlertBarProps > ( {
4246 message : '' ,
4347 severity : 'info' ,
4448 } ) ;
4549
50+ const dialog = useConfirmationDialog ( ) ;
51+
4652 const {
4753 handleSubmit,
4854 control,
4955 formState : { errors, isValid } ,
56+ setValue,
5057 } = useForm < FormValues > ( {
5158 resolver : zodResolver ( zodSchema ) ,
5259 } ) ;
5360
5461 const onSubmit : SubmitHandler < FormValues > = async ( data ) => {
5562 try {
63+ setIsSubmitting ( true ) ;
5664 const result = await getApiResponse < {
5765 reqData : FormValues ;
5866 } > ( {
@@ -62,14 +70,16 @@ const ReactHookForm: React.FC = () => {
6270 } ) ;
6371 setApiResult ( result ?. reqData ) ;
6472 consoleLog ( 'getApiResponse result' , result , errors ) ;
73+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
74+ setIsSubmitting ( false ) ;
6575
6676 setAlertBarProps ( {
6777 message : 'Form submitted successfully' ,
6878 severity : 'success' ,
6979 } ) ;
7080 } catch ( error ) {
7181 consoleLog ( 'handleSubmit ERROR' , error ) ;
72-
82+ setIsSubmitting ( false ) ;
7383 setAlertBarProps ( {
7484 message : 'Form submission failed' ,
7585 severity : 'error' ,
@@ -90,7 +100,6 @@ const ReactHookForm: React.FC = () => {
90100 return (
91101 < StyledForm onSubmit = { handleSubmit ( onSubmit ) } >
92102 < Box sx = { { m : 2 } } >
93- { /* <label>Name:</label> */ }
94103 < Controller
95104 name = 'name'
96105 control = { control }
@@ -107,7 +116,6 @@ const ReactHookForm: React.FC = () => {
107116 </ Box >
108117
109118 < Box sx = { { m : 2 } } >
110- { /* <label>Email:</label> */ }
111119 < Controller
112120 name = 'email'
113121 control = { control }
@@ -122,19 +130,48 @@ const ReactHookForm: React.FC = () => {
122130 </ FormHelperText >
123131 ) }
124132 </ Box >
125- { apiResult && (
133+ { apiResult && ! isSubmitting && (
126134 < Box sx = { { m : 2 , color : 'green' } } >
127135 API result from { apiEndpoint } : { apiResult . name } & { apiResult . email }
128136 </ Box >
129137 ) }
130- < Button variant = 'contained' type = 'submit' startIcon = { < Save /> } >
131- Test react hook form with zod
132- </ Button >
138+
139+ < SubmitButton
140+ isSubmitting = { isSubmitting }
141+ submittingText = 'Fetching API ...'
142+ >
143+ < Button variant = 'contained' type = 'submit' startIcon = { < Save /> } >
144+ Test react hook form with zod
145+ </ Button >
146+ </ SubmitButton >
147+
148+ < Box sx = { { m : 5 } } >
149+ < Button
150+ variant = 'outlined'
151+ onClick = { ( ) => {
152+ const randomNumber = Math . floor ( Math . random ( ) * 90 ) + 10 ;
153+ dialog . openConfirmDialog ( {
154+ title : 'Change form name' ,
155+ content : `Are you sure to change above form name to Alex ${ randomNumber } and submit?` ,
156+ onConfirm : ( ) => {
157+ setValue ( 'name' , `Alex ${ randomNumber } ` ) ;
158+ setValue ( 'email' , 'alex@test.com' ) ;
159+ handleSubmit ( onSubmit ) ( ) ;
160+ } ,
161+ } ) ;
162+ } }
163+ endIcon = { < NextPlan /> }
164+ >
165+ Test MUI confirmation dialog
166+ </ Button >
167+ </ Box >
133168
134169 < AlertBar
135170 onClose = { ( ) => setAlertBarProps ( { message : '' } ) }
136171 { ...alertBarProps }
137172 />
173+
174+ { dialog . renderConfirmationDialog ( ) }
138175 </ StyledForm >
139176 ) ;
140177} ;
0 commit comments