@@ -111,31 +111,36 @@ const Mapper = ({
111111
112112 // const validLabels = cmsLocaleOptions?.map((item)=> item?.label);
113113
114- const existingMasterKey = Object ?. keys ( selectedMappings || { } ) ?. find ( ( key ) =>
114+ const existingMasterID = Object ?. keys ( selectedMappings || { } ) ?. find ( ( key ) =>
115115 key ?. includes ( '-master_locale' )
116116 ) ;
117117
118118 const recentMsterLocale = cmsLocaleOptions ?. find ( ( item ) => item ?. value === 'master_locale' ) ?. label ;
119+ const presentLocale = `${ recentMsterLocale } -master_locale` ;
119120
120121 Object . keys ( updatedExistingField || { } ) ?. forEach ( ( key ) => {
121- if ( ( existingMasterKey !== ` ${ recentMsterLocale } -master_locale` ) || isStackChanged ) {
122+ if ( ( existingMasterID !== presentLocale ) || isStackChanged ) {
122123 delete updatedExistingField [ key ] ;
123124 }
124125 } ) ;
125126
126127 Object . keys ( updatedExistingLocale || { } ) ?. forEach ( ( key ) => {
127- if ( ( existingMasterKey !== ` ${ recentMsterLocale } -master_locale` ) || isStackChanged ) {
128+ if ( ( existingMasterID !== presentLocale ) || isStackChanged ) {
128129 delete updatedExistingLocale [ key ] ;
129130 }
130131 } ) ;
131- if ( ( existingMasterKey !== ` ${ recentMsterLocale } -master_locale` ) || isStackChanged ) {
132+ if ( ( existingMasterID !== presentLocale ) || isStackChanged ) {
132133 setselectedCsOption ( [ ] ) ;
133134 setselectedSourceOption ( [ ] ) ;
134135 }
135136
136137 setexistingLocale ( updatedExistingLocale ) ;
137138
138139 cmsLocaleOptions ?. map ( ( locale , index ) => {
140+ const existingLabel = existingMasterID ;
141+ const expectedLabel = `${ locale ?. label } -master_locale` ;
142+
143+ const isLabelMismatch = existingLabel && existingLabel . localeCompare ( expectedLabel ) !== 0 ;
139144 if ( locale ?. value === 'master_locale' ) {
140145 if ( ! updatedExistingField ?. [ index ] ) {
141146 updatedExistingField [ index ] = {
@@ -145,7 +150,7 @@ const Mapper = ({
145150 }
146151
147152
148- if ( ( existingMasterKey !== ` ${ locale ?. label } -master_locale` ) || isStackChanged ) {
153+ if ( isLabelMismatch || isStackChanged ) {
149154 setselectedCsOption ( [ ] ) ;
150155 setselectedSourceOption ( [ ] ) ;
151156 setexistingLocale ( { } ) ;
@@ -274,34 +279,25 @@ const Mapper = ({
274279 const csLocale = existingField ?. [ index ] ?. label ?? '' ;
275280 const sourceLocale = existingLocale ?. [ index ] ?. label ?? '' ;
276281
277- setExistingField (
278- ( prevOptions : Record < number , { label : string ; value : string } > | undefined ) => {
279- //if (!prevOptions) return {}; // Ensure it's an object
280-
281- const updatedOptions = { ...prevOptions } ; // Create a shallow copy
282- //csLocale = updatedOptions[index]?.label;
283-
284- setselectedCsOption ( ( prevSelected ) => {
285- const newSelectedOptions : string [ ] = prevSelected ?. filter (
286- ( item ) => item !== csLocale // Remove the item equal to csLocale
287- ) ;
288- return newSelectedOptions ;
289- } ) ;
290-
291- Object . entries ( updatedOptions ) . forEach ( ( [ key , value ] ) => {
292- const numKey = Number ( key ) ;
293- if ( numKey < index ) {
294- updatedOptions [ numKey ] = value ;
295- } else if ( numKey > index ) {
296- updatedOptions [ numKey - 1 ] = value ; // Shift down by 1
297- delete updatedOptions [ numKey ]
298- }
299- } ) ;
300- // Remove the key
301-
302- return updatedOptions ;
282+ setExistingField ( ( prevOptions ) => {
283+ const updatedOptions : Record < number , { label : string ; value : string } > = { } ;
284+ const prev = prevOptions ?? { } ;
285+
286+ setselectedCsOption ( ( prevSelected ) =>
287+ prevSelected ?. filter ( ( item ) => item !== csLocale )
288+ ) ;
289+
290+ for ( let i = 0 ; i < Object ?. keys ( prev ) ?. length ; i ++ ) {
291+ if ( i < index ) {
292+ updatedOptions [ i ] = prev ?. [ i ] ;
293+ } else if ( i > index ) {
294+ updatedOptions [ i - 1 ] = prev ?. [ i ] ;
295+ }
303296 }
304- ) ;
297+
298+ return updatedOptions ;
299+ } ) ;
300+
305301
306302 // Remove item at index from existingLocale
307303 setexistingLocale ( ( prevLocales : ExistingFieldType ) => {
@@ -322,11 +318,14 @@ const Mapper = ({
322318 setSelectedMappings ( ( prev ) => {
323319 const updatedMappings = { ...prev } ;
324320 if ( ! csLocale ) {
325- Object . entries ( updatedMappings ) . forEach ( ( [ key , value ] ) => {
326- if ( value === sourceLocale ) {
327- delete updatedMappings [ key ] ;
321+ for ( const key in updatedMappings ) {
322+ if ( Object ?. prototype ?. hasOwnProperty ?. call ( updatedMappings , key ) ) {
323+ const value = updatedMappings ?. [ key ] ;
324+ if ( value === sourceLocale ) {
325+ delete updatedMappings ?. [ key ] ;
326+ }
328327 }
329- } ) ;
328+ }
330329 } else {
331330 delete updatedMappings [ csLocale ] ;
332331 }
@@ -336,9 +335,6 @@ const Mapper = ({
336335 handleLangugeDelete ( index , locale ) ;
337336 } ;
338337
339- useEffect ( ( ) => {
340- console . info ( "existing locale --> " , existingLocale )
341- } , [ existingLocale ] )
342338 return (
343339 < >
344340 { cmsLocaleOptions ?. length > 0 ? (
@@ -477,7 +473,7 @@ const Mapper = ({
477473 ) ;
478474} ;
479475
480- const LanguageMapper = ( { stack, uid} :{ stack : IDropDown | null , uid : string } ) => {
476+ const LanguageMapper = ( { stack, uid} :{ stack : IDropDown , uid : string } ) => {
481477
482478 const newMigrationData = useSelector ( ( state : RootState ) => state ?. migration ?. newMigrationData ) ;
483479 const [ options , setoptions ] = useState < { label : string ; value : string } [ ] > ( [ ] ) ;
@@ -487,19 +483,18 @@ const LanguageMapper = ({stack, uid} :{ stack : IDropDown | null, uid : string})
487483 const [ currentStack , setCurrentStack ] = useState < IDropDown > ( ) ;
488484 const [ previousStack , setPreviousStack ] = useState < IDropDown > ( ) ;
489485 const [ isStackChanged , setisStackChanged ] = useState < boolean > ( false ) ;
486+ const [ stackValue , setStackValue ] = useState < string > ( stack ?. value )
490487
491488 const prevStackRef :any = useRef ( null ) ;
492489
493490 useEffect ( ( ) => {
494- console . info ( "-----> " , prevStackRef ?. current , stack ?. uid , prevStackRef ?. current )
495491 if ( prevStackRef ?. current && stack && stack ?. uid !== prevStackRef ?. current ?. uid ) {
496492 setisStackChanged ( true ) ;
497493 setCurrentStack ( stack ) ;
498494 setPreviousStack ( prevStackRef ?. current ) ;
499495 }
500496
501497 prevStackRef . current = stack ;
502- console . info ( "after setting " , prevStackRef . current )
503498 } , [ stack ] ) ;
504499
505500 useEffect ( ( ) => {
@@ -605,7 +600,6 @@ const LanguageMapper = ({stack, uid} :{ stack : IDropDown | null, uid : string})
605600 ) ;
606601 } ) ;
607602 } ;
608-
609603 return (
610604 < div >
611605 { isLoading ? (
@@ -619,8 +613,8 @@ const LanguageMapper = ({stack, uid} :{ stack : IDropDown | null, uid : string})
619613 }
620614 rowComponent = {
621615 < Mapper
622- key = { stack ?. value ?? '' }
623- uid = { stack ?. value ?? '' }
616+ key = { uid }
617+ uid = { stack ?. value }
624618 options = { options }
625619 cmsLocaleOptions = { cmsLocaleOptions }
626620 handleLangugeDelete = { handleDeleteLocale }
0 commit comments