1
|
import {Component, OnDestroy, OnInit, ViewChild} from "@angular/core";
|
2
|
import {StakeholderService} from "../services/stakeholder.service";
|
3
|
import {EnvProperties} from "../openaireLibrary/utils/properties/env-properties";
|
4
|
import {EnvironmentSpecificService} from "../openaireLibrary/utils/properties/environment-specific.service";
|
5
|
import {Stakeholder} from "../utils/entities/stakeholder";
|
6
|
import {Subscriber, zip} from "rxjs";
|
7
|
import {StakeholderUtils} from "../utils/indicator-utils";
|
8
|
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
|
9
|
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
|
10
|
import {StakeholderCreator} from "../utils/entities/stakeholderCreator";
|
11
|
import {LayoutService} from "../openaireLibrary/dashboard/sharedComponents/sidebar/layout.service";
|
12
|
import {Option} from "../openaireLibrary/dashboard/sharedComponents/input/input.component";
|
13
|
|
14
|
@Component({
|
15
|
selector: 'home',
|
16
|
templateUrl: "./manageStakeholders.component.html"
|
17
|
})
|
18
|
export class ManageStakeholdersComponent implements OnInit, OnDestroy {
|
19
|
|
20
|
public properties: EnvProperties;
|
21
|
public loading: boolean = true;
|
22
|
public stakeholderUtils: StakeholderUtils = new StakeholderUtils();
|
23
|
public defaultStakeholders: Stakeholder[];
|
24
|
public stakeholders: Stakeholder[];
|
25
|
public stakeholder: Stakeholder;
|
26
|
public stakeholderFb: FormGroup;
|
27
|
public index: number;
|
28
|
|
29
|
/**
|
30
|
* Filtered Stakeholders
|
31
|
*/
|
32
|
public displayDefaultStakeholders: Stakeholder[];
|
33
|
public displayStakeholders: Stakeholder[];
|
34
|
|
35
|
/**
|
36
|
* Top filters
|
37
|
*/
|
38
|
public filters: FormGroup;
|
39
|
public all: Option = {
|
40
|
value: 'all',
|
41
|
label: 'All'
|
42
|
};
|
43
|
|
44
|
/**
|
45
|
* Grid or List View
|
46
|
*/
|
47
|
public grid: boolean = true;
|
48
|
private subscriptions: any[] = [];
|
49
|
|
50
|
@ViewChild('editStakeholderModal') editStakeholderModal: AlertModal;
|
51
|
@ViewChild('deleteStakeholderModal') deleteStakeholderModal: AlertModal;
|
52
|
|
53
|
constructor(private stakeholderService: StakeholderService,
|
54
|
private propertiesService: EnvironmentSpecificService,
|
55
|
private fb: FormBuilder, private layoutService: LayoutService) {
|
56
|
}
|
57
|
|
58
|
ngOnInit(): void {
|
59
|
this.buildFilters();
|
60
|
this.propertiesService.loadEnvironment()
|
61
|
.then(properties => {
|
62
|
this.properties = properties;
|
63
|
let data = zip(
|
64
|
this.stakeholderService.getDefaultStakeholders(this.properties.monitorServiceAPIURL),
|
65
|
this.stakeholderService.getStakeholders(this.properties.monitorServiceAPIURL)
|
66
|
);
|
67
|
data.subscribe(res => {
|
68
|
this.defaultStakeholders = res[0];
|
69
|
this.stakeholders = res[1];
|
70
|
this.displayDefaultStakeholders = res[0];
|
71
|
this.displayStakeholders = res[1];
|
72
|
this.loading = false;
|
73
|
});
|
74
|
});
|
75
|
this.layoutService.setHasHeader(true);
|
76
|
this.layoutService.setOpen(true);
|
77
|
}
|
78
|
|
79
|
ngOnDestroy(): void {
|
80
|
this.subscriptions.forEach(value => {
|
81
|
if (value instanceof Subscriber) {
|
82
|
value.unsubscribe();
|
83
|
}
|
84
|
});
|
85
|
}
|
86
|
|
87
|
public changeGrid(value) {
|
88
|
this.grid = value;
|
89
|
}
|
90
|
|
91
|
private buildFilters() {
|
92
|
this.filters = this.fb.group({
|
93
|
privacy: this.fb.control('all'),
|
94
|
status: this.fb.control('all'),
|
95
|
keyword: this.fb.control('')
|
96
|
});
|
97
|
this.subscriptions.push(this.filters.get('privacy').valueChanges.subscribe(value => {
|
98
|
this.onPrivacyChange(value);
|
99
|
}));
|
100
|
this.subscriptions.push(this.filters.get('status').valueChanges.subscribe(value => {
|
101
|
this.onStatusChange(value);
|
102
|
}));
|
103
|
this.subscriptions.push(this.filters.get('keyword').valueChanges.subscribe(value => {
|
104
|
this.onKeywordChange(value);
|
105
|
}));
|
106
|
}
|
107
|
|
108
|
onPrivacyChange(value) {
|
109
|
this.displayDefaultStakeholders = this.filterPrivacy(this.defaultStakeholders, value);
|
110
|
this.displayStakeholders = this.filterPrivacy(this.stakeholders, value);
|
111
|
}
|
112
|
|
113
|
onStatusChange(value) {
|
114
|
this.displayDefaultStakeholders = this.filterStatus(this.defaultStakeholders, value);
|
115
|
this.displayStakeholders = this.filterStatus(this.stakeholders, value);
|
116
|
}
|
117
|
|
118
|
onKeywordChange(value) {
|
119
|
this.displayDefaultStakeholders = this.filterByKeyword(this.defaultStakeholders, value);
|
120
|
this.displayStakeholders = this.filterByKeyword(this.stakeholders, value);
|
121
|
}
|
122
|
|
123
|
|
124
|
private filterPrivacy(stakeholders: Stakeholder[], value): Stakeholder[] {
|
125
|
if (value === 'all') {
|
126
|
return stakeholders;
|
127
|
} else {
|
128
|
return stakeholders.filter(stakeholder => stakeholder.isPublic === value);
|
129
|
}
|
130
|
}
|
131
|
|
132
|
private filterStatus(stakeholders: Stakeholder[], value): Stakeholder[] {
|
133
|
if (value === 'all') {
|
134
|
return stakeholders;
|
135
|
} else {
|
136
|
return stakeholders.filter(stakeholder => stakeholder.isActive === value);
|
137
|
}
|
138
|
}
|
139
|
|
140
|
private filterByKeyword(stakeholders: Stakeholder[], value): Stakeholder[] {
|
141
|
if (value === null || value === '') {
|
142
|
return stakeholders;
|
143
|
} else {
|
144
|
return stakeholders.filter(stakeholder => (
|
145
|
stakeholder.index_id && stakeholder.index_id.toLowerCase().includes(value.toLowerCase())) ||
|
146
|
stakeholder.index_shortName && stakeholder.index_shortName.toLowerCase().includes(value.toLowerCase()) ||
|
147
|
stakeholder.index_name && stakeholder.index_name.toLowerCase().includes(value.toLowerCase())
|
148
|
);
|
149
|
}
|
150
|
}
|
151
|
|
152
|
public editStakeholder(stakeholder: Stakeholder = null, isDefault: boolean = false) {
|
153
|
if (isDefault) {
|
154
|
this.index = (stakeholder) ? this.defaultStakeholders.findIndex(value => value._id === stakeholder._id) : -1;
|
155
|
} else {
|
156
|
this.index = (stakeholder) ? this.stakeholders.findIndex(value => value._id === stakeholder._id) : -1;
|
157
|
}
|
158
|
if (!stakeholder) {
|
159
|
this.stakeholder = new Stakeholder(null, null, null,
|
160
|
null, null, isDefault, null, false, false, null);
|
161
|
} else {
|
162
|
this.stakeholder = stakeholder;
|
163
|
}
|
164
|
this.stakeholderFb = this.fb.group({
|
165
|
_id: this.fb.control(this.stakeholder._id),
|
166
|
index_name: this.fb.control(this.stakeholder.index_name, Validators.required),
|
167
|
index_id: this.fb.control(this.stakeholder.index_id, Validators.required),
|
168
|
index_shortName: this.fb.control(this.stakeholder.index_shortName, Validators.required),
|
169
|
isDefaultProfile: this.fb.control(this.stakeholder.isDefaultProfile),
|
170
|
creationDate: this.fb.control(this.stakeholder.creationDate),
|
171
|
alias: this.fb.control(this.stakeholder.alias,
|
172
|
[
|
173
|
Validators.required,
|
174
|
this.stakeholderUtils.aliasValidator(
|
175
|
(this.stakeholder.isDefaultProfile) ?
|
176
|
this.defaultStakeholders.filter(stakeholder => stakeholder.alias !== this.stakeholder.alias):
|
177
|
this.stakeholders.filter(stakeholder => stakeholder.alias !== this.stakeholder.alias)
|
178
|
)]
|
179
|
),
|
180
|
isPublic: this.fb.control(this.stakeholder.isPublic),
|
181
|
isActive: this.fb.control(this.stakeholder.isActive),
|
182
|
type: this.fb.control(this.stakeholder.type, Validators.required),
|
183
|
topics: this.fb.control(this.stakeholder.topics),
|
184
|
managers: this.fb.control(this.stakeholder.managers),
|
185
|
logoUrl: this.fb.control(this.stakeholder.logoUrl)
|
186
|
});
|
187
|
if (this.index !== -1) {
|
188
|
if (this.stakeholderFb.value.type) {
|
189
|
setTimeout(() => {
|
190
|
this.stakeholderFb.get('type').disable();
|
191
|
}, 0);
|
192
|
} else {
|
193
|
setTimeout(() => {
|
194
|
this.stakeholderFb.get('type').enable();
|
195
|
}, 0);
|
196
|
}
|
197
|
this.editStakeholderModal.okButtonText = 'Save Changes';
|
198
|
} else {
|
199
|
setTimeout(() => {
|
200
|
this.stakeholderFb.get('type').enable();
|
201
|
}, 0);
|
202
|
this.editStakeholderModal.okButtonText = 'Create';
|
203
|
}
|
204
|
this.editStakeholderModal.cancelButtonText = 'Cancel';
|
205
|
this.editStakeholderModal.okButtonLeft = false;
|
206
|
this.editStakeholderModal.alertMessage = false;
|
207
|
this.editStakeholderModal.open();
|
208
|
}
|
209
|
|
210
|
public saveStakeholder() {
|
211
|
if (this.index === -1) {
|
212
|
if (!this.stakeholderFb.value.isDefaultProfile) {
|
213
|
// this.stakeholderFb.setValue(this.stakeholderUtils.
|
214
|
// createFunderFromDefaultProfile(this.stakeholderFb.value,
|
215
|
// this.defaultStakeholders.find( value => value.type === this.stakeholderFb.value.type).topics));
|
216
|
this.stakeholderFb.setValue(this.stakeholderUtils.createFunderFromDefaultProfile(this.stakeholderFb.value, StakeholderCreator.createFunderDefaultProfile().topics));
|
217
|
}/* else {
|
218
|
this.stakeholderFb.setValue(StakeholderCreator.createFunderDefaultProfile());
|
219
|
}*/
|
220
|
this.stakeholderService.buildStakeholder(this.properties.monitorServiceAPIURL, this.stakeholderFb.value).subscribe(stakeholder => {
|
221
|
if (stakeholder.isDefaultProfile) {
|
222
|
this.defaultStakeholders.push(stakeholder);
|
223
|
} else {
|
224
|
this.stakeholders.push(stakeholder);
|
225
|
}
|
226
|
});
|
227
|
} else {
|
228
|
this.stakeholderFb.get('type').enable();
|
229
|
this.stakeholderService.saveElement(this.properties.monitorServiceAPIURL, this.stakeholderFb.value).subscribe(stakeholder => {
|
230
|
if (stakeholder.isDefaultProfile) {
|
231
|
this.defaultStakeholders[this.index] = stakeholder;
|
232
|
} else {
|
233
|
this.stakeholders[this.index] = stakeholder;
|
234
|
}
|
235
|
});
|
236
|
}
|
237
|
}
|
238
|
|
239
|
public deleteStakeholderOpen(stakeholder: Stakeholder) {
|
240
|
this.stakeholder = stakeholder;
|
241
|
this.deleteStakeholderModal.alertTitle = 'Delete ' + this.stakeholder.index_name;
|
242
|
this.deleteStakeholderModal.cancelButtonText = 'No';
|
243
|
this.deleteStakeholderModal.okButtonText = 'Yes';
|
244
|
this.deleteStakeholderModal.message = 'This stakeholder will permanently be deleted. Are you sure you want to proceed?';
|
245
|
this.deleteStakeholderModal.open();
|
246
|
}
|
247
|
|
248
|
public deleteStakeholder() {
|
249
|
if (this.stakeholder.isDefaultProfile) {
|
250
|
this.index = (this.stakeholder) ? this.defaultStakeholders.findIndex(value => value._id === this.stakeholder._id) : -1;
|
251
|
} else {
|
252
|
this.index = (this.stakeholder) ? this.stakeholders.findIndex(value => value._id === this.stakeholder._id) : -1;
|
253
|
}
|
254
|
this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, [this.stakeholder._id]).subscribe(() => {
|
255
|
if (this.stakeholder.isDefaultProfile) {
|
256
|
this.defaultStakeholders.splice(this.index, 1);
|
257
|
} else {
|
258
|
this.stakeholders.splice(this.index, 1);
|
259
|
}
|
260
|
});
|
261
|
}
|
262
|
}
|