@@ -13,7 +13,7 @@ import TableHeader from './tableHeader';
1313import { useDispatch , useSelector } from 'react-redux' ;
1414import { RootState } from '../../../store' ;
1515import { updateNewMigrationData } from '../../../store/slice/migrationDataSlice' ;
16- import { INewMigration } from '../../../context/app/app.interface' ;
16+ import { IDropDown , INewMigration } from '../../../context/app/app.interface' ;
1717
1818export type ExistingFieldType = {
1919 [ key : string ] : { label : string ; value : string } ;
@@ -33,13 +33,15 @@ const Mapper = ({
3333 handleLangugeDelete,
3434 options,
3535 sourceOptions,
36- isDisabled
36+ isDisabled,
37+ localeChanged,
3738} : {
3839 cmsLocaleOptions : Array < { label : string ; value : string } > ;
3940 handleLangugeDelete : ( index : number , locale : { label : string ; value : string } ) => void ;
4041 options : Array < { label : string ; value : string } > ;
4142 sourceOptions : Array < { label : string ; value : string } > ;
4243 isDisabled : boolean ;
44+ localeChanged : boolean
4345} ) => {
4446 const [ selectedMappings , setSelectedMappings ] = useState < { [ key : string ] : string } > ( { } ) ;
4547 const [ existingField , setExistingField ] = useState < ExistingFieldType > ( { } ) ;
@@ -92,46 +94,59 @@ const Mapper = ({
9294 } , [ selectedCsOptions , selectedSourceOption , options ] ) ;
9395
9496 useEffect ( ( ) => {
95- setExistingField ( ( prevExisting : ExistingFieldType ) => {
96- const updatedExisting = { ...prevExisting } ;
97- const validLabels = cmsLocaleOptions ?. map ( locale => locale ?. label ) || [ ] ;
98- Object ?. entries ( updatedExisting ) ?. forEach ( ( [ index , entry ] ) => {
99- const [ labelPart ] = entry . label . split ( '-' ) ;
100- if ( ! validLabels ?. includes ( labelPart ) ) {
101- delete updatedExisting ?. [ index ] ;
102- }
103- } ) ;
97+ const updatedExistingField = { ...existingField } ;
98+ const updatedExistingLocale = { ...existingLocale } ;
99+ let updatedSelectedMappings = { ...selectedMappings } ;
100+
101+ const validLabels = cmsLocaleOptions ?. map ( ( item ) => item ?. label ) ;
102+
103+ const existingMasterKey = Object ?. keys ( selectedMappings || { } ) ?. find ( ( key ) =>
104+ key ?. includes ( '-master_locale' )
105+ ) ;
106+
107+ const recentMsterLocale = cmsLocaleOptions ?. find ( ( item ) => { item ?. value === 'master_locale' } )
108+
109+ Object . keys ( updatedExistingField || { } ) ?. forEach ( ( key ) => {
110+ if ( ! validLabels ?. includes ( updatedExistingField ?. [ key ] ?. label ) || existingMasterKey !== `${ recentMsterLocale } -master_locale` ) {
111+ delete updatedExistingField ?. [ key ] ;
112+ }
113+ } ) ;
114+
115+ Object . keys ( updatedExistingLocale || { } ) ?. forEach ( ( key ) => {
104116
105- setSelectedMappings ( ( prev ) => {
106- const updated = { ...prev } ;
107-
108- Object ?. keys ( updated ) ?. forEach ( ( key ) => {
109- const [ labelPart ] = key ?. split ( '-' ) ?? [ ] ;
110- if ( ! validLabels ?. includes ( labelPart ) ) {
111- delete updated ?. [ key ] ;
112- }
113- } ) ;
114-
115- return updated ;
116- } ) ;
117+ if ( ( ! validLabels ?. includes ( updatedExistingLocale ?. [ key ] ?. label ) && Object ?. entries ( updatedExistingField ) ?. length === 0 ) || existingMasterKey !== `${ recentMsterLocale } -master_locale` ) {
118+ delete updatedExistingLocale ?. [ key ] ;
119+ }
120+ } ) ;
117121
118- cmsLocaleOptions ?. forEach ( ( locale : { label : string ; value : string } , index : number ) => {
119- if ( locale ?. value === 'master_locale' && ! updatedExisting ?. [ index ] ) {
120- setSelectedMappings ( ( prev ) => ( {
121-
122- ...prev ,
123- [ `${ locale ?. label } -master_locale` ] : ''
124- } ) ) ;
125- updatedExisting [ index ] = {
126- label : locale ?. label ,
127- value : `${ locale ?. label } -master_locale`
122+ cmsLocaleOptions ?. map ( ( locale , index ) => {
123+ if ( locale ?. value === 'master_locale' ) {
124+ if ( ! updatedExistingField ?. [ index ] ) {
125+ updatedExistingField [ index ] = {
126+ label : `${ locale ?. label } ` ,
127+ value : `${ locale ?. label } -master_locale` ,
128128 } ;
129129 }
130- } ) ;
131130
132- return updatedExisting ;
133- } ) ;
134- } , [ cmsLocaleOptions ] ) ;
131+
132+ if ( existingMasterKey !== `${ locale ?. label } -master_locale` ) {
133+ setselectedCsOption ( [ ] ) ;
134+ setselectedSourceOption ( [ ] ) ;
135+ updatedSelectedMappings = {
136+ [ `${ locale ?. label } -master_locale` ] : '' ,
137+ } ;
138+
139+ }
140+ }
141+ } )
142+
143+ setExistingField ( updatedExistingField ) ;
144+ setexistingLocale ( updatedExistingLocale ) ;
145+ setSelectedMappings ( updatedSelectedMappings ) ;
146+
147+
148+ } , [ cmsLocaleOptions ] ) ;
149+
135150
136151 // function for change select value
137152 const handleSelectedCsLocale = (
@@ -284,20 +299,21 @@ const Mapper = ({
284299
285300 handleLangugeDelete ( index , locale ) ;
286301 } ;
287-
288302 return (
289303 < >
290- { cmsLocaleOptions ?. length > 0 ? (
291- cmsLocaleOptions ?. map ( ( locale : any , index : number ) => (
292- < div key = { index } className = "lang-container" >
304+ { cmsLocaleOptions ?. length > 0 ? (
305+ cmsLocaleOptions ?. map ( ( locale : { label :string , value : string } , index : number ) => (
306+
307+ < div key = { locale . label } className = "lang-container" >
308+
293309 { locale ?. value === 'master_locale' ? (
294310 < Tooltip
295311 content = "This is the master locale of above selected stacks and cannot be changed. Please select a corresponding language to map."
296312 position = "top"
297313 >
298314 < div >
299315 < Select
300- value = { locale ?. value === 'master_locale' ? locale : existingField [ locale ] }
316+ value = { locale ?. value === 'master_locale' ? locale : existingField [ locale ?. label ] }
301317 onChange = { ( key : { label : string ; value : string } ) =>
302318 handleSelectedCsLocale ( key , index , 'csLocale' )
303319 }
@@ -320,7 +336,7 @@ const Mapper = ({
320336 </ Tooltip >
321337 ) : (
322338 < Select
323- value = { locale ?. value ? locale : existingField [ locale ] }
339+ value = { locale ?. value ? locale : existingField [ locale ?. label ] }
324340 onChange = { ( key : { label : string ; value : string } ) => {
325341 handleSelectedCsLocale ( key , index , 'csLocale' ) ;
326342 } }
@@ -364,7 +380,7 @@ const Mapper = ({
364380 value = {
365381 locale ?. value && locale ?. value !== 'master_locale'
366382 ? { label : locale ?. value , value : locale ?. value }
367- : existingLocale [ locale ]
383+ : existingLocale [ locale ?. label ]
368384 }
369385 onChange = { ( data : { label : string ; value : string } ) =>
370386 handleSelectedSourceLocale ( data , index , 'sourceLocale' , locale )
@@ -421,12 +437,14 @@ const Mapper = ({
421437 ) ;
422438} ;
423439
424- const LanguageMapper = ( ) => {
440+ const LanguageMapper = ( { stack} :{ stack : IDropDown } ) => {
441+
425442 const newMigrationData = useSelector ( ( state : RootState ) => state ?. migration ?. newMigrationData ) ;
426443 const [ options , setoptions ] = useState < { label : string ; value : string } [ ] > ( [ ] ) ;
427444 const [ cmsLocaleOptions , setcmsLocaleOptions ] = useState < { label : string ; value : string } [ ] > ( [ ] ) ;
428445 const [ sourceLocales , setsourceLocales ] = useState < { label : string ; value : string } [ ] > ( [ ] ) ;
429446 const [ isLoading , setisLoading ] = useState < boolean > ( false ) ;
447+ const [ localeChanged , setlocaleChanged ] = useState < boolean > ( false ) ;
430448
431449 useEffect ( ( ) => {
432450 const fetchData = async ( ) => {
@@ -446,18 +464,18 @@ const LanguageMapper = () => {
446464 setoptions ( allLocales ) ;
447465 const keys = Object ?. keys ( newMigrationData ?. destination_stack ?. localeMapping || { } ) ?. find ( key => key === `${ newMigrationData ?. destination_stack ?. selectedStack ?. master_locale } -master_locale` ) ;
448466
449- ( Object ?. entries ( newMigrationData ?. destination_stack ?. localeMapping ) ?. length === 0 || keys !== ` ${ newMigrationData ?. destination_stack ?. selectedStack ?. master_locale } -master_locale` ) &&
467+ ( Object ?. entries ( newMigrationData ?. destination_stack ?. localeMapping ) ?. length === 0 || ! keys ) &&
450468 newMigrationData ?. project_current_step <= 2 &&
451469 setcmsLocaleOptions ( ( prevList : { label : string ; value : string } [ ] ) => {
452- const newLabel = newMigrationData ?. destination_stack ?. selectedStack ?. master_locale ;
470+ const newLabel = stack ?. master_locale ;
453471
454472 const isPresent = prevList ?. filter (
455473 ( item : { label : string ; value : string } ) => ( item ?. value === 'master_locale' )
456474 ) ;
457-
458475 if ( isPresent ?. [ 0 ] ?. label !== newLabel ) {
476+ setlocaleChanged ( true ) ;
459477 return [
460- ...prevList ?. filter ( item => item ?. value !== 'master_locale' ) ?? [ ] ,
478+ ...prevList ?. filter ( item => ( item ?. value !== 'master_locale' && item ?. value !== '' ) ) ?? [ ] ,
461479 {
462480 label : newLabel ,
463481 value : 'master_locale' ,
@@ -507,16 +525,17 @@ const LanguageMapper = () => {
507525 } ;
508526
509527 fetchData ( ) ;
510- } , [ newMigrationData ?. destination_stack ] ) ;
528+ } , [ newMigrationData ?. destination_stack ?. selectedStack ] ) ;
511529
512530 // const fetchLocales = async () => {
513531 // return await getStackLocales(newMigrationData?.destination_stack?.selectedOrg?.value);
514532 // };
515533 const addRowComp = ( ) => {
534+ setlocaleChanged ( false ) ;
516535 setcmsLocaleOptions ( ( prevList : { label : string ; value : string } [ ] ) => [
517536 ...prevList , // Keep existing elements
518537 {
519- label : `${ prevList . length + 1 } ` , // Generate new label
538+ label : `${ prevList . length } ` , // Generate new label
520539 value : ''
521540 }
522541 ] ) ;
@@ -543,6 +562,7 @@ const LanguageMapper = () => {
543562 }
544563 rowComponent = {
545564 < Mapper
565+ localeChanged = { localeChanged }
546566 options = { options }
547567 cmsLocaleOptions = { cmsLocaleOptions }
548568 handleLangugeDelete = { handleDeleteLocale }
0 commit comments