@@ -17,11 +17,21 @@ export const CippApiDialog = (props) => {
17
17
row = { } ,
18
18
relatedQueryKeys,
19
19
dialogAfterEffect,
20
+ allowResubmit = false ,
20
21
...other
21
22
} = props ;
22
23
const router = useRouter ( ) ;
23
24
const [ addedFieldData , setAddedFieldData ] = useState ( { } ) ;
24
25
const [ partialResults , setPartialResults ] = useState ( [ ] ) ;
26
+ const [ isFormSubmitted , setIsFormSubmitted ] = useState ( false ) ;
27
+
28
+ useEffect ( ( ) => {
29
+ if ( createDialog . open ) {
30
+ setIsFormSubmitted ( false ) ;
31
+ formHook . reset ( ) ;
32
+ }
33
+ } , [ createDialog . open ] ) ;
34
+
25
35
const [ getRequestInfo , setGetRequestInfo ] = useState ( {
26
36
url : "" ,
27
37
waiting : false ,
@@ -103,6 +113,7 @@ export const CippApiDialog = (props) => {
103
113
} ;
104
114
const tenantFilter = useSettings ( ) . currentTenant ;
105
115
const handleActionClick = ( row , action , formData ) => {
116
+ setIsFormSubmitted ( true ) ;
106
117
if ( action . multiPost === undefined ) {
107
118
action . multiPost = false ;
108
119
}
@@ -207,14 +218,63 @@ export const CippApiDialog = (props) => {
207
218
const onSubmit = ( data ) => handleActionClick ( row , api , data ) ;
208
219
const selectedType = api . type === "POST" ? actionPostRequest : actionGetRequest ;
209
220
221
+ useEffect ( ( ) => {
222
+ if ( api ?. setDefaultValues && createDialog . open ) {
223
+ fields . map ( ( field ) => {
224
+ if (
225
+ ( ( typeof row [ field . name ] === "string" && field . type === "textField" ) ||
226
+ ( typeof row [ field . name ] === "boolean" && field . type === "switch" ) ) &&
227
+ row [ field . name ] !== undefined &&
228
+ row [ field . name ] !== null &&
229
+ row [ field . name ] !== ""
230
+ ) {
231
+ formHook . setValue ( field . name , row [ field . name ] ) ;
232
+ } else if ( Array . isArray ( row [ field . name ] ) && field . type === "autoComplete" ) {
233
+ var values = [ ] ;
234
+ row [ field . name ] . map ( ( element ) => {
235
+ if ( element . label && element . value ) {
236
+ values . push ( element ) ;
237
+ } else if ( typeof element === "string" || typeof element === "number" ) {
238
+ values . push ( {
239
+ label : element ,
240
+ value : element ,
241
+ } ) ;
242
+ }
243
+ } ) ;
244
+ formHook . setValue ( field . name , values ) ;
245
+ } else if (
246
+ field . type === "autoComplete" &&
247
+ row [ field . name ] !== "" &&
248
+ ( typeof row [ field . name ] === "string" ||
249
+ ( typeof row [ field . name ] === "object" &&
250
+ row [ field . name ] !== undefined &&
251
+ row [ field . name ] !== null ) )
252
+ ) {
253
+ if ( typeof row [ field . name ] === "string" ) {
254
+ formHook . setValue ( field . name , {
255
+ label : row [ field . name ] ,
256
+ value : row [ field . name ] ,
257
+ } ) ;
258
+ } else if (
259
+ typeof row [ field . name ] === "object" &&
260
+ row [ field . name ] ?. label &&
261
+ row [ field . name ] ?. value
262
+ ) {
263
+ formHook . setValue ( field . name , row [ field . name ] ) ;
264
+ }
265
+ }
266
+ } ) ;
267
+ }
268
+ } , [ createDialog . open , api ?. setDefaultValues ] ) ;
269
+
270
+ const getNestedValue = ( obj , path ) => {
271
+ return path
272
+ . split ( "." )
273
+ . reduce ( ( acc , key ) => ( acc && acc [ key ] !== undefined ? acc [ key ] : undefined ) , obj ) ;
274
+ } ;
275
+
210
276
// Handling link navigation
211
277
if ( api . link ) {
212
- const getNestedValue = ( obj , path ) => {
213
- return path
214
- . split ( "." )
215
- . reduce ( ( acc , key ) => ( acc && acc [ key ] !== undefined ? acc [ key ] : undefined ) , obj ) ;
216
- } ;
217
-
218
278
const linkWithRowData = api . link . replace ( / \[ ( [ ^ \] ] + ) \] / g, ( _ , key ) => {
219
279
return getNestedValue ( row , key ) || `[${ key } ]` ;
220
280
} ) ;
@@ -239,17 +299,35 @@ export const CippApiDialog = (props) => {
239
299
setPartialResults ( [ ] ) ;
240
300
} ;
241
301
302
+ var confirmText ;
303
+ if ( typeof api ?. confirmText === "string" && ! Array . isArray ( row ) ) {
304
+ confirmText = api . confirmText . replace ( / \[ ( [ ^ \] ] + ) \] / g, ( _ , key ) => {
305
+ return getNestedValue ( row , key ) || `[${ key } ]` ;
306
+ } ) ;
307
+ } else if ( Array . isArray ( row ) && row . length > 1 ) {
308
+ confirmText = api . confirmText . replace ( / \[ ( [ ^ \] ] + ) \] / g, "the selected rows" ) ;
309
+ } else if ( Array . isArray ( row ) && row . length === 1 ) {
310
+ confirmText = api . confirmText . replace ( / \[ ( [ ^ \] ] + ) \] / g, ( _ , key ) => {
311
+ return getNestedValue ( row [ 0 ] , key ) || `[${ key } ]` ;
312
+ } ) ;
313
+ } else {
314
+ confirmText = api . confirmText ;
315
+ }
316
+
242
317
return (
243
318
< Dialog fullWidth maxWidth = "sm" onClose = { handleClose } open = { createDialog . open } >
244
319
< form onSubmit = { formHook . handleSubmit ( onSubmit ) } >
245
320
< DialogTitle > { title } </ DialogTitle >
246
321
< DialogContent >
247
- < Stack spacing = { 3 } > { api . confirmText } </ Stack >
322
+ < Stack spacing = { 3 } > { confirmText } </ Stack >
248
323
</ DialogContent >
249
324
< DialogContent >
250
325
< Grid container spacing = { 2 } >
251
326
{ fields &&
252
327
fields . map ( ( fieldProps , index ) => {
328
+ if ( fieldProps ?. api ?. processFieldData ) {
329
+ fieldProps . api . data = processActionData ( fieldProps . api . data , row ) ;
330
+ }
253
331
return (
254
332
< Grid item xs = { 12 } key = { index } >
255
333
< CippFormComponent
@@ -270,8 +348,8 @@ export const CippApiDialog = (props) => {
270
348
< Button color = "inherit" onClick = { ( ) => handleClose ( ) } >
271
349
Close
272
350
</ Button >
273
- < Button variant = "contained" type = "submit" >
274
- Confirm
351
+ < Button variant = "contained" type = "submit" disabled = { isFormSubmitted && ! allowResubmit } >
352
+ { isFormSubmitted && allowResubmit ? "Reconfirm" : " Confirm" }
275
353
</ Button >
276
354
</ DialogActions >
277
355
</ form >
0 commit comments