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