Skip to content

Commit 8f2ae08

Browse files
committed
refactor:added optional chaining
1 parent cb5c0a0 commit 8f2ae08

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

api/src/utils/content-type-creator.utils.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ const arrangGroups = ({ schema, newStack }: any) => {
7575
if (item?.contentstackFieldType === 'group') {
7676
const groupSchema: any = { ...item, schema: [] }
7777
if (item?.contentstackFieldUid?.includes('.')) {
78-
const parts = item.contentstackFieldUid.split('.');
79-
groupSchema.contentstackFieldUid = parts[parts.length - 1];
78+
const parts = item?.contentstackFieldUid?.split('.');
79+
groupSchema.contentstackFieldUid = parts?.[parts?.length - 1];
8080
}
8181
schema?.forEach((et: any) => {
8282
if (et?.contentstackFieldUid?.includes(`${item?.contentstackFieldUid}.`) ||
@@ -668,12 +668,12 @@ const mergeArrays = (a: any[], b: any[]): any[] => {
668668
const result = [...a];
669669

670670
for (const field of b) {
671-
const exists = result.some(f =>
671+
const exists = result?.some(f =>
672672
f?.uid === field?.uid &&
673673
f?.data_type === field?.data_type
674674
);
675675
if (!exists) {
676-
result.push(field);
676+
result?.push(field);
677677
}
678678
}
679679

@@ -684,27 +684,27 @@ const mergeFields = async (schema1: any[], schema2: any[]): Promise<any[]> => {
684684
const result: any[] = [];
685685

686686
for (const field2 of schema2) {
687-
if (field2.data_type === 'group') {
687+
if (field2?.data_type === 'group') {
688688
const machingGroup = findGroupByUid(schema1, field2?.uid);
689689
if(machingGroup){
690690
const schema = await mergeArrays(machingGroup?.schema ?? [],field2?.schema ?? [] );
691-
result.push({
691+
result?.push({
692692
...field2,
693693
schema: schema
694694
});
695695
}
696696
else{
697-
result.push({
697+
result?.push({
698698
...field2,
699699
schema: await mergeFields(schema1, field2?.schema)
700700
})
701701
}
702702
}
703703
else{
704-
const exists = schema1.find(
704+
const exists = schema1?.find(
705705
(fld) =>
706-
fld.uid === field2.uid &&
707-
fld.data_type === field2.data_type
706+
fld?.uid === field2?.uid &&
707+
fld?.data_type === field2?.data_type
708708
);
709709
result?.push({
710710
...field2
@@ -715,14 +715,14 @@ const mergeFields = async (schema1: any[], schema2: any[]): Promise<any[]> => {
715715
}
716716

717717
for (const field1 of schema1) {
718-
const isMatched = schema2.some(
718+
const isMatched = schema2?.some(
719719
(field2) =>
720-
field1.uid === field2.uid &&
721-
field1.data_type === field2.data_type
720+
field1?.uid === field2?.uid &&
721+
field1?.data_type === field2?.data_type
722722
);
723723

724724
if (!isMatched) {
725-
result.push(field1);
725+
result?.push(field1);
726726
}
727727
}
728728

@@ -736,9 +736,9 @@ const findGroupByUid = (
736736
excludeRef: any = null
737737
): any | undefined => {
738738
for (const field of schema) {
739-
if (field.data_type === 'group') {
740-
if (field.uid === uid && field !== excludeRef) return field;
741-
const nested = findGroupByUid(field.schema ?? [], uid, excludeRef);
739+
if (field?.data_type === 'group') {
740+
if (field?.uid === uid && field !== excludeRef) return field;
741+
const nested = findGroupByUid(field?.schema ?? [], uid, excludeRef);
742742
if (nested) return nested;
743743
}
744744
}

ui/src/components/ContentMapper/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ const ContentMapper = forwardRef(({handleStepChange}: contentMapperProps, ref: R
11331133

11341134
const handleFieldChange = (selectedValue: FieldTypes, rowIndex: string, contentstackFieldUid: string, backupFieldUid: string) => {
11351135
setIsDropDownChanged(true);
1136-
const previousSelectedValue = existingField[backupFieldUid]?.label;
1136+
const previousSelectedValue = existingField?.[backupFieldUid]?.label;
11371137
const groupArray = nestedList?.filter(item =>
11381138
item?.child?.some(e => e?.id)
11391139
)
@@ -1142,16 +1142,16 @@ const ContentMapper = forwardRef(({handleStepChange}: contentMapperProps, ref: R
11421142
for(const item of groupArray?.[0]?.child ?? []){
11431143
deletedExstingField[item?.backupFieldUid] = {
11441144
label:item?.uid,
1145-
value:existingField[item?.backupFieldUid]
1145+
value:existingField?.[item?.backupFieldUid]
11461146

11471147
}
11481148
setIsFieldDeleted(true);
1149-
const index = selectedOptions?.indexOf(existingField[item?.backupFieldUid]?.value?.label);
1149+
const index = selectedOptions?.indexOf(existingField?.[item?.backupFieldUid]?.value?.label);
11501150

11511151
if(index > -1){
11521152
selectedOptions?.splice(index,1 );
11531153
}
1154-
delete existingField[item?.backupFieldUid]
1154+
delete existingField?.[item?.backupFieldUid]
11551155

11561156
}
11571157
}

0 commit comments

Comments
 (0)