1
|
import {Component, ElementRef, Input, OnInit} from '@angular/core';
|
2
|
import {FormBuilder, FormGroup} from '@angular/forms';
|
3
|
import {ActivatedRoute, Router} from '@angular/router';
|
4
|
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
5
|
import {Session, User} from '../../openaireLibrary/login/utils/helper.class';
|
6
|
import {ManageUserNotificationsService} from './manage-user-notifications.service';
|
7
|
import {UserNotificationsRights} from './userNotificationsRights';
|
8
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
9
|
import {HelperFunctions} from '../../openaireLibrary/utils/HelperFunctions.class';
|
10
|
import {UserManagementService} from '../../openaireLibrary/services/user-management.service';
|
11
|
import {Title} from '@angular/platform-browser';
|
12
|
import {CommunityService} from '../../openaireLibrary/connect/community/community.service';
|
13
|
import {properties} from '../../../environments/environment';
|
14
|
import { Subscriber} from 'rxjs';
|
15
|
import {MailPrefsService} from '../../openaireLibrary/connect/userEmailPreferences/mailPrefs.service';
|
16
|
|
17
|
declare var UIkit;
|
18
|
@Component({
|
19
|
selector: 'manage-user-notifications',
|
20
|
templateUrl: './manage-user-notifications.component.html',
|
21
|
})
|
22
|
|
23
|
export class ManageUserNotificationsComponent implements OnInit {
|
24
|
|
25
|
@Input('group')
|
26
|
myForm: FormGroup;
|
27
|
|
28
|
public properties: EnvProperties = null;
|
29
|
public communityId;
|
30
|
public userNotifications:UserNotificationsRights = null;
|
31
|
public initialUserNotifications:UserNotificationsRights = null;
|
32
|
public notifications = null;
|
33
|
public initialNotifications = [];
|
34
|
public userEmail = null;
|
35
|
|
36
|
public showLoading = true;
|
37
|
public hasChanged = false;
|
38
|
public user: User;
|
39
|
private subscriptions = [];
|
40
|
|
41
|
frequencyOptions = [{label:"Daily", value: 24}, {label:"Every two days", value: 48}, {label:"Weekly", value: 168}]
|
42
|
constructor(private route: ActivatedRoute, private _router: Router, public _fb: FormBuilder,
|
43
|
private title: Title,
|
44
|
private _manageUserNotificationsService: ManageUserNotificationsService,
|
45
|
private element: ElementRef, private userManagementService: UserManagementService, private communityService: CommunityService, private _mailPrefsService: MailPrefsService,) {
|
46
|
}
|
47
|
|
48
|
ngOnInit() {
|
49
|
|
50
|
this.properties = properties;
|
51
|
HelperFunctions.scroll();
|
52
|
this.subscriptions.push(this.communityService.getCommunityAsObservable().subscribe(community => {
|
53
|
if(community) {
|
54
|
this.communityId =community.communityId;
|
55
|
this.title.setTitle( this.communityId.toUpperCase() + ' | User Notifications');
|
56
|
this.showLoading = true;
|
57
|
this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
|
58
|
this.user = user;
|
59
|
if (this.user) {
|
60
|
this.userEmail = this.user.email;
|
61
|
this.subscriptions.push(this._manageUserNotificationsService.getUserNotifications(this.properties, this.communityId).subscribe(
|
62
|
userNotifications => {
|
63
|
this.initialUserNotifications = userNotifications;
|
64
|
if (this.initialUserNotifications['notifyForNewManagers'] == null ||
|
65
|
this.initialUserNotifications['notifyForNewSubscribers'] == null) {
|
66
|
if(Session.isManager("community",this.communityId, this.user)) {
|
67
|
this.initialUserNotifications = new UserNotificationsRights(true, true, "");
|
68
|
}else{
|
69
|
this.initialUserNotifications = new UserNotificationsRights(false, false, "");
|
70
|
}
|
71
|
}
|
72
|
|
73
|
this.userNotifications = JSON.parse(JSON.stringify(this.initialUserNotifications));
|
74
|
this.getClaimsNotifications();
|
75
|
},
|
76
|
error => {
|
77
|
if (error.status === 404) {
|
78
|
if(Session.isManager("community",this.communityId, this.user)) {
|
79
|
this.initialUserNotifications = new UserNotificationsRights(true, true, "");
|
80
|
}else{
|
81
|
this.initialUserNotifications = new UserNotificationsRights(false, false, "");
|
82
|
}
|
83
|
this.userNotifications = JSON.parse(JSON.stringify(this.initialUserNotifications));
|
84
|
} else {
|
85
|
this.handleError('System error retrieving user notifications', error);
|
86
|
}
|
87
|
this.getClaimsNotifications();
|
88
|
}
|
89
|
));
|
90
|
}
|
91
|
}));
|
92
|
}
|
93
|
}));
|
94
|
}
|
95
|
getClaimsNotifications(){
|
96
|
this.subscriptions.push(this._mailPrefsService.getUserEmailPreferencesForCommunity(this.communityId, this.properties.claimsAPIURL).subscribe(
|
97
|
data => {
|
98
|
this.initialNotifications = data.data;
|
99
|
this.notifications = JSON.parse(JSON.stringify( this.initialNotifications ));
|
100
|
this.showLoading = false;
|
101
|
},
|
102
|
err => {
|
103
|
if(err.status === 404) {
|
104
|
if(Session.isManager("community",this.communityId, this.user)) {
|
105
|
this.initialNotifications = [{notify: true, frequency:24, openaireId: this.communityId}];
|
106
|
}else{
|
107
|
this.initialNotifications = [{notify: false, frequency:24, openaireId: this.communityId}];
|
108
|
}
|
109
|
this.notifications = JSON.parse(JSON.stringify( this.initialNotifications ));
|
110
|
}else{
|
111
|
this.handleError("Error getting user email preferences for community with id: "+this.communityId, err);
|
112
|
}
|
113
|
this.showLoading = false;
|
114
|
}
|
115
|
));
|
116
|
}
|
117
|
ngOnDestroy() {
|
118
|
this.subscriptions.forEach(value => {
|
119
|
if (value instanceof Subscriber) {
|
120
|
value.unsubscribe();
|
121
|
}
|
122
|
});
|
123
|
}
|
124
|
|
125
|
|
126
|
public updateUserNotifications() {
|
127
|
if (!Session.isLoggedIn()) {
|
128
|
this._router.navigate(['/user-info'],
|
129
|
{queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
130
|
} else {
|
131
|
if (this.communityId != null && this.communityId !== '') {
|
132
|
// this.mailPrefs.saveNotification(0);
|
133
|
|
134
|
// this.successfulSaveMessage = '';
|
135
|
this.showLoading = true;
|
136
|
const userNotifications = this.parseUpdatedUserNotifications();
|
137
|
this.subscriptions.push(this._manageUserNotificationsService.updateUserNotifications(this.properties, this.communityId, userNotifications).subscribe(
|
138
|
userNotifications => {
|
139
|
this.initialUserNotifications = JSON.parse(JSON.stringify(this.userNotifications));
|
140
|
this.handleSuccessfulSave('Notification settings saved!');
|
141
|
},
|
142
|
error => this.handleUpdateError('System error updating user notifications', error)
|
143
|
));
|
144
|
this.subscriptions.push(this._mailPrefsService.saveUserEmailPreferences(this.notifications[0], this.properties.claimsAPIURL).subscribe(
|
145
|
data => {
|
146
|
this.initialNotifications[0] = JSON.parse(JSON.stringify( this.notifications[0] ));
|
147
|
this.handleSuccessfulSave('Claims notification settings saved!');
|
148
|
},
|
149
|
err => {
|
150
|
//console.log(err);
|
151
|
this.handleError("Error saving user email preferences: "+JSON.stringify(this.notifications[0]), err);
|
152
|
}
|
153
|
));
|
154
|
}
|
155
|
this.resetChange();
|
156
|
}
|
157
|
}
|
158
|
|
159
|
private parseUpdatedUserNotifications(): {} {
|
160
|
const userNotifications = {};
|
161
|
|
162
|
userNotifications['notifyForNewManagers'] = this.userNotifications.notifyForNewManagers;
|
163
|
userNotifications['notifyForNewSubscribers'] = this.userNotifications.notifyForNewSubscribers;
|
164
|
|
165
|
if (this.userNotifications.managerEmail) {
|
166
|
userNotifications['managerEmail'] = this.userNotifications.managerEmail;
|
167
|
}
|
168
|
// else {
|
169
|
// if (this.user) {
|
170
|
// userNotifications['managerEmail'] = this.user.email;
|
171
|
// }
|
172
|
// }
|
173
|
return userNotifications;
|
174
|
}
|
175
|
|
176
|
public resetForm() {
|
177
|
if (!Session.isLoggedIn()) {
|
178
|
this._router.navigate(['/user-info'], {
|
179
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
180
|
});
|
181
|
} else {
|
182
|
// this.mailPrefs.restoreNotification(0);
|
183
|
this.notifications[0] = JSON.parse(JSON.stringify( this.initialNotifications[0] ));
|
184
|
|
185
|
if (this.userNotifications && this.initialUserNotifications) {
|
186
|
// this.successfulSaveMessage = '';
|
187
|
this.showLoading = true;
|
188
|
this.userNotifications = JSON.parse(JSON.stringify(this.initialUserNotifications));
|
189
|
this.showLoading = false;
|
190
|
}
|
191
|
this.resetChange();
|
192
|
}
|
193
|
}
|
194
|
|
195
|
public changeValueForNewManagers(notifyForManagers: any) {
|
196
|
if (!Session.isLoggedIn()) {
|
197
|
this._router.navigate(['/user-info'], {
|
198
|
queryParams: {' "errorCode"': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
199
|
});
|
200
|
} else {
|
201
|
this.userNotifications.notifyForNewManagers = !notifyForManagers;
|
202
|
this.change();
|
203
|
}
|
204
|
}
|
205
|
|
206
|
public changeValueForNewSubscribers(notifyForSubscribers: any) {
|
207
|
if (!Session.isLoggedIn()) {
|
208
|
this._router.navigate(['/user-info'], {
|
209
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
210
|
});
|
211
|
} else {
|
212
|
this.userNotifications.notifyForNewSubscribers = !notifyForSubscribers;
|
213
|
this.change();
|
214
|
}
|
215
|
}
|
216
|
|
217
|
private change() {
|
218
|
this.hasChanged = true;
|
219
|
// this.successfulSaveMessage = '';
|
220
|
// this.successfulResetMessage = '';
|
221
|
}
|
222
|
|
223
|
private resetChange() {
|
224
|
this.hasChanged = false;
|
225
|
}
|
226
|
|
227
|
handleUpdateError(message: string, error) {
|
228
|
// this.updateErrorMessage = message;
|
229
|
console.log('Server responded: ' + error);
|
230
|
|
231
|
this.showLoading = false;
|
232
|
}
|
233
|
|
234
|
handleError(message: string, error) {
|
235
|
// this.errorMessage = message;
|
236
|
console.log('Server responded: ' + error);
|
237
|
UIkit.notification(message, {
|
238
|
status: 'danger',
|
239
|
timeout: 6000,
|
240
|
pos: 'bottom-right'
|
241
|
});
|
242
|
this.showLoading = false;
|
243
|
}
|
244
|
|
245
|
handleSuccessfulSave(message) {
|
246
|
this.showLoading = false;
|
247
|
// this.successfulSaveMessage = message;
|
248
|
UIkit.notification(message, {
|
249
|
status: 'success',
|
250
|
timeout: 6000,
|
251
|
pos: 'bottom-right'
|
252
|
});
|
253
|
}
|
254
|
|
255
|
changeNotify(notification: any, checked: boolean, index: number) {
|
256
|
notification.notify = checked;
|
257
|
this.change();
|
258
|
}
|
259
|
|
260
|
changeFrequency() {
|
261
|
this.change();
|
262
|
}
|
263
|
|
264
|
|
265
|
}
|