Skip to content

Commit c0372a1

Browse files
committed
minor code changes
1 parent 83fcd3a commit c0372a1

File tree

1 file changed

+41
-60
lines changed

1 file changed

+41
-60
lines changed

packages/angular-sdk-components/src/lib/_components/field/dropdown/dropdown.component.ts

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,47 @@ import { MatOptionModule } from '@angular/material/core';
55
import { MatSelectModule } from '@angular/material/select';
66
import { MatFormFieldModule } from '@angular/material/form-field';
77
import { interval } from 'rxjs';
8+
import isEqual from 'fast-deep-equal';
89
import { AngularPConnectData, AngularPConnectService } from '../../../_bridge/angular-pconnect';
910
import { DatapageService } from '../../../_services/datapage.service';
1011
import { Utils } from '../../../_helpers/utils';
1112
import { ComponentMapperComponent } from '../../../_bridge/component-mapper/component-mapper.component';
1213
import { handleEvent } from '../../../_helpers/event-util';
1314
import { PConnFieldProps } from '../../../_types/PConnProps.interface';
1415

16+
function flattenParameters(params = {}) {
17+
const flatParams = {};
18+
Object.keys(params).forEach(key => {
19+
const { name, value: theVal } = params[key];
20+
flatParams[name] = theVal;
21+
});
22+
23+
return flatParams;
24+
}
25+
26+
function preProcessColumns(columnList) {
27+
return columnList.map(col => {
28+
const tempColObj = { ...col };
29+
tempColObj.value = col.value && col.value.startsWith('.') ? col.value.substring(1) : col.value;
30+
return tempColObj;
31+
});
32+
}
33+
34+
function getDisplayFieldsMetaData(columnList) {
35+
const displayColumns = columnList.filter(col => col.display === 'true');
36+
const metaDataObj: any = { key: '', primary: '', secondary: [] };
37+
const keyCol = columnList.filter(col => col.key === 'true');
38+
metaDataObj.key = keyCol.length > 0 ? keyCol[0].value : 'auto';
39+
for (let index = 0; index < displayColumns.length; index += 1) {
40+
if (displayColumns[index].primary === 'true') {
41+
metaDataObj.primary = displayColumns[index].value;
42+
} else {
43+
metaDataObj.secondary.push(displayColumns[index].value);
44+
}
45+
}
46+
return metaDataObj;
47+
}
48+
1549
interface IOption {
1650
key: string;
1751
value: string;
@@ -85,9 +119,10 @@ export class DropdownComponent implements OnInit, OnDestroy {
85119
// Then, continue on with other initialization
86120

87121
// call updateSelf when initializing
88-
// this.updateSelf();
89122
this.checkAndUpdate();
123+
// this should get called afer checkAndUpdate
90124
this.getDatapageData();
125+
91126
if (this.formGroup$) {
92127
// add control to formGroup
93128
this.formGroup$.addControl(this.controlName$, this.fieldControl);
@@ -147,8 +182,8 @@ export class DropdownComponent implements OnInit, OnDestroy {
147182
this.cdRef.detectChanges();
148183
});
149184

150-
if (!this.deepEqual(datasource, this.theDatasource)) {
151-
// inbound datasource is different, so update theDatasource (to trigger useEffect)
185+
if (!isEqual(datasource, this.theDatasource)) {
186+
// inbound datasource is different, so update theDatasource
152187
this.theDatasource = datasource || null;
153188
}
154189

@@ -215,27 +250,6 @@ export class DropdownComponent implements OnInit, OnDestroy {
215250
}
216251
}
217252

218-
deepEqual(obj1: any, obj2: any): boolean {
219-
if (obj1 === obj2) return true; // Check if they are the same reference or primitive values
220-
221-
if (typeof obj1 !== 'object' || obj1 === null || typeof obj2 !== 'object' || obj2 === null) {
222-
return false; // If either is not an object, they cannot be equal
223-
}
224-
225-
// Get the list of property names from both objects
226-
const keys1 = Object.keys(obj1);
227-
const keys2 = Object.keys(obj2);
228-
229-
if (keys1.length !== keys2.length) return false; // Objects must have the same number of keys
230-
231-
for (const key of keys1) {
232-
if (!keys2.includes(key)) return false; // obj2 should have all the keys of obj1
233-
if (!this.deepEqual(obj1[key], obj2[key])) return false; // Recursively check the values
234-
}
235-
236-
return true;
237-
}
238-
239253
getDatapageData() {
240254
const configProps = this.pConn$.getConfigProps() as DropdownProps;
241255
let { listType, parameters, datasource = [], columns = [] } = configProps;
@@ -245,7 +259,7 @@ export class DropdownComponent implements OnInit, OnDestroy {
245259
listType = 'datapage';
246260
datasource = datasourceMetadata.datasource.name;
247261
const { parameters: dataSourceParameters, propertyForDisplayText, propertyForValue } = datasourceMetadata.datasource;
248-
parameters = this.flattenParameters(dataSourceParameters);
262+
parameters = flattenParameters(dataSourceParameters);
249263
const displayProp = propertyForDisplayText?.startsWith('@P') ? propertyForDisplayText.substring(3) : propertyForDisplayText;
250264
const valueProp = propertyForValue?.startsWith('@P') ? propertyForValue.substring(3) : propertyForValue;
251265
columns = [
@@ -263,7 +277,7 @@ export class DropdownComponent implements OnInit, OnDestroy {
263277
];
264278
}
265279

266-
columns = this.preProcessColumns(columns) || [];
280+
columns = preProcessColumns(columns) || [];
267281
if (!this.displayMode$ && listType !== 'associated' && typeof datasource === 'string') {
268282
this.getData(datasource, parameters, columns, context);
269283
}
@@ -272,7 +286,7 @@ export class DropdownComponent implements OnInit, OnDestroy {
272286
getData(datasource, parameters, columns, context) {
273287
this.dataPageService.getDataPageData(datasource, parameters, context).then((results: any) => {
274288
const optionsData: any[] = [];
275-
const displayColumn = this.getDisplayFieldsMetaData(columns);
289+
const displayColumn = getDisplayFieldsMetaData(columns);
276290
results?.forEach(element => {
277291
const val = element[displayColumn.primary]?.toString();
278292
const obj = {
@@ -286,39 +300,6 @@ export class DropdownComponent implements OnInit, OnDestroy {
286300
});
287301
}
288302

289-
flattenParameters(params = {}) {
290-
const flatParams = {};
291-
Object.keys(params).forEach(key => {
292-
const { name, value: theVal } = params[key];
293-
flatParams[name] = theVal;
294-
});
295-
296-
return flatParams;
297-
}
298-
299-
preProcessColumns(columnList) {
300-
return columnList.map(col => {
301-
const tempColObj = { ...col };
302-
tempColObj.value = col.value && col.value.startsWith('.') ? col.value.substring(1) : col.value;
303-
return tempColObj;
304-
});
305-
}
306-
307-
getDisplayFieldsMetaData(columnList) {
308-
const displayColumns = columnList.filter(col => col.display === 'true');
309-
const metaDataObj: any = { key: '', primary: '', secondary: [] };
310-
const keyCol = columnList.filter(col => col.key === 'true');
311-
metaDataObj.key = keyCol.length > 0 ? keyCol[0].value : 'auto';
312-
for (let index = 0; index < displayColumns.length; index += 1) {
313-
if (displayColumns[index].primary === 'true') {
314-
metaDataObj.primary = displayColumns[index].value;
315-
} else {
316-
metaDataObj.secondary.push(displayColumns[index].value);
317-
}
318-
}
319-
return metaDataObj;
320-
}
321-
322303
isSelected(buttonValue: string): boolean {
323304
return this.value$ === buttonValue;
324305
}

0 commit comments

Comments
 (0)