Skip to content

Commit 4b4c98f

Browse files
authored
fix: resource modal opening as a blank page until we click anywhere (#1664)
1 parent 04a6560 commit 4b4c98f

File tree

5 files changed

+96
-65
lines changed

5 files changed

+96
-65
lines changed

libs/safe/src/lib/services/form/form.service.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Inject, Injectable } from '@angular/core';
1+
import { Inject, Injectable, NgZone } from '@angular/core';
22
import * as SurveyKo from 'survey-knockout';
33
import * as Survey from 'survey-angular';
44
import { initCreatorSettings } from '../../survey/creator';
@@ -30,6 +30,7 @@ export class SafeFormService {
3030
* @param formBuilder Angular form builder
3131
* @param authService Shared authentication service
3232
* @param referenceDataService Reference data service
33+
* @param ngZone Angular Service to execute code inside Angular environment
3334
*/
3435
constructor(
3536
@Inject('environment') environment: any,
@@ -38,7 +39,8 @@ export class SafeFormService {
3839
public apollo: Apollo,
3940
public formBuilder: UntypedFormBuilder,
4041
public authService: SafeAuthService,
41-
public referenceDataService: SafeReferenceDataService
42+
public referenceDataService: SafeReferenceDataService,
43+
public ngZone: NgZone
4244
) {
4345
this.environment = environment;
4446
this.setSurveyCreatorInstance();
@@ -65,7 +67,8 @@ export class SafeFormService {
6567
this.authService,
6668
this.environment,
6769
this.referenceDataService,
68-
additionalQuestions.customQuestions
70+
additionalQuestions.customQuestions,
71+
this.ngZone
6972
);
7073
// === CREATOR SETTINGS ===
7174
initCreatorSettings(SurveyKo);
@@ -79,7 +82,8 @@ export class SafeFormService {
7982
this.authService,
8083
this.environment,
8184
this.referenceDataService,
82-
additionalQuestions.customQuestions
85+
additionalQuestions.customQuestions,
86+
this.ngZone
8387
);
8488
}
8589
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { buildSearchButton, buildAddButton } from './utils';
1414
import get from 'lodash/get';
1515
import { Question, QuestionResource } from '../types';
1616
import { JsonMetadata, SurveyModel } from 'survey-angular';
17+
import { NgZone } from '@angular/core';
1718

1819
/**
1920
* Inits the resource question component of for survey.
@@ -23,13 +24,15 @@ import { JsonMetadata, SurveyModel } from 'survey-angular';
2324
* @param apollo Apollo client
2425
* @param dialog Dialog
2526
* @param formBuilder Angular form service
27+
* @param ngZone Angular Service to execute code inside Angular environment
2628
*/
2729
export const init = (
2830
Survey: any,
2931
domService: DomService,
3032
apollo: Apollo,
3133
dialog: Dialog,
32-
formBuilder: UntypedFormBuilder
34+
formBuilder: UntypedFormBuilder,
35+
ngZone: NgZone
3336
): void => {
3437
const getResourceById = (data: { id: string }) =>
3538
apollo.query<GetResourceByIdQueryResponse>({
@@ -600,7 +603,7 @@ export const init = (
600603
);
601604
actionsButtons.appendChild(searchBtn);
602605

603-
const addBtn = buildAddButton(question, false, dialog);
606+
const addBtn = buildAddButton(question, false, dialog, ngZone);
604607
actionsButtons.appendChild(addBtn);
605608

606609
const parentElement = el.querySelector('.safe-qst-content');

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { buildSearchButton, buildAddButton } from './utils';
1515
import { QuestionResource } from '../types';
1616
import { SurveyModel } from 'survey-angular';
1717
import localForage from 'localforage';
18+
import { NgZone } from '@angular/core';
1819

1920
/** Create the list of filter values for resources */
2021
export const resourcesFilterValues = new BehaviorSubject<
@@ -40,13 +41,15 @@ export const resourceConditions = [
4041
* @param apollo Apollo client
4142
* @param dialog Dialog service
4243
* @param formBuilder Angular form service
44+
* @param ngZone Angular Service to execute code inside Angular environment
4345
*/
4446
export const init = (
4547
Survey: any,
4648
domService: DomService,
4749
apollo: Apollo,
4850
dialog: Dialog,
49-
formBuilder: UntypedFormBuilder
51+
formBuilder: UntypedFormBuilder,
52+
ngZone: NgZone
5053
): void => {
5154
const getResourceById = (data: { id: string }) =>
5255
apollo.query<GetResourceByIdQueryResponse>({
@@ -768,7 +771,7 @@ export const init = (
768771
);
769772
actionsButtons.appendChild(searchBtn);
770773

771-
const addBtn = buildAddButton(question, true, dialog);
774+
const addBtn = buildAddButton(question, true, dialog, ngZone);
772775
actionsButtons.appendChild(addBtn);
773776

774777
parentElement.insertBefore(

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

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Dialog } from '@angular/cdk/dialog';
2+
import { NgZone } from '@angular/core';
23
import { UntypedFormGroup } from '@angular/forms';
34
import { surveyLocalization } from 'survey-angular';
45

@@ -64,12 +65,14 @@ export const buildSearchButton = (
6465
* @param question The question object
6566
* @param multiselect Indicate if we need multiselect
6667
* @param dialog The Dialog service
68+
* @param ngZone Angular Service to execute code inside Angular environment
6769
* @returns The button DOM element
6870
*/
6971
export const buildAddButton = (
7072
question: any,
7173
multiselect: boolean,
72-
dialog: Dialog
74+
dialog: Dialog,
75+
ngZone: NgZone
7376
): any => {
7477
const addButton = document.createElement('button');
7578
addButton.innerText = surveyLocalization.getString(
@@ -78,60 +81,61 @@ export const buildAddButton = (
7881
);
7982
if (question.addRecord && question.addTemplate && !question.isReadOnly) {
8083
addButton.onclick = async () => {
81-
const { SafeResourceModalComponent } = await import(
82-
'../../components/resource-modal/resource-modal.component'
83-
);
84-
const dialogRef = dialog.open(SafeResourceModalComponent, {
85-
disableClose: true,
86-
data: {
87-
template: question.addTemplate,
88-
locale: question.resource.value,
89-
askForConfirm: false,
90-
...(question.prefillWithCurrentRecord && {
91-
prefillData: question.survey.data,
92-
}),
93-
},
94-
height: '98%',
95-
width: '100vw',
96-
panelClass: 'full-screen-modal',
97-
autoFocus: false,
98-
});
99-
dialogRef.closed.subscribe((result: any) => {
100-
if (result) {
101-
const { data } = result;
102-
// TODO: call reload method
103-
// if (question.displayAsGrid && gridComponent) {
104-
// gridComponent.availableRecords.push({
105-
// value: data.id,
106-
// text: data.data[question.displayField]
107-
// });
108-
// }
109-
if (multiselect) {
110-
const newItem = {
111-
value: data.id,
112-
text: data.data[question.displayField],
113-
};
114-
question.contentQuestion.choices = [
115-
newItem,
116-
...question.contentQuestion.choices,
117-
];
118-
question.newCreatedRecords = question.newCreatedRecords
119-
? question.newCreatedRecords.concat(data.id)
120-
: [data.id];
121-
question.value = question.value.concat(data.id);
122-
} else {
123-
const newItem = {
124-
value: data.id,
125-
text: data.data[question.displayField],
126-
};
127-
question.contentQuestion.choices = [
128-
newItem,
129-
...question.contentQuestion.choices,
130-
];
131-
question.newCreatedRecords = data.id;
132-
question.value = data.id;
84+
ngZone.run(async () => {
85+
const { SafeResourceModalComponent } = await import(
86+
'../../components/resource-modal/resource-modal.component'
87+
);
88+
const dialogRef = dialog.open(SafeResourceModalComponent, {
89+
disableClose: true,
90+
data: {
91+
template: question.addTemplate,
92+
locale: question.resource.value,
93+
askForConfirm: false,
94+
...(question.prefillWithCurrentRecord && {
95+
prefillData: question.survey.data,
96+
}),
97+
},
98+
height: '98%',
99+
width: '100vw',
100+
panelClass: 'full-screen-modal',
101+
});
102+
dialogRef.closed.subscribe((result: any) => {
103+
if (result) {
104+
const { data } = result;
105+
// TODO: call reload method
106+
// if (question.displayAsGrid && gridComponent) {
107+
// gridComponent.availableRecords.push({
108+
// value: data.id,
109+
// text: data.data[question.displayField]
110+
// });
111+
// }
112+
if (multiselect) {
113+
const newItem = {
114+
value: data.id,
115+
text: data.data[question.displayField],
116+
};
117+
question.contentQuestion.choices = [
118+
newItem,
119+
...question.contentQuestion.choices,
120+
];
121+
question.newCreatedRecords = question.newCreatedRecords
122+
? question.newCreatedRecords.concat(data.id)
123+
: [data.id];
124+
question.value = question.value.concat(data.id);
125+
} else {
126+
const newItem = {
127+
value: data.id,
128+
text: data.data[question.displayField],
129+
};
130+
question.contentQuestion.choices = [
131+
newItem,
132+
...question.contentQuestion.choices,
133+
];
134+
question.newCreatedRecords = data.id;
135+
question.value = data.id;
136+
}
133137
}
134-
}
138+
});
135139
});
136140
};
137141
}

libs/safe/src/lib/survey/init.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as ReferenceDataProperties from './global-properties/reference-data';
2121
import * as TooltipProperty from './global-properties/tooltip';
2222
import { initLocalization } from './localization';
2323
import { Dialog } from '@angular/cdk/dialog';
24+
import { NgZone } from '@angular/core';
2425

2526
/**
2627
* Executes all init methods of custom SurveyJS.
@@ -34,6 +35,7 @@ import { Dialog } from '@angular/cdk/dialog';
3435
* @param environment injected environment
3536
* @param referenceDataService Reference data service
3637
* @param containsCustomQuestions If survey contains custom questions or not
38+
* @param ngZone Angular Service to execute code inside Angular environment
3739
*/
3840
export const initCustomSurvey = (
3941
Survey: any,
@@ -44,7 +46,8 @@ export const initCustomSurvey = (
4446
authService: SafeAuthService,
4547
environment: any,
4648
referenceDataService: SafeReferenceDataService,
47-
containsCustomQuestions: boolean
49+
containsCustomQuestions: boolean,
50+
ngZone: NgZone
4851
): void => {
4952
// If the survey created does not contain custom questions, we destroy previously set custom questions if so
5053
if (!containsCustomQuestions) {
@@ -59,8 +62,22 @@ export const initCustomSurvey = (
5962
if (containsCustomQuestions) {
6063
CommentWidget.init(Survey);
6164
// load components (same as widgets, but with less configuration options)
62-
ResourceComponent.init(Survey, domService, apollo, dialog, formBuilder);
63-
ResourcesComponent.init(Survey, domService, apollo, dialog, formBuilder);
65+
ResourceComponent.init(
66+
Survey,
67+
domService,
68+
apollo,
69+
dialog,
70+
formBuilder,
71+
ngZone
72+
);
73+
ResourcesComponent.init(
74+
Survey,
75+
domService,
76+
apollo,
77+
dialog,
78+
formBuilder,
79+
ngZone
80+
);
6481
OwnerComponent.init(Survey, domService, apollo);
6582
UsersComponent.init(Survey, domService, apollo);
6683
}

0 commit comments

Comments
 (0)