5
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
6
*/
7
7
8
- import React from 'react' ;
8
+ import React , { useCallback , useState } from 'react' ;
9
9
import { FieldErrors , UseFormReturn } from 'react-hook-form' ;
10
10
import { FormattedMessage } from 'react-intl' ;
11
11
import { Dialog , DialogActions , DialogContent , DialogTitle , Grid , LinearProgress } from '@mui/material' ;
12
12
import * as yup from 'yup' ;
13
13
import SubmitButton from '../inputs/react-hook-form/utils/submit-button' ;
14
14
import CancelButton from '../inputs/react-hook-form/utils/cancel-button' ;
15
15
import CustomFormProvider , { MergedFormContextProps } from '../inputs/react-hook-form/provider/custom-form-provider' ;
16
+ import PopupConfirmationDialog from './popup-confirmation-dialog' ;
16
17
17
18
interface ICustomMuiDialog {
18
19
open : boolean ;
@@ -28,6 +29,7 @@ interface ICustomMuiDialog {
28
29
children : React . ReactNode ;
29
30
isDataFetching ?: boolean ;
30
31
language ?: string ;
32
+ confirmationMessageKey ?: string ;
31
33
}
32
34
33
35
const styles = {
@@ -54,13 +56,19 @@ function CustomMuiDialog({
54
56
onCancel,
55
57
children,
56
58
language,
59
+ confirmationMessageKey,
57
60
} : ICustomMuiDialog ) {
61
+ const [ openConfirmationPopup , setOpenConfirmationPopup ] = useState ( false ) ;
62
+ const [ validatedData , setValidatedData ] = useState ( undefined ) ;
58
63
const { handleSubmit } = formMethods ;
59
64
60
- const handleCancel = ( event : React . MouseEvent ) => {
61
- onCancel ?.( ) ;
62
- onClose ( event ) ;
63
- } ;
65
+ const handleCancel = useCallback (
66
+ ( event : React . MouseEvent ) => {
67
+ onCancel ?.( ) ;
68
+ onClose ( event ) ;
69
+ } ,
70
+ [ onCancel , onClose ]
71
+ ) ;
64
72
65
73
const handleClose = ( event : React . MouseEvent , reason ?: string ) => {
66
74
if ( reason === 'backdropClick' && onCancel ) {
@@ -69,10 +77,30 @@ function CustomMuiDialog({
69
77
onClose ( event ) ;
70
78
} ;
71
79
72
- const handleValidate = ( data : any ) => {
73
- onSave ( data ) ;
74
- onClose ( data ) ;
75
- } ;
80
+ const validate = useCallback (
81
+ ( data : any ) => {
82
+ onSave ( data ) ;
83
+ onClose ( data ) ;
84
+ } ,
85
+ [ onClose , onSave ]
86
+ ) ;
87
+
88
+ const handleValidate = useCallback (
89
+ ( data : any ) => {
90
+ if ( confirmationMessageKey ) {
91
+ setValidatedData ( data ) ;
92
+ setOpenConfirmationPopup ( true ) ;
93
+ } else {
94
+ validate ( data ) ;
95
+ }
96
+ } ,
97
+ [ confirmationMessageKey , validate ]
98
+ ) ;
99
+
100
+ const handlePopupConfirmation = useCallback ( ( ) => {
101
+ setOpenConfirmationPopup ( false ) ;
102
+ validate ( validatedData ) ;
103
+ } , [ validate , validatedData ] ) ;
76
104
77
105
const handleValidationError = ( errors : FieldErrors ) => {
78
106
onValidationError ?.( errors ) ;
@@ -102,6 +130,14 @@ function CustomMuiDialog({
102
130
/>
103
131
</ DialogActions >
104
132
</ Dialog >
133
+ { confirmationMessageKey && (
134
+ < PopupConfirmationDialog
135
+ message = { confirmationMessageKey }
136
+ openConfirmationPopup = { openConfirmationPopup }
137
+ setOpenConfirmationPopup = { setOpenConfirmationPopup }
138
+ handlePopupConfirmation = { handlePopupConfirmation }
139
+ />
140
+ ) }
105
141
</ CustomFormProvider >
106
142
) ;
107
143
}
0 commit comments