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