@@ -17,11 +17,21 @@ export const CippApiDialog = (props) => {
1717 row = { } ,
1818 relatedQueryKeys,
1919 dialogAfterEffect,
20+ allowResubmit = false ,
2021 ...other
2122 } = props ;
2223 const router = useRouter ( ) ;
2324 const [ addedFieldData , setAddedFieldData ] = useState ( { } ) ;
2425 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+
2535 const [ getRequestInfo , setGetRequestInfo ] = useState ( {
2636 url : "" ,
2737 waiting : false ,
@@ -103,6 +113,7 @@ export const CippApiDialog = (props) => {
103113 } ;
104114 const tenantFilter = useSettings ( ) . currentTenant ;
105115 const handleActionClick = ( row , action , formData ) => {
116+ setIsFormSubmitted ( true ) ;
106117 if ( action . multiPost === undefined ) {
107118 action . multiPost = false ;
108119 }
@@ -207,14 +218,63 @@ export const CippApiDialog = (props) => {
207218 const onSubmit = ( data ) => handleActionClick ( row , api , data ) ;
208219 const selectedType = api . type === "POST" ? actionPostRequest : actionGetRequest ;
209220
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+
210276 // Handling link navigation
211277 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-
218278 const linkWithRowData = api . link . replace ( / \[ ( [ ^ \] ] + ) \] / g, ( _ , key ) => {
219279 return getNestedValue ( row , key ) || `[${ key } ]` ;
220280 } ) ;
@@ -239,17 +299,35 @@ export const CippApiDialog = (props) => {
239299 setPartialResults ( [ ] ) ;
240300 } ;
241301
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+
242317 return (
243318 < Dialog fullWidth maxWidth = "sm" onClose = { handleClose } open = { createDialog . open } >
244319 < form onSubmit = { formHook . handleSubmit ( onSubmit ) } >
245320 < DialogTitle > { title } </ DialogTitle >
246321 < DialogContent >
247- < Stack spacing = { 3 } > { api . confirmText } </ Stack >
322+ < Stack spacing = { 3 } > { confirmText } </ Stack >
248323 </ DialogContent >
249324 < DialogContent >
250325 < Grid container spacing = { 2 } >
251326 { fields &&
252327 fields . map ( ( fieldProps , index ) => {
328+ if ( fieldProps ?. api ?. processFieldData ) {
329+ fieldProps . api . data = processActionData ( fieldProps . api . data , row ) ;
330+ }
253331 return (
254332 < Grid item xs = { 12 } key = { index } >
255333 < CippFormComponent
@@ -270,8 +348,8 @@ export const CippApiDialog = (props) => {
270348 < Button color = "inherit" onClick = { ( ) => handleClose ( ) } >
271349 Close
272350 </ Button >
273- < Button variant = "contained" type = "submit" >
274- Confirm
351+ < Button variant = "contained" type = "submit" disabled = { isFormSubmitted && ! allowResubmit } >
352+ { isFormSubmitted && allowResubmit ? "Reconfirm" : " Confirm" }
275353 </ Button >
276354 </ DialogActions >
277355 </ form >
0 commit comments