Skip to content

Commit 49de45f

Browse files
mashm4manasa
authored andcommitted
Fixed the dropdown issue
1 parent 8880590 commit 49de45f

File tree

1 file changed

+72
-33
lines changed

1 file changed

+72
-33
lines changed

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

Lines changed: 72 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class DropdownComponent implements OnInit, OnDestroy {
5959
testId = '';
6060
helperText: string;
6161
hideLabel: any;
62-
62+
theDatasource: any[] | null;
6363
fieldControl = new FormControl('', null);
6464
fieldMetadata: any[];
6565
localeContext = '';
@@ -87,7 +87,7 @@ export class DropdownComponent implements OnInit, OnDestroy {
8787
// call updateSelf when initializing
8888
// this.updateSelf();
8989
this.checkAndUpdate();
90-
90+
this.getDatapageData();
9191
if (this.formGroup$) {
9292
// add control to formGroup
9393
this.formGroup$.addControl(this.controlName$, this.fieldControl);
@@ -129,8 +129,6 @@ export class DropdownComponent implements OnInit, OnDestroy {
129129
updateSelf(): void {
130130
// moved this from ngOnInit() and call this from there instead...
131131
this.configProps$ = this.pConn$.resolveConfigProps(this.pConn$.getConfigProps()) as DropdownProps;
132-
let { listType, parameters, datasource = [], columns = [] } = this.configProps$;
133-
const { deferDatasource, datasourceMetadata } = this.pConn$.getConfigProps() as DropdownProps;
134132
if (this.configProps$.value != undefined) {
135133
this.value$ = this.configProps$.value;
136134
}
@@ -140,7 +138,7 @@ export class DropdownComponent implements OnInit, OnDestroy {
140138
this.label$ = this.configProps$.label;
141139
this.helperText = this.configProps$.helperText;
142140
this.hideLabel = this.configProps$.hideLabel;
143-
const context = this.pConn$.getContextName();
141+
const datasource = this.configProps$.datasource;
144142
// timeout and detectChanges to avoid ExpressionChangedAfterItHasBeenCheckedError
145143
setTimeout(() => {
146144
if (this.configProps$.required != null) {
@@ -149,6 +147,11 @@ export class DropdownComponent implements OnInit, OnDestroy {
149147
this.cdRef.detectChanges();
150148
});
151149

150+
if (!this.deepEqual(datasource, this.theDatasource)) {
151+
// inbound datasource is different, so update theDatasource (to trigger useEffect)
152+
this.theDatasource = datasource || null;
153+
}
154+
152155
if (this.configProps$.visibility != null) {
153156
this.bVisible$ = this.utils.getBooleanValue(this.configProps$.visibility);
154157
}
@@ -170,40 +173,17 @@ export class DropdownComponent implements OnInit, OnDestroy {
170173

171174
this.componentReference = this.pConn$.getStateProps().value;
172175

173-
const optionsList = [...this.utils.getOptionList(this.configProps$, this.pConn$.getDataObject())];
174-
optionsList?.unshift({ key: 'Select', value: this.pConn$.getLocalizedValue('Select...', '', '') });
175-
this.options$ = optionsList;
176176
if (this.value$ === '' && !this.bReadonly$) {
177177
this.value$ = 'Select';
178178
}
179179

180-
this.actionsApi = this.pConn$.getActionsApi();
181-
if (deferDatasource && datasourceMetadata?.datasource?.name) {
182-
listType = 'datapage';
183-
datasource = datasourceMetadata.datasource.name;
184-
const { parameters: dataSourceParameters, propertyForDisplayText, propertyForValue } = datasourceMetadata.datasource;
185-
parameters = this.flattenParameters(dataSourceParameters);
186-
const displayProp = propertyForDisplayText?.startsWith('@P') ? propertyForDisplayText.substring(3) : propertyForDisplayText;
187-
const valueProp = propertyForValue?.startsWith('@P') ? propertyForValue.substring(3) : propertyForValue;
188-
columns = [
189-
{
190-
key: 'true',
191-
setProperty: 'Associated property',
192-
value: valueProp
193-
},
194-
{
195-
display: 'true',
196-
primary: 'true',
197-
useForSearch: true,
198-
value: displayProp
199-
}
200-
];
180+
if (this.theDatasource) {
181+
const optionsList = [...this.utils.getOptionList(this.configProps$, this.pConn$.getDataObject())];
182+
optionsList?.unshift({ key: 'Select', value: this.pConn$.getLocalizedValue('Select...', '', '') });
183+
this.options$ = optionsList;
201184
}
202185

203-
columns = this.preProcessColumns(columns) || [];
204-
if (!this.displayMode$ && listType !== 'associated' && typeof datasource === 'string') {
205-
this.getData(datasource, parameters, columns, context);
206-
}
186+
this.actionsApi = this.pConn$.getActionsApi();
207187

208188
this.propName = this.pConn$.getStateProps().value;
209189
const className = this.pConn$.getCaseInfo().getClassName();
@@ -235,6 +215,65 @@ export class DropdownComponent implements OnInit, OnDestroy {
235215
}
236216
}
237217

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+
239+
getDatapageData() {
240+
const configProps = this.pConn$.getConfigProps() as DropdownProps;
241+
let { listType, parameters, datasource = [], columns = [] } = configProps;
242+
const { deferDatasource, datasourceMetadata } = configProps;
243+
const context = this.pConn$.getContextName();
244+
if (deferDatasource && datasourceMetadata?.datasource?.name) {
245+
listType = 'datapage';
246+
datasource = datasourceMetadata.datasource.name;
247+
const { parameters: dataSourceParameters, propertyForDisplayText, propertyForValue } = datasourceMetadata.datasource;
248+
parameters = this.flattenParameters(dataSourceParameters);
249+
const displayProp = propertyForDisplayText?.startsWith('@P') ? propertyForDisplayText.substring(3) : propertyForDisplayText;
250+
const valueProp = propertyForValue?.startsWith('@P') ? propertyForValue.substring(3) : propertyForValue;
251+
columns = [
252+
{
253+
key: 'true',
254+
setProperty: 'Associated property',
255+
value: valueProp
256+
},
257+
{
258+
display: 'true',
259+
primary: 'true',
260+
useForSearch: true,
261+
value: displayProp
262+
}
263+
];
264+
}
265+
266+
columns = this.preProcessColumns(columns) || [];
267+
if (!this.displayMode$ && listType !== 'associated' && typeof datasource === 'string') {
268+
this.getData(datasource, parameters, columns, context);
269+
}
270+
// else {
271+
// const optionsList = [...this.utils.getOptionList(this.configProps$, this.pConn$.getDataObject())];
272+
// optionsList?.unshift({ key: 'Select', value: this.pConn$.getLocalizedValue('Select...', '', '') });
273+
// this.options$ = optionsList;
274+
// }
275+
}
276+
238277
getData(datasource, parameters, columns, context) {
239278
this.dataPageService.getDataPageData(datasource, parameters, context).then((results: any) => {
240279
const optionsData: any[] = [];

0 commit comments

Comments
 (0)