1
|
import {Component, Input} from '@angular/core';
|
2
|
import {Location} from '@angular/common';
|
3
|
import {ActivatedRoute, Router} from '@angular/router';
|
4
|
import {Session} from '../../login/utils/helper.class';
|
5
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
6
|
import {MailPrefsService} from './mailPrefs.service';
|
7
|
import {ConnectHelper} from '../connectHelper';
|
8
|
import {ErrorCodes} from '../../utils/properties/errorCodes';
|
9
|
import {ErrorMessagesComponent} from '../../utils/errorMessages.component';
|
10
|
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
|
11
|
import {properties} from "../../../../environments/environment";
|
12
|
import {Subscriber} from "rxjs";
|
13
|
|
14
|
declare var UIkit: any;
|
15
|
|
16
|
@Component({
|
17
|
selector: 'mailPrefs',
|
18
|
templateUrl: 'mailPrefs.component.html',
|
19
|
providers:[MailPrefsService]
|
20
|
|
21
|
})
|
22
|
export class MailPrefsComponent {
|
23
|
properties:EnvProperties;
|
24
|
subscriptions = [];
|
25
|
public communityId: string;
|
26
|
public preferencesFor: string = "community";
|
27
|
public status: number;
|
28
|
|
29
|
public notifications = [];
|
30
|
public initialNotifications = [];
|
31
|
public prefsChanged = {};
|
32
|
public hidden: boolean = true;
|
33
|
|
34
|
//public showForbiddenMessage:boolean = false;
|
35
|
public userValidMessage:string = "";
|
36
|
public savedMessage: string = "";
|
37
|
|
38
|
private errorCodes: ErrorCodes;
|
39
|
private errorMessages: ErrorMessagesComponent;
|
40
|
|
41
|
@Input() showSaveResetButtons: boolean = true;
|
42
|
|
43
|
constructor (private _mailPrefsService: MailPrefsService, private route: ActivatedRoute, private _router:Router, private location: Location) {
|
44
|
this.errorCodes = new ErrorCodes();
|
45
|
this.errorMessages = new ErrorMessagesComponent();
|
46
|
this.status = this.errorCodes.LOADING;
|
47
|
}
|
48
|
|
49
|
ngOnInit() {
|
50
|
this.properties = properties;
|
51
|
this.subscriptions.push(this.route.queryParams.subscribe(params => {
|
52
|
this.hidden = true;
|
53
|
|
54
|
this.communityId = ConnectHelper.getCommunityFromDomain(this.properties.domain);
|
55
|
if(!this.communityId) {
|
56
|
this.communityId = params['communityId'];
|
57
|
}
|
58
|
this.getEmailPreferences();
|
59
|
}));
|
60
|
|
61
|
|
62
|
}
|
63
|
|
64
|
getEmailPreferences() {
|
65
|
if(!Session.isLoggedIn()){
|
66
|
//this.userValidMessage = "User session has expired. Please login again.";
|
67
|
if(this.showSaveResetButtons) {
|
68
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
69
|
}
|
70
|
} else {
|
71
|
this.status = this.errorCodes.LOADING;
|
72
|
this.savedMessage = "";
|
73
|
|
74
|
if(this.communityId && this.communityId != "openaire") {
|
75
|
this.preferencesFor = "community";
|
76
|
this.subscriptions.push(this._mailPrefsService.getUserEmailPreferencesForCommunity(this.communityId, this.properties.claimsAPIURL).subscribe(
|
77
|
data => {
|
78
|
if(data.code == "204") {
|
79
|
this.status = this.errorCodes.NONE;
|
80
|
} else {
|
81
|
this.initialNotifications = data.data;
|
82
|
this.notifications = JSON.parse(JSON.stringify( this.initialNotifications ));
|
83
|
|
84
|
this.status = this.errorCodes.DONE;
|
85
|
this.hidden = false;
|
86
|
}
|
87
|
},
|
88
|
err => {
|
89
|
this.hidden = false;
|
90
|
this.handleErrors(err);
|
91
|
this.handleError("Error getting user email preferences for community with id: "+this.communityId, err);
|
92
|
}
|
93
|
));
|
94
|
} else {
|
95
|
this.preferencesFor = "project";
|
96
|
this.subscriptions.push(this._mailPrefsService.getUserEmailPreferencesForOpenaire(this.properties.claimsAPIURL).subscribe(
|
97
|
data => {
|
98
|
|
99
|
if(data.code == "204") {
|
100
|
this.status = this.errorCodes.NONE;
|
101
|
} else {
|
102
|
|
103
|
this.initialNotifications = data.data;
|
104
|
this.notifications = JSON.parse(JSON.stringify( this.initialNotifications ));
|
105
|
//this.notifications = this.initialNotifications.map(x => Object.assign({}, x));
|
106
|
//this.notifications = this.initialNotifications;
|
107
|
|
108
|
this.status = this.errorCodes.DONE;
|
109
|
}
|
110
|
},
|
111
|
err => {
|
112
|
//console.info(err);
|
113
|
this.handleErrors(err);
|
114
|
this.handleError("Error getting user email preferences for openaire", err);
|
115
|
}
|
116
|
));
|
117
|
}
|
118
|
}
|
119
|
}
|
120
|
|
121
|
changeNotify(notification: any, checked: boolean, index: number) {
|
122
|
if(!Session.isLoggedIn()){
|
123
|
//this.userValidMessage = "User session has expired. Please login again.";
|
124
|
if(this.showSaveResetButtons) {
|
125
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
126
|
}
|
127
|
} else {
|
128
|
this.savedMessage = "";
|
129
|
this.status = this.errorCodes.DONE;
|
130
|
notification.notify = checked;
|
131
|
this.prefsChanged[index] = true;
|
132
|
}
|
133
|
}
|
134
|
|
135
|
changeFrequency(index: number) {
|
136
|
if(!Session.isLoggedIn()){
|
137
|
//this.userValidMessage = "User session has expired. Please login again.";
|
138
|
if(this.showSaveResetButtons) {
|
139
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
140
|
}
|
141
|
} else {
|
142
|
this.savedMessage = "";
|
143
|
this.status = this.errorCodes.DONE;
|
144
|
if(this.initialNotifications[index].frequency != this.notifications[index].frequency) {
|
145
|
this.prefsChanged[index] = true;
|
146
|
}
|
147
|
}
|
148
|
}
|
149
|
|
150
|
saveNotification(index: number) {
|
151
|
if(this.notifications.length > 0 && this.initialNotifications.length > 0) {
|
152
|
if(!Session.isLoggedIn()){
|
153
|
//this.userValidMessage = "User session has expired. Please login again.";
|
154
|
if(this.showSaveResetButtons) {
|
155
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
156
|
}
|
157
|
} else {
|
158
|
if(JSON.stringify(this.notifications[index]) != JSON.stringify(this.initialNotifications[index])) {
|
159
|
|
160
|
this.status = this.errorCodes.LOADING;
|
161
|
this.savedMessage = "";
|
162
|
|
163
|
this.subscriptions.push(this._mailPrefsService.saveUserEmailPreferences(this.notifications[index], this.properties.claimsAPIURL).subscribe(
|
164
|
data => {
|
165
|
this.initialNotifications[index] = JSON.parse(JSON.stringify( this.notifications[index] ));
|
166
|
|
167
|
this.status = this.errorCodes.DONE;
|
168
|
|
169
|
/*UIkit.notification({
|
170
|
message : '<strong>Your email preferences for '+this.notifications[index].openaireName+' have been successfully changed<strong>',
|
171
|
status : 'success',
|
172
|
timeout : 3000,
|
173
|
pos : 'top-center'
|
174
|
});*/
|
175
|
this.savedMessage = "Notification settings for claims saved!";
|
176
|
},
|
177
|
err => {
|
178
|
//console.log(err);
|
179
|
this.handleError("Error saving user email preferences: "+JSON.stringify(this.notifications[index]), err);
|
180
|
this.status = this.errorCodes.NOT_SAVED;
|
181
|
}
|
182
|
));
|
183
|
}
|
184
|
else {
|
185
|
/*UIkit.notification({
|
186
|
message : '<strong>No changes selected for '+this.notifications[index].openaireName+' email preferences<strong>',
|
187
|
status : 'primary',
|
188
|
timeout : 3000,
|
189
|
pos : 'top-center'
|
190
|
});*/
|
191
|
this.savedMessage = "Notification settings for claims saved!";
|
192
|
}
|
193
|
}
|
194
|
}
|
195
|
}
|
196
|
|
197
|
restoreNotification(index: number) {
|
198
|
if(!Session.isLoggedIn()){
|
199
|
//this.userValidMessage = "User session has expired. Please login again.";
|
200
|
if(this.showSaveResetButtons) {
|
201
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
202
|
}
|
203
|
} else {
|
204
|
if(this.notifications.length > 0 && this.initialNotifications.length > 0) {
|
205
|
this.status = this.errorCodes.LOADING;
|
206
|
this.savedMessage = "";
|
207
|
this.notifications[index] = JSON.parse(JSON.stringify( this.initialNotifications[index] ));
|
208
|
this.status = this.errorCodes.DONE;
|
209
|
this.prefsChanged[index] = false;
|
210
|
}
|
211
|
}
|
212
|
}
|
213
|
/*
|
214
|
prefsChanged(index: number) : boolean {
|
215
|
if(this.notifications.length > 0 && this.initialNotifications.length > 0) {
|
216
|
if(JSON.stringify(this.notifications[index]) != JSON.stringify(this.initialNotifications[index])) {
|
217
|
return true;
|
218
|
}
|
219
|
}
|
220
|
return false;
|
221
|
}
|
222
|
*/
|
223
|
ngOnDestroy() {
|
224
|
this.subscriptions.forEach(subscription => {
|
225
|
if (subscription instanceof Subscriber) {
|
226
|
subscription.unsubscribe();
|
227
|
}
|
228
|
});
|
229
|
}
|
230
|
|
231
|
|
232
|
handleErrors(err){
|
233
|
//this.showErrorMessage = true;
|
234
|
//try{
|
235
|
var code = "";
|
236
|
if(!err.status) {
|
237
|
var error = err.json();
|
238
|
code = error.code;
|
239
|
} else {
|
240
|
code = err.status;
|
241
|
}
|
242
|
this.status = this.errorMessages.getErrorCode(code);
|
243
|
}
|
244
|
|
245
|
private handleError(message: string, error) {
|
246
|
console.error("User mail notification preferences Page (for claims): "+message, error);
|
247
|
}
|
248
|
|
249
|
|
250
|
//}catch (e) {
|
251
|
//console.log("Couldn't parse answer as json")
|
252
|
//this.showErrorMessage = true;
|
253
|
//}
|
254
|
}
|