1
|
import {Component, ViewChild, OnInit, Input, Output} from '@angular/core';
|
2
|
import {ViewEncapsulation, EventEmitter} from '@angular/core';
|
3
|
import {SimpleChanges, OnChanges} from '@angular/core';
|
4
|
import {FormGroup, FormArray, FormBuilder, Validators} from '@angular/forms';
|
5
|
import {ActivatedRoute, Router} from '@angular/router';
|
6
|
import {Subject} from 'rxjs/Subject';
|
7
|
import {DataTableDirective} from 'angular-datatables';
|
8
|
|
9
|
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
10
|
import {SubscribeService} from '../../openaireLibrary/utils/subscribe/subscribe.service';
|
11
|
import {RouterHelper} from '../../openaireLibrary/utils/routerHelper.class';
|
12
|
|
13
|
import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes';
|
14
|
import {SearchUtilsClass} from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class';
|
15
|
|
16
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
17
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
18
|
|
19
|
@Component({
|
20
|
selector: 'manage-subscribers',
|
21
|
templateUrl: './manage-subscribers.component.html',
|
22
|
styles: [`
|
23
|
#dpTable_info, #dpTable_paginate, #dpTable_length, #dpTable_filter{
|
24
|
display: none;
|
25
|
}
|
26
|
`],
|
27
|
encapsulation: ViewEncapsulation.None // this used in order styles to work
|
28
|
})
|
29
|
|
30
|
export class ManageSubscribersComponent implements OnInit {
|
31
|
public routerHelper:RouterHelper = new RouterHelper();
|
32
|
|
33
|
private errorCodes: ErrorCodes;
|
34
|
|
35
|
@Output() commmunitySubscribersChanged = new EventEmitter();
|
36
|
public communitySubscribers = [];
|
37
|
public subscribersSearchUtils: SearchUtilsClass = new SearchUtilsClass();
|
38
|
|
39
|
public sub: any; public subResults: any; subRemove: any;
|
40
|
public properties: EnvProperties;
|
41
|
|
42
|
public disableForms: boolean = false;
|
43
|
|
44
|
dtOptions: DataTables.Settings = {};
|
45
|
showTable = false; filteringAdded = false;
|
46
|
@ViewChild(DataTableDirective) datatableElement: DataTableDirective;
|
47
|
dtTrigger: Subject<any> = new Subject(); //necessary
|
48
|
|
49
|
public rowsOnPage:number = 10;
|
50
|
|
51
|
public queryParameters: string = "";
|
52
|
|
53
|
public query = '';
|
54
|
public selectedSubscribersEmail=[];
|
55
|
public elementRef;
|
56
|
|
57
|
public subscribers:string[];
|
58
|
private triggered: boolean = false;
|
59
|
|
60
|
private selectedSubscriberEmail: any;
|
61
|
|
62
|
public communityId = null;
|
63
|
|
64
|
@ViewChild('AlertModalDeleteSubscriber') alertModalDeleteSubscriber;
|
65
|
|
66
|
constructor (private route: ActivatedRoute,
|
67
|
private _router: Router,
|
68
|
public _fb: FormBuilder,
|
69
|
private _subscribeService: SubscribeService) {
|
70
|
|
71
|
this.errorCodes = new ErrorCodes();
|
72
|
this.subscribersSearchUtils.status = this.errorCodes.LOADING;
|
73
|
}
|
74
|
|
75
|
ngOnInit() {
|
76
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
77
|
this.properties = data.envSpecific;
|
78
|
this.route.queryParams.subscribe(
|
79
|
|
80
|
communityId => {
|
81
|
this.communityId = communityId['communityId'];
|
82
|
|
83
|
if (this.communityId != null && this.communityId != '') {
|
84
|
this._getCommunitySubscribers();
|
85
|
}
|
86
|
});
|
87
|
});
|
88
|
|
89
|
this.dtOptions = {
|
90
|
"pageLength": this.rowsOnPage,
|
91
|
"language": {
|
92
|
"search": "",
|
93
|
"searchPlaceholder": "Search subscribers..."
|
94
|
}
|
95
|
};
|
96
|
|
97
|
this.subscribersSearchUtils.keyword = "";
|
98
|
}
|
99
|
|
100
|
public ngOnDestroy() {
|
101
|
if(this.sub){
|
102
|
this.sub.unsubscribe();
|
103
|
}
|
104
|
if(this.subResults) {
|
105
|
this.subResults.unsubscribe();
|
106
|
}
|
107
|
if(this.subRemove) {
|
108
|
this.subRemove.unsubscribe();
|
109
|
}
|
110
|
|
111
|
$.fn['dataTable'].ext.search.pop();
|
112
|
}
|
113
|
|
114
|
rerender(): void {
|
115
|
this.datatableElement.dtInstance.then((dtInstance: DataTables.Api) => {
|
116
|
// Destroy the table first
|
117
|
dtInstance.destroy();
|
118
|
|
119
|
// Call the dtTrigger to rerender again
|
120
|
this.dtTrigger.next();
|
121
|
});
|
122
|
}
|
123
|
|
124
|
ngAfterViewInit(): void {
|
125
|
$.fn['dataTable'].ext.search.push((settings, data, dataIndex) => {
|
126
|
|
127
|
if (this.filterData(data, this.subscribersSearchUtils.keyword)) {
|
128
|
return true;
|
129
|
}
|
130
|
return false;
|
131
|
});
|
132
|
|
133
|
console.info("ngAfterViewInit");
|
134
|
}
|
135
|
|
136
|
filterData(row: any, query: string) {
|
137
|
if(!Session.isLoggedIn()){
|
138
|
console.info(this._router.url);
|
139
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
140
|
} else {
|
141
|
let returnValue: boolean = false;
|
142
|
|
143
|
if(query) {
|
144
|
for(var i = 0; i < 2; i++) {
|
145
|
var r= this.filterQuery(row[i], query);
|
146
|
if(r) {
|
147
|
returnValue = true;
|
148
|
break;
|
149
|
}
|
150
|
}
|
151
|
|
152
|
if(!returnValue) {
|
153
|
return false;
|
154
|
}
|
155
|
}
|
156
|
return true;
|
157
|
}
|
158
|
}
|
159
|
|
160
|
filterQuery(data, query){
|
161
|
if(data.toLowerCase().indexOf(query.toLowerCase()) > -1) {
|
162
|
return true;
|
163
|
} else {
|
164
|
return false;
|
165
|
}
|
166
|
}
|
167
|
|
168
|
/*
|
169
|
Trigger a table draw in order to get the initial filtering
|
170
|
*/
|
171
|
triggerInitialLoad(){
|
172
|
this.triggered = true;
|
173
|
console.info("triggerInitialLoad");
|
174
|
setTimeout(function() {
|
175
|
var table = (<any>$('#dpTable')).DataTable();
|
176
|
table.page( 0 ).draw( false );
|
177
|
}, 500);
|
178
|
this.dtTrigger.next();
|
179
|
}
|
180
|
|
181
|
public goTo(page:number = 1) {
|
182
|
if(!Session.isLoggedIn()){
|
183
|
console.info(this._router.url);
|
184
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
185
|
} else {
|
186
|
this.subscribersSearchUtils.page=page;
|
187
|
|
188
|
var table = $('#dpTable').DataTable();
|
189
|
table.page( page - 1 ).draw( false );
|
190
|
|
191
|
var info = table.page.info();
|
192
|
console.info("records: "+info.recordsDisplay);
|
193
|
this.subscribersSearchUtils.totalResults = info.recordsDisplay;
|
194
|
}
|
195
|
}
|
196
|
|
197
|
totalPages(): number {
|
198
|
let totalPages:any = this.subscribersSearchUtils.totalResults/(this.rowsOnPage);
|
199
|
if(!(Number.isInteger(totalPages))) {
|
200
|
totalPages = (parseInt(totalPages, 10) + 1);
|
201
|
}
|
202
|
return totalPages;
|
203
|
}
|
204
|
|
205
|
public removeSubscriber(email:string) {
|
206
|
if(!Session.isLoggedIn()){
|
207
|
console.info(this._router.url);
|
208
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
209
|
} else {
|
210
|
this.selectedSubscriberEmail = email;
|
211
|
this.alertModalDeleteSubscriber.cancelButton = true;
|
212
|
this.alertModalDeleteSubscriber.okButton = true;
|
213
|
this.alertModalDeleteSubscriber.alertTitle = "Remove subscriber?";
|
214
|
|
215
|
this.alertModalDeleteSubscriber.message = "Subscriber with email ";
|
216
|
if (email) {
|
217
|
this.alertModalDeleteSubscriber.message += " '"+email+"' ";
|
218
|
}
|
219
|
|
220
|
this.alertModalDeleteSubscriber.message += "will be removed from your community. Are you sure?";
|
221
|
this.alertModalDeleteSubscriber.okButtonText = "Yes";
|
222
|
this.alertModalDeleteSubscriber.open();
|
223
|
}
|
224
|
}
|
225
|
|
226
|
public confirmedDeleteSubscriber() {
|
227
|
if(!Session.isLoggedIn()){
|
228
|
console.info(this._router.url);
|
229
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
230
|
} else {
|
231
|
console.info("REMOVE: "+ this.selectedSubscriberEmail);
|
232
|
this.subRemove = this._subscribeService.unSubscribeToCommunity(this.communityId, this.selectedSubscriberEmail, this.properties.adminToolsAPIURL).subscribe(
|
233
|
result => {
|
234
|
console.log(result);
|
235
|
console.info("email REMOVE");
|
236
|
console.log(this.selectedSubscriberEmail);
|
237
|
this.communitySubscribers = result;
|
238
|
},
|
239
|
err => {
|
240
|
console.info("error REMOVE");
|
241
|
console.log(err);
|
242
|
},
|
243
|
() => {
|
244
|
console.info("completed REMOVE");
|
245
|
console.log(this.selectedSubscriberEmail);
|
246
|
console.log(this.communitySubscribers["subscribers"]);
|
247
|
this.subscribersSearchUtils.totalResults--;
|
248
|
this.subscribersSearchUtils.page=1;
|
249
|
|
250
|
this.rerender();
|
251
|
}
|
252
|
)
|
253
|
}
|
254
|
}
|
255
|
|
256
|
public _getCommunitySubscribers(){
|
257
|
if(!Session.isLoggedIn()){
|
258
|
console.info(this._router.url);
|
259
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
260
|
} else {
|
261
|
this.subscribersSearchUtils.status = this.errorCodes.LOADING;
|
262
|
this.disableForms = true;
|
263
|
this.communitySubscribers = [];
|
264
|
this.subscribersSearchUtils.totalResults = 0;
|
265
|
this.subscribersSearchUtils.page=1;
|
266
|
this.subscribersSearchUtils.keyword = "";
|
267
|
|
268
|
this.subResults = this._subscribeService.getCommunitySubscribers(this.communityId, this.properties.adminToolsAPIURL).subscribe(
|
269
|
|
270
|
res => {
|
271
|
console.info(res);
|
272
|
console.info("search Subscribers [total communitySubscribers:"+res.subscribers.length+"]");
|
273
|
this.communitySubscribers = res;
|
274
|
|
275
|
this.subscribersSearchUtils.totalResults = res.subscribers.length;
|
276
|
this.subscribersSearchUtils.status = this.errorCodes.DONE;
|
277
|
|
278
|
this.disableForms = false;
|
279
|
if(!this.triggered) {
|
280
|
this.triggerInitialLoad();
|
281
|
} else {
|
282
|
var table = $('#dpTable').DataTable();
|
283
|
table.clear();
|
284
|
|
285
|
this.rerender();
|
286
|
}
|
287
|
|
288
|
this.commmunitySubscribersChanged.emit({
|
289
|
value: this.communitySubscribers,
|
290
|
});
|
291
|
},
|
292
|
err => {
|
293
|
console.log(err);
|
294
|
//TODO check erros (service not available, bad request)
|
295
|
|
296
|
if(err.status == '404') {
|
297
|
this.subscribersSearchUtils.status = this.errorCodes.NOT_FOUND;
|
298
|
} else if(err.status == '500') {
|
299
|
this.subscribersSearchUtils.status = this.errorCodes.ERROR;
|
300
|
} else {
|
301
|
this.subscribersSearchUtils.status = this.errorCodes.NOT_AVAILABLE;
|
302
|
}
|
303
|
|
304
|
this.disableForms = false;
|
305
|
if(!this.triggered) {
|
306
|
this.triggerInitialLoad();
|
307
|
} else {
|
308
|
var table = $('#dpTable').DataTable();
|
309
|
table.clear();
|
310
|
|
311
|
this.rerender();
|
312
|
}
|
313
|
}
|
314
|
);
|
315
|
}
|
316
|
}
|
317
|
}
|