Project

General

Profile

1
import {Component, OnInit, Input, ViewChild} 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
import {MailPrefsComponent} from '../../openaireLibrary/connect/userEmailPreferences/mailPrefs.component';
15
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
16

    
17
@Component({
18
    selector: 'manage-user-notifications',
19
    templateUrl: './manage-user-notifications.component.html',
20
})
21

    
22
export class ManageUserNotificationsComponent implements OnInit {
23

    
24
    @Input('group')
25
    myForm: FormGroup;
26

    
27
    public properties: EnvProperties = null;
28
    public communityId = null;
29
    public userNotifications = null;
30
    public initialUserNotifications = null;
31
    public userEmail = null;
32

    
33
    public showLoading: boolean = true;
34
    public errorMessage: string = '';
35
    public updateErrorMessage: string = '';
36

    
37
    public successfulSaveMessage: string = '';
38
    public successfulResetMessage: string = '';
39

    
40
    public hasChanged: boolean = false;
41

    
42
    @ViewChild (MailPrefsComponent) mailPrefs : MailPrefsComponent;
43

    
44
    constructor (private route: ActivatedRoute, private _router: Router, public _fb: FormBuilder,
45
                 private _manageUserNotificationsService: ManageUserNotificationsService) {
46
    }
47

    
48
    ngOnInit() {
49
        this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
50
            this.properties = data.envSpecific;
51
            this.route.queryParams.subscribe(
52
                communityId => {
53
                    this.communityId = communityId['communityId'];
54
                    if (this.communityId != null && this.communityId != '') {
55
                        this.showLoading = true;
56
                        this.updateErrorMessage = "";
57
                        this.errorMessage = "";
58
                        this.successfulSaveMessage = "";
59

    
60
                        if (Session.getUser()) {
61
                            this.userEmail = Session.getUserEmail();
62

    
63
                            this._manageUserNotificationsService.getUserNotifications(this.properties.adminToolsAPIURL + "community/" + this.communityId + "/notifications", this.userEmail).subscribe(
64
                                userNotifications => {
65
                                    this.initialUserNotifications = userNotifications;
66

    
67
                                    if (this.initialUserNotifications['notifyForNewManagers'] == null || this.initialUserNotifications['notifyForNewSubscribers'] == null) {
68
                                        this.initialUserNotifications = this.initiateUserNotifications();
69
                                    }
70

    
71
                                    this.userNotifications = JSON.parse(JSON.stringify( this.initialUserNotifications ));
72

    
73
                                    //TODO remove after final testing
74
                                    console.log("Before: ", userNotifications);
75
                                    console.log("After: ", this.initialUserNotifications);
76
                                    this.showLoading = false;
77
                                },
78
                                error => {
79
                                    console.log(error.status);
80
                                    if (error.status == '404') {
81
                                        this.initialUserNotifications = this.initiateUserNotifications();
82
                                        console.log(this.initialUserNotifications);
83
                                        this.userNotifications = JSON.parse(JSON.stringify( this.initialUserNotifications ));
84
                                    } else {
85
                                        this.handleError('System error retrieving user notifications', error)
86
                                    }
87
                                    this.showLoading = false;
88
                                }
89
                            );
90
                        }
91
                    }
92
                }
93
            );
94
        });
95
    }
96

    
97
    public initiateUserNotifications() : UserNotificationsRights {
98
        var notificationRights: UserNotificationsRights = new UserNotificationsRights();
99

    
100
        notificationRights['notifyForNewManagers'] = true;
101
        notificationRights['notifyForNewSubscribers'] = true;
102
        notificationRights['managerEmail'] = this.userEmail;
103

    
104
        return notificationRights;
105
    }
106

    
107
    public updateUserNotifications() {
108
        if(!Session.isLoggedIn()){
109
          console.info(this._router.url);
110
          this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
111
        } else {
112
          if (this.communityId != null && this.communityId != '') {
113
              this.mailPrefs.saveNotification(0);
114

    
115
              this.successfulSaveMessage = "";
116
              this.showLoading = true;
117
              var userNotifications = this.parseUpdatedUserNotifications();
118
              console.log(userNotifications);
119

    
120
              this._manageUserNotificationsService.updateUserNotifications(this.properties.adminToolsAPIURL + "community/" + this.communityId + "/notifications", userNotifications).subscribe(
121
                  userNotifications => {
122
                      this.initialUserNotifications = JSON.parse(JSON.stringify( this.userNotifications ));
123
                      this.handleSuccessfulSave('Notification settings saved!')
124
                  },
125
                  error => this.handleUpdateError('System error updating user notifications', error)
126
              );
127
          }
128
          this.resetChange();
129
        }
130
    }
