1
|
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
|
2
|
import {ActivatedRoute, Router} from '@angular/router';
|
3
|
import {HelpContentService} from '../../services/help-content.service';
|
4
|
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
|
5
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
6
|
|
7
|
import {Session} from '../../login/utils/helper.class';
|
8
|
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
|
9
|
import {HelperFunctions} from "../../utils/HelperFunctions.class";
|
10
|
import {Subscriber} from "rxjs";
|
11
|
import {CheckPortal, Portal} from "../../utils/entities/adminTool/portal";
|
12
|
import {PortalUtils} from "./portalHelper";
|
13
|
import {properties} from "../../../../environments/environment";
|
14
|
import {AlertModal} from "../../utils/modal/alert";
|
15
|
|
16
|
@Component({
|
17
|
selector: 'portals',
|
18
|
templateUrl: './portals.component.html',
|
19
|
})
|
20
|
|
21
|
export class PortalsComponent implements OnInit {
|
22
|
|
23
|
@ViewChild('editModal') editModal: AlertModal;
|
24
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
25
|
private selectedPortals: string[] = [];
|
26
|
|
27
|
public checkboxes: CheckPortal[] = [];
|
28
|
public portals: Portal[] = [];
|
29
|
|
30
|
public portalForm: FormGroup;
|
31
|
public filterForm: FormGroup;
|
32
|
private subscriptions: any[] = [];
|
33
|
|
34
|
private searchText: RegExp = new RegExp('');
|
35
|
public keyword = '';
|
36
|
|
37
|
public properties: EnvProperties = null;
|
38
|
|
39
|
public showLoading = true;
|
40
|
public errorMessage = '';
|
41
|
public updateErrorMessage = '';
|
42
|
public modalErrorMessage = '';
|
43
|
public portalUtils: PortalUtils = new PortalUtils();
|
44
|
private index: number;
|
45
|
|
46
|
ngOnInit() {
|
47
|
this.filterForm = this._fb.group({
|
48
|
keyword: [''],
|
49
|
type: ['all', Validators.required]
|
50
|
});
|
51
|
this.subscriptions.push(this.filterForm.get('keyword').valueChanges.subscribe(value => {
|
52
|
this.filterBySearch(value);
|
53
|
}));
|
54
|
this.subscriptions.push(this.filterForm.get('type').valueChanges.subscribe(value => {
|
55
|
this.applyTypeFilter();
|
56
|
}));
|
57
|
|
58
|
HelperFunctions.scroll();
|
59
|
this.properties = properties;
|
60
|
this.getPortals();
|
61
|
|
62
|
}
|
63
|
|
64
|
constructor(private element: ElementRef, private route: ActivatedRoute,
|
65
|
private _router: Router, private _helpContentService: HelpContentService, private _fb: FormBuilder) {
|
66
|
}
|
67
|
|
68
|
|
69
|
ngOnDestroy(): void {
|
70
|
this.subscriptions.forEach(value => {
|
71
|
if (value instanceof Subscriber) {
|
72
|
value.unsubscribe();
|
73
|
} else if (value instanceof Function) {
|
74
|
value();
|
75
|
}
|
76
|
});
|
77
|
}
|
78
|
|
79
|
getPortals() {
|
80
|
if (!Session.isLoggedIn()) {
|
81
|
this._router.navigate(['/user-info'], {
|
82
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
83
|
});
|
84
|
} else {
|
85
|
this.showLoading = true;
|
86
|
this.updateErrorMessage = '';
|
87
|
this.errorMessage = '';
|
88
|
|
89
|
|
90
|
this.subscriptions.push(this._helpContentService.getPortalsFull(this.properties.adminToolsAPIURL).subscribe(
|
91
|
portals => {
|
92
|
this.portals = portals;
|
93
|
if (portals) {
|
94
|
portals.forEach(_ => {
|
95
|
this.checkboxes.push(<CheckPortal>{portal: _, checked: false});
|
96
|
});
|
97
|
}
|
98
|
this.showLoading = false;
|
99
|
},
|
100
|
error => this.handleError('System error retrieving portals', error)));
|
101
|
}
|
102
|
}
|
103
|
|
104
|
public toggleCheckBoxes(event) {
|
105
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
106
|
}
|
107
|
|
108
|
public applyCheck(flag: boolean) {
|
109
|
this.checkboxes.forEach(_ => _.checked = flag);
|
110
|
}
|
111
|
|
112
|
public getSelectedPortals(): string[] {
|
113
|
return this.checkboxes.filter(portal => portal.checked === true).map(checkedPortal => checkedPortal.portal).map(res => res._id);
|
114
|
}
|
115
|
|
116
|
private deletePortalsFromArray(ids: string[]): void {
|
117
|
for (let id of ids) {
|
118
|
let i = this.portals.findIndex(_ => _._id == id);
|
119
|
this.portals.splice(i, 1);
|
120
|
}
|
121
|
this.applyTypeFilter();
|
122
|
this.applyFilter();
|
123
|
}
|
124
|
|
125
|
public confirmDeletePortal(id: string) {
|
126
|
// this.deleteConfirmationModal.ids = [id];
|
127
|
// this.deleteConfirmationModal.showModal();
|
128
|
this.selectedPortals = [id];
|
129
|
this.confirmModalOpen();
|
130
|
}
|
131
|
|
132
|
public confirmDeleteSelectedPortals() {
|
133
|
this.selectedPortals = this.getSelectedPortals();
|
134
|
this.confirmModalOpen();
|
135
|
}
|
136
|
|
137
|
private confirmModalOpen() {
|
138
|
if (!Session.isLoggedIn()) {
|
139
|
this._router.navigate(['/user-info'], {
|
140
|
queryParams: {
|
141
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
142
|
"redirectUrl": this._router.url
|
143
|
}
|
144
|
});
|
145
|
} else {
|
146
|
this.deleteModal.cancelButton = true;
|
147
|
this.deleteModal.okButton = true;
|
148
|
this.deleteModal.alertTitle = 'Delete Confirmation';
|
149
|
this.deleteModal.message = 'Are you sure you want to delete the selected portal(-ies)?';
|
150
|
this.deleteModal.okButtonText = 'Yes';
|
151
|
this.deleteModal.open();
|
152
|
}
|
153
|
}
|
154
|
|
155
|
public confirmedDeletePortals(data: any) {
|
156
|
if (!Session.isLoggedIn()) {
|
157
|
this._router.navigate(['/user-info'], {
|
158
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
159
|
});
|
160
|
} else {
|
161
|
this.showLoading = true;
|
162
|
this.updateErrorMessage = '';
|
163
|
|
164
|
this.subscriptions.push(this._helpContentService.deleteCommunities(this.selectedPortals, this.properties.adminToolsAPIURL).subscribe(
|
165
|
_ => {
|
166
|
this.deletePortalsFromArray(this.selectedPortals);
|
167
|
this.showLoading = false;
|
168
|
},
|
169
|
error => this.handleUpdateError('System error deleting the selected communities', error)
|
170
|
));
|
171
|
}
|
172
|
}
|
173
|
|
174
|
public editPortal(i: number) {
|
175
|
const portal: Portal = this.checkboxes[i].portal;
|
176
|
this.index = this.portals.findIndex(value => value._id === portal._id);
|
177
|
this.portalForm = this._fb.group({
|
178
|
_id: this._fb.control(portal._id),
|
179
|
name: this._fb.control(portal.name, Validators.required),
|
180
|
pid: this._fb.control(portal.pid, Validators.required),
|
181
|
piwik: this._fb.control(portal.piwik),
|
182
|
type: this._fb.control(portal.type, Validators.required),
|
183
|
});
|
184
|
this.portalForm.controls['type'].disable();
|
185
|
this.modalErrorMessage = '';
|
186
|
this.portalModalOpen('Edit Portal', 'Save');
|
187
|
}
|
188
|
|
189
|
public newPortal() {
|
190
|
this.portalForm.controls['type'].enable();
|
191
|
this.portalForm = this._fb.group({
|
192
|
_id: this._fb.control(''),
|
193
|
name: this._fb.control('', Validators.required),
|
194
|
pid: this._fb.control('', Validators.required),
|
195
|
piwik: this._fb.control(''),
|
196
|
type: this._fb.control('', Validators.required),
|
197
|
});
|
198
|
this.modalErrorMessage = '';
|
199
|
this.portalModalOpen('Create Portal', 'Create');
|
200
|
}
|
201
|
|
202
|
private portalModalOpen(title: string, yesBtn: string) {
|
203
|
if (!Session.isLoggedIn()) {
|
204
|
this._router.navigate(['/user-info'], {
|
205
|
queryParams: {
|
206
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
207
|
"redirectUrl": this._router.url
|
208
|
}
|
209
|
});
|
210
|
} else {
|
211
|
this.editModal.okButtonLeft = false;
|
212
|
this.editModal.cancelButton = true;
|
213
|
this.editModal.okButton = true;
|
214
|
this.editModal.alertTitle = title;
|
215
|
this.editModal.okButtonText = yesBtn;
|
216
|
this.editModal.open();
|
217
|
}
|
218
|
}
|
219
|
|
220
|
public portalSaveConfirmed(data: any) {
|
221
|
if (!Session.isLoggedIn()) {
|
222
|
this._router.navigate(['/user-info'], {
|
223
|
queryParams: {
|
224
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
225
|
"redirectUrl": this._router.url
|
226
|
}
|
227
|
});
|
228
|
} else {
|
229
|
this.modalErrorMessage = '';
|
230
|
if (this.portalForm.value._id) {
|
231
|
this.portalForm.controls['type'].enable();
|
232
|
this.subscriptions.push(this._helpContentService.updateCommunity(<Portal>this.portalForm.value,
|
233
|
this.properties.adminToolsAPIURL).subscribe(
|
234
|
portal => {
|
235
|
this.portalUpdatedSuccessfully(portal);
|
236
|
},
|
237
|
error => this.handleUpdateError('System error updating portal', error)
|
238
|
));
|
239
|
} else {
|
240
|
this.subscriptions.push(this._helpContentService.saveCommunity(<Portal>this.portalForm.value,
|
241
|
this.properties.adminToolsAPIURL).subscribe(
|
242
|
portal => {
|
243
|
this.portalSavedSuccessfully(portal);
|
244
|
},
|
245
|
error => this.handleUpdateError('System error creating portal', error)
|
246
|
));
|
247
|
}
|
248
|
}
|
249
|
}
|
250
|
|
251
|
public portalUpdateConfirmed(data: any) {
|
252
|
if (!Session.isLoggedIn()) {
|
253
|
this._router.navigate(['/user-info'], {
|
254
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
255
|
});
|
256
|
} else {
|
257
|
this.portalForm.controls['type'].enable();
|
258
|
this.subscriptions.push(this._helpContentService.updateCommunity(<Portal>this.portalForm.value,
|
259
|
this.properties.adminToolsAPIURL).subscribe(
|
260
|
portal => {
|
261
|
this.portalUpdatedSuccessfully(portal);
|
262
|
},
|
263
|
error => this.handleUpdateError('System error updating portal', error)
|
264
|
));
|
265
|
}
|
266
|
}
|
267
|
|
268
|
public portalSavedSuccessfully(portal: Portal) {
|
269
|
this.portals.push(portal)
|
270
|
this.applyTypeFilter();
|
271
|
this.applyFilter();
|
272
|
this.applyCheck(false);
|
273
|
}
|
274
|
|
275
|
public portalUpdatedSuccessfully(portal: Portal) {
|
276
|
this.portals[this.index] = portal;
|
277
|
this.applyTypeFilter();
|
278
|
this.applyFilter();
|
279
|
this.applyCheck(false);
|
280
|
}
|
281
|
|
282
|
public filterBySearch(text: string) {
|
283
|
this.searchText = new RegExp(text, 'i');
|
284
|
this.applyFilter();
|
285
|
}
|
286
|
|
287
|
public applyFilter() {
|
288
|
this.checkboxes = [];
|
289
|
this.portals.filter(item => this.filterPortals(item)).forEach(
|
290
|
_ => this.checkboxes.push(<CheckPortal>{portal: _, checked: false})
|
291
|
);
|
292
|
}
|
293
|
|
294
|
public applyTypeFilter() {
|
295
|
this.checkboxes = [];
|
296
|
this.portals.filter(item => this.filterByType(item)).forEach(
|
297
|
_ => this.checkboxes.push(<CheckPortal>{portal: _, checked: false})
|
298
|
);
|
299
|
}
|
300
|
|
301
|
public filterByType(portal: Portal): boolean {
|
302
|
let type = this.filterForm.get("type").value;
|
303
|
return type == "all" || (type == portal.type);
|
304
|
}
|
305
|
|
306
|
public filterPortals(portal: Portal): boolean {
|
307
|
const textFlag = this.searchText.toString() === '' || (portal.name || portal.type).match(this.searchText) != null;
|
308
|
return textFlag;
|
309
|
}
|
310
|
|
311
|
handleUpdateError(message: string, error) {
|
312
|
if (error == null) {
|
313
|
this.portalForm = this._fb.group({
|
314
|
name: '',
|
315
|
_id: '',
|
316
|
pid: '',
|
317
|
piwik: '',
|
318
|
type: ''
|
319
|
});
|
320
|
} else {
|
321
|
this.updateErrorMessage = message;
|
322
|
console.log('Server responded: ' + error);
|
323
|
}
|
324
|
this.showLoading = false;
|
325
|
}
|
326
|
|
327
|
handleError(message: string, error) {
|
328
|
this.errorMessage = message;
|
329
|
console.log('Server responded: ' + error);
|
330
|
this.showLoading = false;
|
331
|
}
|
332
|
}
|