1
|
import {Component, ViewChild, OnInit, ElementRef} from '@angular/core';
|
2
|
import {ActivatedRoute, Router} from '@angular/router';
|
3
|
import {HelpContentService} from '../../services/help-content.service';
|
4
|
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
|
5
|
import {CheckEntity, Entity} from '../../utils/entities/adminTool/entity';
|
6
|
import {Portal} from '../../utils/entities/adminTool/portal';
|
7
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
8
|
import {Session} from '../../login/utils/helper.class';
|
9
|
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
|
10
|
import {HelperFunctions} from "../../utils/HelperFunctions.class";
|
11
|
import {UserManagementService} from '../../services/user-management.service';
|
12
|
import {Subscriber} from "rxjs";
|
13
|
import {properties} from "../../../../environments/environment";
|
14
|
import {ConnectHelper} from "../../connect/connectHelper";
|
15
|
|
16
|
@Component({
|
17
|
selector: 'entities',
|
18
|
templateUrl: './entities.component.html',
|
19
|
})
|
20
|
|
21
|
export class EntitiesComponent implements OnInit {
|
22
|
|
23
|
@ViewChild('AlertModalSaveEntity') alertModalSaveEntity;
|
24
|
@ViewChild('AlertModalDeleteEntities') alertModalDeleteEntities;
|
25
|
private selectedEntities: string[] = [];
|
26
|
|
27
|
public checkboxes: CheckEntity[] = [];
|
28
|
|
29
|
public entities: Entity[] = [];
|
30
|
|
31
|
public myForm: FormGroup;
|
32
|
|
33
|
private searchText: RegExp = new RegExp('');
|
34
|
public keyword = '';
|
35
|
|
36
|
public communities: Portal[] = [];
|
37
|
public portal: string;
|
38
|
|
39
|
@ViewChild('AlertModalRelatedPages') alertModalRelatedPages;
|
40
|
|
41
|
public toggleIds: string[];
|
42
|
public toggleStatus: boolean;
|
43
|
public properties: EnvProperties = properties;
|
44
|
|
45
|
public showLoading = true;
|
46
|
public errorMessage = '';
|
47
|
public updateErrorMessage = '';
|
48
|
public modalErrorMessage = '';
|
49
|
public isPortalAdministrator = null;
|
50
|
public filterForm: FormGroup;
|
51
|
private subscriptions: any[] = [];
|
52
|
|
53
|
constructor(private element: ElementRef, private route: ActivatedRoute,
|
54
|
private _router: Router,
|
55
|
private _helpContentService: HelpContentService,
|
56
|
private userManagementService: UserManagementService, private _fb: FormBuilder) {
|
57
|
}
|
58
|
|
59
|
ngOnInit() {
|
60
|
this.filterForm = this._fb.group({
|
61
|
keyword: [''],
|
62
|
status: ['all', Validators.required]
|
63
|
});
|
64
|
|
65
|
this.myForm = this._fb.group({
|
66
|
pid: ['', Validators.required],
|
67
|
name: ['', Validators.required],
|
68
|
isEnabled: '',
|
69
|
_id: ''
|
70
|
});
|
71
|
this.subscriptions.push(this.filterForm.get('keyword').valueChanges.subscribe(value => {
|
72
|
this.filterBySearch(value);
|
73
|
}));
|
74
|
this.subscriptions.push(this.filterForm.get('status').valueChanges.subscribe(value => {
|
75
|
this.applyStatusFilter();
|
76
|
}));
|
77
|
this.userManagementService.getUserInfo().subscribe(user => {
|
78
|
this.portal = (this.route.snapshot.data.portal) ? this.route.snapshot.data.portal : this.route.snapshot.params[this.route.snapshot.data.param];
|
79
|
if(this.portal === 'connect' || this.portal === 'explore') {
|
80
|
ConnectHelper.setPortalTypeFromPid(this.portal);
|
81
|
}
|
82
|
this.applyPortalFilter(this.portal);
|
83
|
this.isPortalAdministrator = Session.isPortalAdministrator(user) && !this.portal;
|
84
|
});
|
85
|
|
86
|
|
87
|
}
|
88
|
|
89
|
ngOnDestroy(): void {
|
90
|
this.subscriptions.forEach(value => {
|
91
|
if (value instanceof Subscriber) {
|
92
|
value.unsubscribe();
|
93
|
} else if (value instanceof Function) {
|
94
|
value();
|
95
|
}
|
96
|
});
|
97
|
}
|
98
|
|
99
|
getEntities(portal: string) {
|
100
|
if (!Session.isLoggedIn()) {
|
101
|
this._router.navigate(['/user-info'],
|
102
|
{queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
103
|
} else {
|
104
|
this.showLoading = true;
|
105
|
this.updateErrorMessage = '';
|
106
|
this.errorMessage = '';
|
107
|
if (portal) {
|
108
|
this._helpContentService.getCommunityEntities(portal, this.properties.adminToolsAPIURL).subscribe(
|
109
|
entities => {
|
110
|
this.entities = entities;
|
111
|
this.checkboxes = [];
|
112
|
|
113
|
let self = this;
|
114
|
entities.forEach(_ => {
|
115
|
self.checkboxes.push(<CheckEntity>{entity: _, checked: false});
|
116
|
});
|
117
|
|
118
|
this.showLoading = false;
|
119
|
},
|
120
|
error => this.handleError('System error retrieving entities', error));
|
121
|
} else {
|
122
|
this._helpContentService.getEntities(this.properties.adminToolsAPIURL).subscribe(
|
123
|
entities => {
|
124
|
this.entities = entities;
|
125
|
this.checkboxes = [];
|
126
|
|
127
|
let self = this;
|
128
|
entities.forEach(_ => {
|
129
|
self.checkboxes.push(<CheckEntity>{entity: _, checked: false});
|
130
|
});
|
131
|
this.showLoading = false;
|
132
|
},
|
133
|
error => this.handleError('System error retrieving entities', error));
|
134
|
}
|
135
|
}
|
136
|
}
|
137
|
|
138
|
public toggleCheckBoxes(event) {
|
139
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
140
|
}
|
141
|
|
142
|
public applyCheck(flag: boolean) {
|
143
|
this.checkboxes.forEach(_ => _.checked = flag);
|
144
|
}
|
145
|
|
146
|
public getSelectedEntities(): string[] {
|
147
|
return this.checkboxes.filter(entity => entity.checked === true).map(checkedEntity => checkedEntity.entity).map(res => res._id);
|
148
|
}
|
149
|
|
150
|
private deleteEntitiesFromArray(ids: string[]): void {
|
151
|
for (let id of ids) {
|
152
|
const i = this.checkboxes.findIndex(_ => _.entity._id === id);
|
153
|
this.checkboxes.splice(i, 1);
|
154
|
}
|
155
|
}
|
156
|
|
157
|
public confirmDeleteEntity(id: string) {
|
158
|
// this.deleteConfirmationModal.ids = [id];
|
159
|
// this.deleteConfirmationModal.showModal();
|
160
|
this.selectedEntities = [id];
|
161
|
this.confirmDeleteEntitiesModalOpen();
|
162
|
}
|
163
|
|
164
|
public confirmDeleteSelectedEntities() {
|
165
|
// this.deleteConfirmationModal.ids = this.getSelectedEntities();
|
166
|
// this.deleteConfirmationModal.showModal();
|
167
|
this.selectedEntities = this.getSelectedEntities();
|
168
|
this.confirmDeleteEntitiesModalOpen();
|
169
|
}
|
170
|
|
171
|
private confirmDeleteEntitiesModalOpen() {
|
172
|
if (!Session.isLoggedIn()) {
|
173
|
this._router.navigate(['/user-info'],
|
174
|
{queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
175
|
} else {
|
176
|
this.alertModalDeleteEntities.cancelButton = true;
|
177
|
this.alertModalDeleteEntities.okButton = true;
|
178
|
this.alertModalDeleteEntities.alertTitle = 'Delete Confirmation';
|
179
|
this.alertModalDeleteEntities.message = 'Are you sure you want to delete the selected entity(-ies)?';
|
180
|
this.alertModalDeleteEntities.okButtonText = 'Yes';
|
181
|
this.alertModalDeleteEntities.open();
|
182
|
}
|
183
|
}
|
184
|
|
185
|
public confirmedDeleteEntities(data: any) {
|
186
|
if (!Session.isLoggedIn()) {
|
187
|
this._router.navigate(['/user-info'],
|
188
|
{queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
189
|
} else {
|
190
|
this.showLoading = true;
|
191
|
this.updateErrorMessage = '';
|
192
|
|
193
|
this._helpContentService.deleteEntities(this.selectedEntities, this.properties.adminToolsAPIURL).subscribe(
|
194
|
_ => {
|
195
|
this.deleteEntitiesFromArray(this.selectedEntities);
|
196
|
this.showLoading = false;
|
197
|
},
|
198
|
error => this.handleUpdateError('System error deleting the selected entities', error)
|
199
|
);
|
200
|
}
|
201
|
}
|
202
|
|
203
|
public editEntity(i: number) {
|
204
|
const entity: Entity = this.checkboxes[i].entity;
|
205
|
this.myForm = this._fb.group({
|
206
|
name: [entity.name, Validators.required],
|
207
|
_id: entity._id,
|
208
|
pid: [entity.pid, Validators.required],
|
209
|
});
|
210
|
this.modalErrorMessage = '';
|
211
|
this.entitiesModalOpen(this.alertModalSaveEntity, '', 'Save Changes');
|
212
|
}
|
213
|
|
214
|
public newEntity() {
|
215
|
this.myForm = this._fb.group({
|
216
|
pid: ['', Validators.required],
|
217
|
name: ['', Validators.required],
|
218
|
isEnabled: '',
|
219
|
_id: ''
|
220
|
});
|
221
|
this.modalErrorMessage = '';
|
222
|
this.entitiesModalOpen(this.alertModalSaveEntity, '', 'Save');
|
223
|
}
|
224
|
|
225
|
private entitiesModalOpen(modal: any, title: string, yesBtn: string) {
|
226
|
if (!Session.isLoggedIn()) {
|
227
|
this._router.navigate(['/user-info'],
|
228
|
{queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
229
|
} else {
|
230
|
modal.cancelButton = true;
|
231
|
modal.okButton = true;
|
232
|
modal.alertTitle = title;
|
233
|
modal.okButtonText = yesBtn;
|
234
|
modal.open();
|
235
|
}
|
236
|
}
|
237
|
|
238
|
public entitySaveConfirmed(data: any) {
|
239
|
if (!Session.isLoggedIn()) {
|
240
|
this._router.navigate(['/user-info'],
|
241
|
{queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
242
|
} else {
|
243
|
this.modalErrorMessage = '';
|
244
|
if (this.myForm.getRawValue()['_id'].length > 0) {
|
245
|
this._helpContentService.updateEntity(
|
246
|
<Entity>this.myForm.value, this.properties.adminToolsAPIURL).subscribe(
|
247
|
entity => {
|
248
|
this.entityUpdatedSuccessfully(entity);
|
249
|
},
|
250
|
error => this.handleUpdateError('System error updating entity', error)
|
251
|
);
|
252
|
} else {
|
253
|
this._helpContentService.saveEntity(<Entity>this.myForm.value, this.properties.adminToolsAPIURL).subscribe(
|
254
|
entity => {
|
255
|
this.entitySavedSuccessfully(entity);
|
256
|
},
|
257
|
error => this.handleUpdateError('System error creating entity', error)
|
258
|
);
|
259
|
}
|
260
|
}
|
261
|
}
|
262
|
|
263
|
|
264
|
public entitySavedSuccessfully(entity: Entity) {
|
265
|
this.checkboxes.push(<CheckEntity>{entity: entity, checked: false});
|
266
|
this.applyCheck(false);
|
267
|
}
|
268
|
|
269
|
public entityUpdatedSuccessfully(entity: Entity) {
|
270
|
this.checkboxes.find(checkItem => checkItem.entity._id === entity._id).entity = entity;
|
271
|
this.applyCheck(false);
|
272
|
}
|
273
|
|
274
|
public filterBySearch(text: string) {
|
275
|
this.searchText = new RegExp(text, 'i');
|
276
|
this.applyFilter();
|
277
|
}
|
278
|
|
279
|
public applyFilter() {
|
280
|
this.checkboxes = [];
|
281
|
this.entities.filter(item => this.filterEntities(item)).forEach(
|
282
|
_ => this.checkboxes.push(<CheckEntity>{entity: _, checked: false})
|
283
|
);
|
284
|
}
|
285
|
|
286
|
public filterEntities(entity: Entity): boolean {
|
287
|
const textFlag = this.searchText.toString() === '' || (entity.name).match(this.searchText) != null;
|
288
|
return textFlag;
|
289
|
}
|
290
|
|
291
|
public applyStatusFilter() {
|
292
|
this.checkboxes = [];
|
293
|
this.entities.filter(item => this.filterEntitiesByStatus(item)).forEach(
|
294
|
_ => this.checkboxes.push(<CheckEntity>{entity: _, checked: false})
|
295
|
);
|
296
|
}
|
297
|
|
298
|
public filterEntitiesByStatus(entity: Entity): boolean {
|
299
|
let status = this.filterForm.get("status").value;
|
300
|
return status == "all" || (status == "disabled" && !entity.isEnabled) || (status == "enabled" && entity.isEnabled);
|
301
|
}
|
302
|
|
303
|
handleError(message: string, error) {
|
304
|
this.errorMessage = message;
|
305
|
console.log('Server responded: ' + error);
|
306
|
|
307
|
this.showLoading = false;
|
308
|
}
|
309
|
|
310
|
handleUpdateError(message: string, error) {
|
311
|
if (error == null) {
|
312
|
this.myForm = this._fb.group({
|
313
|
pid: ['', Validators.required],
|
314
|
name: ['', Validators.required],
|
315
|
isEnabled: '',
|
316
|
_id: ''
|
317
|
});
|
318
|
} else {
|
319
|
this.updateErrorMessage = message;
|
320
|
console.log('Server responded: ' + error);
|
321
|
}
|
322
|
|
323
|
this.showLoading = false;
|
324
|
}
|
325
|
|
326
|
public applyPortalFilter(portal: string) {
|
327
|
this.getEntities(portal);
|
328
|
}
|
329
|
|
330
|
public toggleEntities(status: boolean, ids: string[]) {
|
331
|
// this.okModal.showModal();
|
332
|
this.toggleIds = ids;
|
333
|
this.toggleStatus = status;
|
334
|
this.confirmRelatedPagesModalOpen();
|
335
|
}
|
336
|
|
337
|
private confirmRelatedPagesModalOpen() {
|
338
|
if (!Session.isLoggedIn()) {
|
339
|
this._router.navigate(['/user-info'],
|
340
|
{queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
341
|
} else {
|
342
|
this.alertModalRelatedPages.cancelButton = true;
|
343
|
this.alertModalRelatedPages.okButton = true;
|
344
|
this.alertModalRelatedPages.alertTitle = 'Warning';
|
345
|
this.alertModalRelatedPages.message = "This action will affect all search pages related to this entity! Pages' status will change to entity's status! Do you want to continue?";
|
346
|
this.alertModalRelatedPages.okButtonText = 'Yes';
|
347
|
this.alertModalRelatedPages.open();
|
348
|
}
|
349
|
}
|
350
|
|
351
|
public continueToggling(event: any) {
|
352
|
if (!Session.isLoggedIn()) {
|
353
|
this._router.navigate(['/user-info'],
|
354
|
{queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
355
|
} else {
|
356
|
this.updateErrorMessage = '';
|
357
|
this._helpContentService.toggleEntities(
|
358
|
this.portal, this.toggleIds, this.toggleStatus, this.properties.adminToolsAPIURL).subscribe(
|
359
|
() => {
|
360
|
for (let id of this.toggleIds) {
|
361
|
const i = this.checkboxes.findIndex(_ => _.entity._id === id);
|
362
|
this.checkboxes[i].entity.isEnabled = this.toggleStatus;
|
363
|
}
|
364
|
this.applyCheck(false);
|
365
|
},
|
366
|
error => this.handleUpdateError('System error changing the status of the selected entity(-ies)', error)
|
367
|
);
|
368
|
}
|
369
|
}
|
370
|
}
|