Skip to content

Commit 23b7ae7

Browse files
committed
refactor:resolved talisman errors
1 parent ca72daa commit 23b7ae7

File tree

4 files changed

+45
-54
lines changed

4 files changed

+45
-54
lines changed

ui/src/components/DestinationStack/Actions/LoadLanguageMapper.tsx

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -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}

ui/src/components/DestinationStack/Actions/LoadStacks.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { RootState } from '../../../store';
1515
import { updateNewMigrationData } from '../../../store/slice/migrationDataSlice';
1616

1717
// Interface
18-
import { IDropDown, INewMigration } from '../../../context/app/app.interface';
18+
import { DEFAULT_DROPDOWN, IDropDown, INewMigration } from '../../../context/app/app.interface';
1919
import { StackResponse } from '../../../services/api/service.interface';
2020
import { Stack } from '../../../components/Common/AddStack/addStack.interface';
2121

@@ -176,8 +176,7 @@ const LoadStacks = (props: LoadFileFormatProps) => {
176176
...newMigrationData,
177177
destination_stack: {
178178
...newMigrationData?.destination_stack,
179-
selectedStack: { ...data },
180-
localeMapping: data?.value ? newMigrationData?.destination_stack?.localeMapping : {}
179+
selectedStack: { ...data }
181180
}
182181
};
183182

@@ -375,7 +374,7 @@ const LoadStacks = (props: LoadFileFormatProps) => {
375374
</div>
376375
<LanguageMapper
377376
uid={selectedStack?.uid ?? ''}
378-
stack={selectedStack} />
377+
stack={selectedStack ?? DEFAULT_DROPDOWN} />
379378
</div>
380379
)}
381380
</div>

ui/src/context/app/app.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export interface IDestinationStack {
176176
stackArray: IDropDown[];
177177
migratedStacks: string[];
178178
sourceLocale: string[];
179-
localeMapping: {};
179+
localeMapping: Record<string, string>;
180180
csLocale: string[];
181181
}
182182
export interface IContentMapper {

ui/src/pages/Migration/index.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,8 @@ const Migration = () => {
534534
const hasNonEmptyMapping =
535535
newMigrationData?.destination_stack?.localeMapping &&
536536
Object.entries(newMigrationData?.destination_stack?.localeMapping || {})?.every(
537-
([key, value]: [string, any]) =>
538-
key !== '' &&
539-
key !== null &&
540-
key !== undefined &&
537+
([label, value]: [string, string]) =>
538+
Boolean(label?.trim()) &&
541539
value !== '' &&
542540
value !== null &&
543541
value !== undefined

0 commit comments

Comments
 (0)