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
|
|
13
|
declare var UIkit: any;
|
14
|
|
15
|
@Component({
|
16
|
selector: 'mailPrefs',
|
17
|
templateUrl: 'mailPrefs.component.html',
|
18
|
providers:[MailPrefsService]
|
19
|
|
20
|
})
|
21
|
export class MailPrefsComponent {
|
22
|
properties:EnvProperties;
|
23
|
sub: any;
|
24
|
public communityId: string;
|
25
|
public preferencesFor: string = "community";
|
26
|
public status: number;
|
27
|
|
28
|
public notifications = [];
|
29
|
public initialNotifications = [];
|
30
|
|
31
|
public showErrorMessage:boolean = false;
|
32
|
//public showForbiddenMessage:boolean = false;
|
33
|
public userValidMessage:string = "";
|
34
|
|
35
|
public fetchId:string;
|
36
|
|
37
|
private errorCodes: ErrorCodes;
|
38
|
|
39
|
constructor (private _mailPrefsService: MailPrefsService, private route: ActivatedRoute, private _router:Router, private location: Location) {
|
40
|
this.errorCodes = new ErrorCodes();
|
41
|
this.status = this.errorCodes.LOADING;
|
42
|
}
|
43
|
|
44
|
ngOnInit() {
|
45
|
this.route.data
|
46
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
47
|
this.properties = data.envSpecific;
|
48
|
});
|
49
|
|
50
|
this.sub = this.route.queryParams.subscribe(params => {
|
51
|
this.communityId = params['communityId'];
|
52
|
|
53
|
if(!this.communityId){
|
54
|
this.communityId = ConnectHelper.getCommunityFromDomain(document.location.hostname);
|
55
|
}
|
56
|
|
57
|
this.fetchId = Session.getUserEmail();
|
58
|
console.info("email: "+this.fetchId);
|
59
|
console.info("communityId: " + this.communityId);
|
60
|
|
61
|
this.getEmailPreferences();
|
62
|
|
63
|
});
|
64
|
}
|
65
|
|
66
|
getEmailPreferences() {
|
67
|
if(!Session.isLoggedIn()){
|
68
|
this.userValidMessage = "User session has expired. Please login again.";
|
69
|
}else{
|
70
|
this.status = this.errorCodes.LOADING;
|
71
|
|
72
|
if(this.communityId && this.communityId != "openaire") {
|
73
|
this.preferencesFor = "community";
|
74
|
this._mailPrefsService.getUserEmailPreferencesForCommunity(this.communityId, this.properties.claimsAPIURL).subscribe(
|
75
|
data => {
|
76
|
if(data.code == "204") {
|
77
|
this.status = this.errorCodes.NONE;
|
78
|
} else {
|
79
|
this.initialNotifications = data.data;
|
80
|
this.notifications = this.initialNotifications;
|
81
|
|
82
|
this.status = this.errorCodes.DONE;
|
83
|
}
|
84
|
},
|
85
|
err => {
|
86
|
this.handleErrors(err);
|
87
|
}
|
88
|
);
|
89
|
} else {
|
90
|
this.preferencesFor = "project";
|
91
|
this._mailPrefsService.getUserEmailPreferencesForOpenaire(this.properties.claimsAPIURL).subscribe(
|
92
|
data => {
|
93
|
console.info("email prefs returned");
|
94
|
|
95
|
if(data.code == "204") {
|
96
|
this.status = this.errorCodes.NONE;
|
97
|
} else {
|
98
|
console.info(data);
|
99
|
|
100
|
this.initialNotifications = data.data;
|
101
|
this.notifications = JSON.parse(JSON.stringify( this.initialNotifications ));
|
102
|
//this.notifications = this.initialNotifications.map(x => Object.assign({}, x));
|
103
|
//this.notifications = this.initialNotifications;
|
104
|
|
105
|
this.status = this.errorCodes.DONE;
|
106
|
}
|
107
|
},
|
108
|
err => {
|
109
|
//console.info(err);
|
110
|
this.handleErrors(err);
|
111
|
}
|
112
|
);
|
113
|
}
|
114
|
}
|
115
|
}
|
116
|
|
117
|
changeNotify(notification: any, checked: boolean) {
|
118
|
notification.notify = checked;
|
119
|
}
|
120
|
|
121
|
saveNotification(notifications: any, index: number) {
|
122
|
if(JSON.stringify(this.notifications[index]) != JSON.stringify(this.initialNotifications[index])) {
|
123
|
if(!Session.isLoggedIn()){
|
124
|
this.userValidMessage = "User session has expired. Please login again.";
|
125
|
|
126
|
}else{
|
127
|
this.status = this.errorCodes.LOADING;
|
128
|
|
129
|
console.info("Send notification to db: ", this.notifications[index]);
|
130
|
|
131
|
this._mailPrefsService.saveUserEmailPreferences(this.notifications[index], this.properties.claimsAPIURL).subscribe(
|
132
|
data => {
|
133
|
console.info("Notification saved successfully");
|
134
|
this.initialNotifications[index] = JSON.parse(JSON.stringify( this.notifications[index] ));
|
135
|
|
136
|
this.status = this.errorCodes.DONE;
|
137
|
|
138
|
UIkit.notification({
|
139
|
message : '<strong>Your email preferences for '+this.notifications[index].openaireName+' have been successfully changed<strong>',
|
140
|
status : 'success',
|
141
|
timeout : 3000,
|
142
|
pos : 'top-center'
|
143
|
});
|
144
|
},
|
145
|
err => {
|
146
|
console.log(err);
|
147
|
this.status = this.errorCodes.NOT_SAVED;
|
148
|
}
|
149
|
);
|
150
|
}
|
151
|
} else {
|
152
|
console.info("Notification not changed: ", this.notifications[index]);
|
153
|
UIkit.notification({
|
154
|
message : '<strong>No changes selected for '+this.notifications[index].openaireName+' email preferences<strong>',
|
155
|
status : 'primary',
|
156
|
timeout : 3000,
|
157
|
pos : 'top-center'
|
158
|
});
|
159
|
}
|
160
|
}
|
161
|
|
162
|
restoreNotification(notifications: any, index: number) {
|
163
|
this.notifications[index] = JSON.parse(JSON.stringify( this.initialNotifications[index] ));
|
164
|
}
|
165
|
|
166
|
ngOnDestroy() {
|
167
|
if(this.sub) {
|
168
|
this.sub.unsubscribe();
|
169
|
}
|
170
|
}
|
171
|
|
172
|
handleErrors(err){
|
173
|
//this.showErrorMessage = true;
|
174
|
//try{
|
175
|
var error = err.json()
|
176
|
//var code = error.code;
|
177
|
|
178
|
console.info(err);
|
179
|
var code = error.code;
|
180
|
console.info(code);
|
181
|
if(code == "403") {
|
182
|
this.status = this.errorCodes.FORBIDDEN;
|
183
|
}
|
184
|
else if(code == "204") {
|
185
|
this.status = this.errorCodes.NONE;
|
186
|
} else if(code == "404") {
|
187
|
this.status = this.errorCodes.NOT_FOUND;
|
188
|
} else if(code == "500") {
|
189
|
this.status = this.errorCodes.ERROR;
|
190
|
} else {
|
191
|
this.status = this.errorCodes.NOT_AVAILABLE;
|
192
|
}
|
193
|
|
194
|
|
195
|
//}catch (e) {
|
196
|
//console.log("Couldn't parse answer as json")
|
197
|
//this.showErrorMessage = true;
|
198
|
//}
|
199
|
}
|
200
|
}
|