@@ -93,6 +93,7 @@ const CippStandardAccordion = ({
9393 handleRemoveStandard,
9494 handleAddMultipleStandard,
9595 formControl,
96+ editMode = false ,
9697} ) => {
9798 const [ configuredState , setConfiguredState ] = useState ( { } ) ;
9899 const [ filter , setFilter ] = useState ( "all" ) ;
@@ -188,46 +189,48 @@ const CippStandardAccordion = ({
188189
189190 // Initialize when watchedValues are available
190191 useEffect ( ( ) => {
191- // Only run initialization if we have watchedValues and they contain data
192- if ( ! watchedValues || Object . keys ( watchedValues ) . length === 0 ) {
193- return ;
194- }
195-
196- // Prevent re-initialization if we already have configuration state
197- const hasConfigState = Object . keys ( configuredState ) . length > 0 ;
198- if ( hasConfigState ) {
199- return ;
200- }
201-
202- console . log ( "Initializing configuration state from template values" ) ;
203- const initial = { } ;
204- const initialConfigured = { } ;
205-
206- // For each standard, get its current values and determine if it's configured
207- Object . keys ( selectedStandards ) . forEach ( ( standardName ) => {
208- const currentValues = _ . get ( watchedValues , standardName ) ;
209- if ( ! currentValues ) return ;
210-
211- initial [ standardName ] = _ . cloneDeep ( currentValues ) ;
192+ if ( editMode ) {
193+ // Only run initialization if we have watchedValues and they contain data
194+ if ( ! watchedValues || Object . keys ( watchedValues ) . length === 0 ) {
195+ return ;
196+ }
212197
213- const baseStandardName = standardName . split ( "[" ) [ 0 ] ;
214- const standard = providedStandards . find ( ( s ) => s . name === baseStandardName ) ;
215- if ( standard ) {
216- initialConfigured [ standardName ] = isStandardConfigured (
217- standardName ,
218- standard ,
219- currentValues
220- ) ;
198+ // Prevent re-initialization if we already have configuration state
199+ const hasConfigState = Object . keys ( configuredState ) . length > 0 ;
200+ if ( hasConfigState ) {
201+ return ;
221202 }
222- } ) ;
223203
224- // Store both the initial values and set them as current saved values
225- setOriginalValues ( initial ) ;
226- setSavedValues ( initial ) ;
227- setConfiguredState ( initialConfigured ) ;
228- // Only depend on watchedValues and selectedStandards to avoid infinite loops
229- // eslint-disable-next-line react-hooks/exhaustive-deps
230- } , [ watchedValues , selectedStandards ] ) ;
204+ console . log ( "Initializing configuration state from template values" ) ;
205+ const initial = { } ;
206+ const initialConfigured = { } ;
207+
208+ // For each standard, get its current values and determine if it's configured
209+ Object . keys ( selectedStandards ) . forEach ( ( standardName ) => {
210+ const currentValues = _ . get ( watchedValues , standardName ) ;
211+ if ( ! currentValues ) return ;
212+
213+ initial [ standardName ] = _ . cloneDeep ( currentValues ) ;
214+
215+ const baseStandardName = standardName . split ( "[" ) [ 0 ] ;
216+ const standard = providedStandards . find ( ( s ) => s . name === baseStandardName ) ;
217+ if ( standard ) {
218+ initialConfigured [ standardName ] = isStandardConfigured (
219+ standardName ,
220+ standard ,
221+ currentValues
222+ ) ;
223+ }
224+ } ) ;
225+
226+ // Store both the initial values and set them as current saved values
227+ setOriginalValues ( initial ) ;
228+ setSavedValues ( initial ) ;
229+ setConfiguredState ( initialConfigured ) ;
230+ // Only depend on watchedValues and selectedStandards to avoid infinite loops
231+ // eslint-disable-next-line react-hooks/exhaustive-deps
232+ }
233+ } , [ watchedValues , selectedStandards , editMode ] ) ;
231234
232235 // Save changes for a standard
233236 const handleSave = ( standardName , standard , current ) => {
@@ -519,6 +522,9 @@ const CippStandardAccordion = ({
519522 // Get current values and check if they differ from saved values
520523 const current = _ . get ( watchedValues , standardName ) ;
521524 const saved = _ . get ( savedValues , standardName ) || { } ;
525+ console . log ( `Current values for ${ standardName } :` , current ) ;
526+ console . log ( `Saved values for ${ standardName } :` , saved ) ;
527+
522528 const hasUnsaved = ! _ . isEqual ( current , saved ) ;
523529
524530 // Check if all required fields are filled
@@ -609,7 +615,7 @@ const CippStandardAccordion = ({
609615 const canSave = hasAction && requiredFieldsFilled && hasUnsaved ;
610616
611617 console . log (
612- `Standard: ${ standardName } , Action Required: ${ actionRequired } , Has Action: ${ hasAction } , Required Fields Filled: ${ requiredFieldsFilled } , Can Save: ${ canSave } `
618+ `Standard: ${ standardName } , Action Required: ${ actionRequired } , Has Action: ${ hasAction } , Required Fields Filled: ${ requiredFieldsFilled } , Unsaved Changes: ${ hasUnsaved } , Can Save: ${ canSave } `
613619 ) ;
614620
615621 return (
@@ -760,7 +766,7 @@ const CippStandardAccordion = ({
760766 </ Grid >
761767 </ Box >
762768 < Divider sx = { { mt : 2 } } />
763- < Box sx = { { px : 3 , py : 2 } } >
769+ < Box sx = { { px : 3 , py : 2 } } >
764770 < Stack direction = "row" justifyContent = "flex-end" spacing = { 1 } >
765771 < Button
766772 variant = "outlined"
0 commit comments