1
|
import {Component, OnInit, Input} from '@angular/core';
|
2
|
import {FormGroup, FormBuilder} from '@angular/forms';
|
3
|
import {ActivatedRoute, Router} from '@angular/router';
|
4
|
import {CommonModule} from "@angular/common";
|
5
|
|
6
|
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
7
|
|
8
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
9
|
|
10
|
import {ManageUserNotificationsService} from './manage-user-notifications.service';
|
11
|
|
12
|
import {UserNotificationsRights} from './userNotificationsRights';
|
13
|
|
14
|
@Component({
|
15
|
selector: 'manage-user-notifications',
|
16
|
templateUrl: './manage-user-notifications.component.html',
|
17
|
})
|
18
|
|
19
|
export class ManageUserNotificationsComponent implements OnInit {
|
20
|
|
21
|
@Input('group')
|
22
|
myForm: FormGroup;
|
23
|
|
24
|
public properties: EnvProperties = null;
|
25
|
public communityId = null;
|
26
|
public userNotifications = null;
|
27
|
public userEmail = null;
|
28
|
|
29
|
public showLoading: boolean = true;
|
30
|
public errorMessage: string = '';
|
31
|
public updateErrorMessage: string = '';
|
32
|
|
33
|
public successfulSaveMessage: string = '';
|
34
|
public successfulResetMessage: string = '';
|
35
|
|
36
|
public hasChanged: boolean = false;
|
37
|
|
38
|
constructor (private route: ActivatedRoute, private _router: Router, public _fb: FormBuilder,
|
39
|
private _manageUserNotificationsService: ManageUserNotificationsService) {
|
40
|
}
|
41
|
|
42
|
ngOnInit() {
|
43
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
44
|
this.properties = data.envSpecific;
|
45
|
this.route.queryParams.subscribe(
|
46
|
communityId => {
|
47
|
this.communityId = communityId['communityId'];
|
48
|
if (this.communityId != null && this.communityId != '') {
|
49
|
this.showLoading = true;
|
50
|
this.updateErrorMessage = "";
|
51
|
this.errorMessage = "";
|
52
|
|
53
|
if (Session.getUser()) {
|
54
|
this.userEmail = Session.getUserEmail();
|
55
|
|
56
|
this._manageUserNotificationsService.getUserNotifications(this.properties.adminToolsAPIURL + "community/" + this.communityId + "/notifications", this.userEmail).subscribe(
|
57
|
userNotifications => {
|
58
|
this.userNotifications = userNotifications;
|
59
|
|
60
|
if (this.userNotifications['notifyForNewManagers'] == null || this.userNotifications['notifyForNewSubscribers'] == null) {
|
61
|
this.userNotifications = this.initiateUserNotifications();
|
62
|
}
|
63
|
|
64
|
//TODO remove after final testing
|
65
|
console.log("Before: ", userNotifications);
|
66
|
console.log("After: ", this.userNotifications);
|
67
|
this.showLoading = false;
|
68
|
},
|
69
|
error => {
|
70
|
console.log(error.status);
|
71
|
if (error.status == '404') {
|
72
|
this.userNotifications = this.initiateUserNotifications();
|
73
|
console.log(this.userNotifications);
|
74
|
this.showLoading = false;
|
75
|
} else {
|
76
|
this.handleError('System error retrieving user notifications', error)
|
77
|
}
|
78
|
}
|
79
|
);
|
80
|
}
|
81
|
}
|
82
|
}
|
83
|
);
|
84
|
});
|
85
|
}
|
86
|
|
87
|
public initiateUserNotifications() : UserNotificationsRights {
|
88
|
var notificationRights: UserNotificationsRights = new UserNotificationsRights();
|
89
|
|
90
|
notificationRights['notifyForNewManagers'] = true;
|
91
|
notificationRights['notifyForNewSubscribers'] = true;
|
92
|
notificationRights['managerEmail'] = this.userEmail;
|
93
|
|
94
|
return notificationRights;
|
95
|
}
|
96
|
|
97
|
public updateUserNotifications() {
|
98
|
if (this.communityId != null && this.communityId != '') {
|
99
|
this.showLoading = true;
|
100
|
var userNotifications = this.parseUpdatedUserNotifications();
|
101
|
console.log(userNotifications);
|
102
|
|
103
|
this._manageUserNotificationsService.updateUserNotifications(this.properties.adminToolsAPIURL + "community/" + this.communityId + "/notifications", userNotifications).subscribe(
|
104
|
userNotifications => {
|
105
|
this.handleSuccessfulSave('Notification settings saved!')
|
106
|
},
|
107
|
error => this.handleUpdateError('System error updating user notifications', error)
|
108
|
);
|
109
|
}
|
110
|
this.resetChange();
|
111
|
}
|
112
|
|
113
|
private parseUpdatedUserNotifications() : {} {
|
114
|
var userNotifications = {};
|
115
|
|
116
|
userNotifications["notifyForNewManagers"] = this.userNotifications.notifyForNewManagers;
|
117
|
userNotifications["notifyForNewSubscribers"] = this.userNotifications.notifyForNewSubscribers;
|
118
|
|
119
|
if (this.userNotifications.managerEmail) {
|
120
|
userNotifications["managerEmail"] = this.userNotifications.managerEmail;
|
121
|
} else {
|
122
|
if (Session.getUser()) {
|
123
|
userNotifications["managerEmail"] = Session.getUserEmail();
|
124
|
}
|
125
|
}
|
126
|
return userNotifications;
|
127
|
}
|
128
|
|
129
|
public resetForm(communityId:string) {
|
130
|
if (communityId != null && communityId != '') {
|
131
|
this.showLoading = true;
|
132
|
this.updateErrorMessage = "";
|
133
|
this.errorMessage = "";
|
134
|
|
135
|
this._manageUserNotificationsService.getUserNotifications(this.properties.adminToolsAPIURL + "community/" + this.communityId + "/notifications", this.userEmail).subscribe(
|
136
|
userNotifications => {
|
137
|
this.userNotifications = userNotifications;
|
138
|
this.showLoading = false;
|
139
|
console.log(userNotifications);
|
140
|
},
|
141
|
error => this.handleError('System error retrieving user notifications', error)
|
142
|
);
|
143
|
}
|
144
|
this.resetChange();
|
145
|
}
|
146
|
|
147
|
public changeValueForNewManagers(notifyForManagers : any) {
|
148
|
this.userNotifications.notifyForNewManagers = !notifyForManagers;
|
149
|
this.change();
|
150
|
}
|
151
|
|
152
|
public changeValueForNewSubscribers(notifyForSubscribers : any) {
|
153
|
this.userNotifications.notifyForNewSubscribers = !notifyForSubscribers;
|
154
|
this.change();
|
155
|
}
|
156
|
|
157
|
private change() {
|
158
|
this.hasChanged = true;
|
159
|
this.successfulSaveMessage = '';
|
160
|
this.successfulResetMessage = '';
|
161
|
// TODO remove after testing
|
162
|
console.log('I have changed: I AM TRUE');
|
163
|
}
|
164
|
|
165
|
private resetChange() {
|
166
|
this.hasChanged = false;
|
167
|
// TODO remove after testing
|
168
|
console.log('I have changed: I AM FALSE');
|
169
|
}
|
170
|
|
171
|
handleUpdateError(message: string, error) {
|
172
|
this.updateErrorMessage = message;
|
173
|
console.log('Server responded: ' +error);
|
174
|
|
175
|
this.showLoading = false;
|
176
|
}
|
177
|
|
178
|
handleError(message: string, error) {
|
179
|
this.errorMessage = message;
|
180
|
console.log('Server responded: ' + error);
|
181
|
|
182
|
this.showLoading = false;
|
183
|
}
|
184
|
|
185
|
handleSuccessfulSave(message) {
|
186
|
this.showLoading = false;
|
187
|
this.successfulSaveMessage = message;
|
188
|
}
|
189
|
|
190
|
handleSuccessfulReset(message) {
|
191
|
this.showLoading = false;
|
192
|
this.successfulResetMessage = message;
|
193
|
}
|
194
|
}
|