Skip to content

Commit 4d0ed40

Browse files
committed
fix: show new records created in resouce questions in the search grid
1 parent 9b16a37 commit 4d0ed40

File tree

4 files changed

+151
-58
lines changed

4 files changed

+151
-58
lines changed

libs/safe/src/lib/components/ui/core-grid/core-grid.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ export class SafeCoreGridComponent
655655
console.error(error);
656656
}
657657
}
658-
if (this.settings.query.temporaryRecords) {
658+
if (this.settings.query.temporaryRecords?.length) {
659659
//Handles temporary records for resources creation in forms
660660
this.getTemporaryRecords();
661661
}

libs/safe/src/lib/survey/components/resource.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@ import {
77
import * as SurveyCreator from 'survey-creator';
88
import { resourceConditions } from './resources';
99
import { Dialog } from '@angular/cdk/dialog';
10-
import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
10+
import {
11+
FormControl,
12+
UntypedFormBuilder,
13+
UntypedFormGroup,
14+
} from '@angular/forms';
1115
import { SafeResourceDropdownComponent } from '../../components/resource-dropdown/resource-dropdown.component';
1216
import { DomService } from '../../services/dom/dom.service';
13-
import { buildSearchButton, buildAddButton } from './utils';
17+
import {
18+
buildSearchButton,
19+
buildAddButton,
20+
processNewCreatedRecords,
21+
} from './utils';
1422
import get from 'lodash/get';
1523
import { Question, QuestionResource } from '../types';
1624
import { JsonMetadata, SurveyModel } from 'survey-angular';
1725

26+
/** Question's temporary records */
27+
export const temporaryRecordsForm = new FormControl([]);
28+
1829
/**
1930
* Inits the resource question component of for survey.
2031
*
@@ -541,6 +552,20 @@ export const init = (
541552
this.populateChoices(question);
542553
}
543554
}
555+
if (question.addRecord && question.canSearch) {
556+
// If search button exists, updates grid displayed records when new records are created with the add button
557+
question.registerFunctionOnPropertyValueChanged(
558+
'newCreatedRecords',
559+
async () => {
560+
const settings = await processNewCreatedRecords(
561+
question,
562+
false,
563+
[]
564+
);
565+
temporaryRecordsForm.setValue(settings.query.temporaryRecords);
566+
}
567+
);
568+
}
544569
}
545570
},
546571
/**
@@ -607,7 +632,8 @@ export const init = (
607632
question,
608633
question.gridFieldsSettings,
609634
false,
610-
dialog
635+
dialog,
636+
temporaryRecordsForm
611637
);
612638
actionsButtons.appendChild(searchBtn);
613639

libs/safe/src/lib/survey/components/resources.ts

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@ import {
66
} from '../graphql/queries';
77
import { BehaviorSubject } from 'rxjs';
88
import * as SurveyCreator from 'survey-creator';
9-
import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
9+
import {
10+
FormControl,
11+
UntypedFormBuilder,
12+
UntypedFormGroup,
13+
} from '@angular/forms';
1014
import { Dialog } from '@angular/cdk/dialog';
1115
import { SafeResourceDropdownComponent } from '../../components/resource-dropdown/resource-dropdown.component';
1216
import { SafeCoreGridComponent } from '../../components/ui/core-grid/core-grid.component';
1317
import { DomService } from '../../services/dom/dom.service';
14-
import { buildSearchButton, buildAddButton } from './utils';
18+
import {
19+
buildSearchButton,
20+
buildAddButton,
21+
processNewCreatedRecords,
22+
} from './utils';
1523
import { QuestionResource } from '../types';
1624
import { SurveyModel } from 'survey-angular';
17-
import localForage from 'localforage';
1825

1926
/** Create the list of filter values for resources */
2027
export const resourcesFilterValues = new BehaviorSubject<
@@ -32,6 +39,9 @@ export const resourceConditions = [
3239
{ value: '<=', text: 'less or equals' },
3340
];
3441

42+
/** Question temporary records */
43+
const temporaryRecordsForm = new FormControl([]);
44+
3545
/**
3646
* Inits the resources question component for survey.
3747
*
@@ -787,7 +797,8 @@ export const init = (
787797
question,
788798
question.gridFieldsSettings,
789799
true,
790-
dialog
800+
dialog,
801+
temporaryRecordsForm
791802
);
792803
actionsButtons.appendChild(searchBtn);
793804

@@ -894,53 +905,8 @@ export const init = (
894905
question: any
895906
) => {
896907
instance.multiSelect = true;
897-
const query = question.gridFieldsSettings || {};
898-
const temporaryRecords: any[] = [];
899908
const promises: any[] = [];
900-
question.newCreatedRecords?.forEach((recordId: string) => {
901-
const promise = new Promise<void>((resolve, reject) => {
902-
localForage
903-
.getItem(recordId)
904-
.then((data: any) => {
905-
if (data != null) {
906-
// We ensure to make it only if such a record is found
907-
const parsedData = JSON.parse(data);
908-
temporaryRecords.push({
909-
id: recordId,
910-
template: parsedData.template,
911-
...parsedData.data,
912-
isTemporary: true,
913-
});
914-
}
915-
resolve();
916-
})
917-
.catch((error: any) => {
918-
console.error(error); // Handle any errors that occur while getting the item
919-
reject(error);
920-
});
921-
});
922-
promises.push(promise);
923-
});
924-
const uuidRegExpr =
925-
/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
926-
const settings = {
927-
query: {
928-
...query,
929-
temporaryRecords: temporaryRecords,
930-
filter: {
931-
logic: 'and',
932-
filters: [
933-
{
934-
field: 'ids',
935-
operator: 'eq',
936-
value:
937-
question.value.filter((id: string) => !uuidRegExpr.test(id)) ||
938-
[], //We exclude the temporary records by excluding id in UUID format
939-
},
940-
],
941-
},
942-
},
943-
};
909+
const settings = await processNewCreatedRecords(question, true, promises);
944910
if (!question.readOnlyGrid) {
945911
Object.assign(settings, {
946912
actions: {
@@ -953,6 +919,10 @@ export const init = (
953919
},
954920
});
955921
}
922+
// If search button exists, updates grid displayed records
923+
if (question.canSearch) {
924+
temporaryRecordsForm.setValue(settings.query.temporaryRecords);
925+
}
956926
instance.settings = settings;
957927
Promise.allSettled(promises).then(() => {
958928
instance.configureGrid();

libs/safe/src/lib/survey/components/utils.ts

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import { Dialog } from '@angular/cdk/dialog';
2-
import { UntypedFormGroup } from '@angular/forms';
2+
import { UntypedFormControl } from '@angular/forms';
33
import { surveyLocalization } from 'survey-angular';
4+
import localForage from 'localforage';
45

56
/**
67
* Build the search button for resource and resources components
78
*
89
* @param question The question object
9-
* @param fieldsSettingsForm The form used for the button
10+
* @param fieldsSettingsForm The raw value from the form used for the grid settings
1011
* @param multiselect Indicate if we need multiselect
1112
* @param dialog The Dialog service
13+
* @param temporaryRecords The form used to save and keep the temporary records updated
1214
* @returns The button DOM element
1315
*/
1416
export const buildSearchButton = (
1517
question: any,
16-
fieldsSettingsForm: UntypedFormGroup,
18+
fieldsSettingsForm: any,
1719
multiselect: boolean,
18-
dialog: Dialog
20+
dialog: Dialog,
21+
temporaryRecords: UntypedFormControl
1922
): any => {
2023
const searchButton = document.createElement('button');
2124
searchButton.innerText = surveyLocalization.getString(
@@ -24,6 +27,11 @@ export const buildSearchButton = (
2427
);
2528
searchButton.style.marginRight = '8px';
2629
if (fieldsSettingsForm) {
30+
temporaryRecords.valueChanges.subscribe((res: any) => {
31+
if (res) {
32+
fieldsSettingsForm.temporaryRecords = res;
33+
}
34+
});
2735
searchButton.onclick = async () => {
2836
const { SafeResourceGridModalComponent } = await import(
2937
'../../components/search-resource-grid-modal/search-resource-grid-modal.component'
@@ -147,3 +155,92 @@ export const buildAddButton = (
147155
);
148156
return addButton;
149157
};
158+
159+
/**
160+
* Updates the newCreatedRecords for resource and resources questions
161+
*
162+
* @param question The question object
163+
* @param multiselect Indicate if the questions is multiselect
164+
* @param promises Promises array to be do something after they're all reject or resolved.
165+
* @returns The grid settings updated
166+
*/
167+
export const processNewCreatedRecords = (
168+
question: any,
169+
multiselect: boolean,
170+
promises: Promise<void>[]
171+
): any => {
172+
const query = question.gridFieldsSettings || {};
173+
const temporaryRecords: any[] = [];
174+
if (multiselect) {
175+
question.newCreatedRecords?.forEach((recordId: string) => {
176+
const promise = new Promise<void>((resolve, reject) => {
177+
localForage
178+
.getItem(recordId)
179+
.then((data: any) => {
180+
if (data != null) {
181+
// We ensure to make it only if such a record is found
182+
const parsedData = JSON.parse(data);
183+
temporaryRecords.push({
184+
id: recordId,
185+
template: parsedData.template,
186+
...parsedData.data,
187+
isTemporary: true,
188+
});
189+
}
190+
resolve();
191+
})
192+
.catch((error: any) => {
193+
console.error(error); // Handle any errors that occur while getting the item
194+
reject(error);
195+
});
196+
});
197+
promises.push(promise);
198+
});
199+
} else {
200+
new Promise<void>((resolve, reject) => {
201+
localForage
202+
.getItem(question.newCreatedRecords)
203+
.then((data: any) => {
204+
if (data != null) {
205+
// We ensure to make it only if such a record is found
206+
const parsedData = JSON.parse(data);
207+
temporaryRecords.push({
208+
id: question.newCreatedRecords,
209+
template: parsedData.template,
210+
...parsedData.data,
211+
isTemporary: true,
212+
});
213+
}
214+
resolve();
215+
})
216+
.catch((error: any) => {
217+
console.error(error); // Handle any errors that occur while getting the item
218+
reject(error);
219+
});
220+
});
221+
}
222+
223+
const uuidRegExpr =
224+
/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
225+
const settings = {
226+
query: {
227+
...query,
228+
temporaryRecords: temporaryRecords,
229+
filter: {
230+
logic: 'and',
231+
...(multiselect && {
232+
filters: [
233+
{
234+
field: 'ids',
235+
operator: 'eq',
236+
value:
237+
question.value.filter((id: string) => !uuidRegExpr.test(id)) ||
238+
[], //We exclude the temporary records by excluding id in UUID format
239+
},
240+
],
241+
}),
242+
},
243+
},
244+
};
245+
return settings;
246+
};

0 commit comments

Comments
 (0)