@@ -282,38 +282,36 @@ export const CippApiDialog = (props) => {
282282 . reduce ( ( acc , key ) => ( acc && acc [ key ] !== undefined ? acc [ key ] : undefined ) , obj ) ;
283283 } ;
284284
285- // Handling external link navigation
286- useEffect ( ( ) => {
287- if ( api . link && createDialog . open ) {
288- const linkWithRowData = api . link . replace ( / \[ ( [ ^ \] ] + ) \] / g, ( _ , key ) => {
289- return getNestedValue ( row , key ) || `[${ key } ]` ;
290- } ) ;
285+ const [ linkClicked , setLinkClicked ] = useState ( false ) ;
291286
292- if ( ! linkWithRowData . startsWith ( "/" ) ) {
293- window . open ( linkWithRowData , api . target || "_blank" ) ;
294- createDialog . handleClose ( ) ;
295- }
296- }
297- } , [ api . link , createDialog . open ] ) ;
287+ useEffect ( ( ) => {
288+ if ( api . link && ! linkClicked && row && Object . keys ( row ) . length > 0 ) {
289+ const timeoutId = setTimeout ( ( ) => {
290+ const linkWithRowData = api . link . replace ( / \[ ( [ ^ \] ] + ) \] / g , ( _ , key ) => {
291+ return getNestedValue ( row , key ) || `[ ${ key } ]` ;
292+ } ) ;
298293
299- // Handling internal link navigation
300- if ( api . link && createDialog . open ) {
301- const linkWithRowData = api . link . replace ( / \[ ( [ ^ \] ] + ) \] / g, ( _ , key ) => {
302- return getNestedValue ( row , key ) || `[${ key } ]` ;
303- } ) ;
294+ if ( linkWithRowData . startsWith ( "/" ) ) {
295+ // Internal link navigation
296+ setLinkClicked ( true ) ;
297+ router . push ( linkWithRowData , undefined , { shallow : true } ) ;
298+ } else {
299+ // External link navigation
300+ setLinkClicked ( true ) ;
301+ window . open ( linkWithRowData , api . target || "_blank" ) ;
302+ }
303+ } , 0 ) ; // Delay execution to the next event loop cycle
304304
305- if ( linkWithRowData . startsWith ( "/" ) ) {
306- router . push ( linkWithRowData , undefined , { shallow : true } ) ;
307- createDialog . handleClose ( ) ;
305+ return ( ) => clearTimeout ( timeoutId ) ;
308306 }
309- }
307+ } , [ api . link , linkClicked , row , router ] ) ;
310308
311309 useEffect ( ( ) => {
312- if ( api . noConfirm ) {
310+ if ( api . noConfirm && ! api . link ) {
313311 formHook . handleSubmit ( onSubmit ) ( ) ; // Submits the form on mount
314312 createDialog . handleClose ( ) ; // Closes the dialog after submitting
315313 }
316- } , [ api . noConfirm ] ) ; // Run effect only when api. noConfirm changes
314+ } , [ api . noConfirm , api . link ] ) ; // Run effect when noConfirm or link changes
317315
318316 const handleClose = ( ) => {
319317 createDialog . handleClose ( ) ;
@@ -336,44 +334,52 @@ export const CippApiDialog = (props) => {
336334 }
337335
338336 return (
339- < Dialog fullWidth maxWidth = "sm" onClose = { handleClose } open = { createDialog . open } { ...other } >
340- < form onSubmit = { formHook . handleSubmit ( onSubmit ) } >
341- < DialogTitle > { title } </ DialogTitle >
342- < DialogContent >
343- < Stack spacing = { 2 } > { confirmText } </ Stack >
344- </ DialogContent >
345- < DialogContent >
346- < Grid container spacing = { 2 } >
347- { fields &&
348- fields . map ( ( fieldProps , index ) => {
349- if ( fieldProps ?. api ?. processFieldData ) {
350- fieldProps . api . data = processActionData ( fieldProps . api . data , row ) ;
351- }
352- return (
353- < Grid item xs = { 12 } key = { index } >
354- < CippFormComponent
355- formControl = { formHook }
356- addedFieldData = { addedFieldData }
357- setAddedFieldData = { setAddedFieldData }
358- { ...fieldProps }
359- />
360- </ Grid >
361- ) ;
362- } ) }
363- </ Grid >
364- </ DialogContent >
365- < DialogContent >
366- < CippApiResults apiObject = { { ...selectedType , data : partialResults } } />
367- </ DialogContent >
368- < DialogActions >
369- < Button color = "inherit" onClick = { ( ) => handleClose ( ) } >
370- Close
371- </ Button >
372- < Button variant = "contained" type = "submit" disabled = { isFormSubmitted && ! allowResubmit } >
373- { isFormSubmitted && allowResubmit ? "Reconfirm" : "Confirm" }
374- </ Button >
375- </ DialogActions >
376- </ form >
377- </ Dialog >
337+ < >
338+ { ! api ?. link && (
339+ < Dialog fullWidth maxWidth = "sm" onClose = { handleClose } open = { createDialog . open } { ...other } >
340+ < form onSubmit = { formHook . handleSubmit ( onSubmit ) } >
341+ < DialogTitle > { title } </ DialogTitle >
342+ < DialogContent >
343+ < Stack spacing = { 2 } > { confirmText } </ Stack >
344+ </ DialogContent >
345+ < DialogContent >
346+ < Grid container spacing = { 2 } >
347+ { fields &&
348+ fields . map ( ( fieldProps , index ) => {
349+ if ( fieldProps ?. api ?. processFieldData ) {
350+ fieldProps . api . data = processActionData ( fieldProps . api . data , row ) ;
351+ }
352+ return (
353+ < Grid item xs = { 12 } key = { index } >
354+ < CippFormComponent
355+ formControl = { formHook }
356+ addedFieldData = { addedFieldData }
357+ setAddedFieldData = { setAddedFieldData }
358+ { ...fieldProps }
359+ />
360+ </ Grid >
361+ ) ;
362+ } ) }
363+ </ Grid >
364+ </ DialogContent >
365+ < DialogContent >
366+ < CippApiResults apiObject = { { ...selectedType , data : partialResults } } />
367+ </ DialogContent >
368+ < DialogActions >
369+ < Button color = "inherit" onClick = { ( ) => handleClose ( ) } >
370+ Close
371+ </ Button >
372+ < Button
373+ variant = "contained"
374+ type = "submit"
375+ disabled = { isFormSubmitted && ! allowResubmit }
376+ >
377+ { isFormSubmitted && allowResubmit ? "Reconfirm" : "Confirm" }
378+ </ Button >
379+ </ DialogActions >
380+ </ form >
381+ </ Dialog >
382+ ) }
383+ </ >
378384 ) ;
379385} ;
0 commit comments