131

    
132
    private parseUpdatedUserNotifications() : {} {
133
        var userNotifications = {};
134

    
135
        userNotifications["notifyForNewManagers"] = this.userNotifications.notifyForNewManagers;
136
        userNotifications["notifyForNewSubscribers"] = this.userNotifications.notifyForNewSubscribers;
137

    
138
        if (this.userNotifications.managerEmail) {
139
            userNotifications["managerEmail"] = this.userNotifications.managerEmail;
140
        } else {
141
            if (Session.getUser()) {
142
                userNotifications["managerEmail"] = Session.getUserEmail();
143
            }
144
        }
145
        return userNotifications;
146
    }
147

    
148
    public resetForm(communityId:string) {
149
      /*
150
        if (communityId != null && communityId != '') {
151
            this.showLoading = true;
152
            this.updateErrorMessage = "";
153
            this.errorMessage = "";
154

    
155
            this.mailPrefs.restoreNotification(0);
156

    
157
            this._manageUserNotificationsService.getUserNotifications(this.properties.adminToolsAPIURL + "community/" + this.communityId + "/notifications", this.userEmail).subscribe(
158
                userNotifications => {
159
                    this.userNotifications = userNotifications;
160
                    this.showLoading = false;
161
                    console.log(userNotifications);
162
                },
163
                error => this.handleError('System error retrieving user notifications', error)
164
            );
165
        }
166

    
167
        */
168
        if(!Session.isLoggedIn()){
169
          console.info(this._router.url);
170
          this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
171
        } else {
172
          this.mailPrefs.restoreNotification(0);
173

    
174
          if(this.userNotifications && this.initialUserNotifications) {
175
            this.successfulSaveMessage = "";
176
            this.showLoading = true;
177
            this.userNotifications = JSON.parse(JSON.stringify( this.initialUserNotifications ));
178
            this.showLoading = false;
179
          }
180

    
181

    
182
          this.resetChange();
183
        }
184
    }
185

    
186
    public changeValueForNewManagers(notifyForManagers : any) {
187
      if(!Session.isLoggedIn()){
188
        console.info(this._router.url);
189
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
190
      } else {
191
        this.userNotifications.notifyForNewManagers = !notifyForManagers;
192
        this.change();
193
      }
194
    }
195

    
196
    public changeValueForNewSubscribers(notifyForSubscribers : any) {
197
      if(!Session.isLoggedIn()){
198
        console.info(this._router.url);
199
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
200
      } else {
201
        this.userNotifications.notifyForNewSubscribers = !notifyForSubscribers;
202
        this.change();
203
      }
204
    }
205

    
206
    private change() {
207
        this.hasChanged = true;
208
        this.successfulSaveMessage = '';
209
        this.successfulResetMessage = '';
210
        // TODO remove after testing
211
        console.log('I have changed: I AM TRUE');
212
    }
213

    
214
    private resetChange() {
215
        this.hasChanged = false;
216
        // TODO remove after testing
217
        console.log('I have changed: I AM FALSE');
218
    }
219

    
220
    public mailPrefsChanged(): boolean {
221
      if(!Session.isLoggedIn()){
222
        console.info(this._router.url);
223
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
224
      } else {
225
        return this.mailPrefs.prefsChanged["0"];//(0);
226
      }
227
    }
228

    
229
    handleUpdateError(message: string, error) {
230
        this.updateErrorMessage = message;
231
        console.log('Server responded: ' +error);
232

    
233
        this.showLoading = false;
234
    }
235

    
236
    handleError(message: string, error) {
237
        this.errorMessage = message;
238
        console.log('Server responded: ' + error);
239

    
240
        this.showLoading = false;
241
    }
242

    
243
    handleSuccessfulSave(message) {
244
        this.showLoading = false;
245
        this.successfulSaveMessage = message;
246
    }
247

    
248
    handleSuccessfulReset(message) {
249
        this.showLoading = false;
250
        this.successfulResetMessage = message;
251
    }
252
}
(2-2/5)