Project

General

Profile

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 {ErrorMessagesComponent}    from '../../utils/errorMessages.component';
13
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
14

    
15
declare var UIkit: any;
16

    
17
@Component({
18
  selector: 'mailPrefs',
19
  templateUrl: 'mailPrefs.component.html',
20
  providers:[MailPrefsService]
21

    
22
})
23
export class MailPrefsComponent {
24
  properties:EnvProperties;
25
  sub: any;
26
  public communityId: string;
27
  public preferencesFor: string = "community";
28
  public status: number;
29

    
30
  public notifications = [];
31
  public initialNotifications = [];
32
  public prefsChanged = {};
33

    
34
  //public showForbiddenMessage:boolean = false;
35
  public userValidMessage:string = "";
36
  public savedMessage: string = "";
37

    
38
  public fetchId:string;
39

    
40
  private errorCodes: ErrorCodes;
41
  private errorMessages: ErrorMessagesComponent;
42

    
43
  @Input() showSaveResetButtons: boolean = true;
44

    
45
  constructor (private _mailPrefsService: MailPrefsService, private route: ActivatedRoute, private _router:Router, private location: Location) {
46
    this.errorCodes = new ErrorCodes();
47
    this.errorMessages = new ErrorMessagesComponent();
48
    this.status = this.errorCodes.LOADING;
49
  }
50

    
51
  ngOnInit() {
52
    this.route.data
53
      .subscribe((data: { envSpecific: EnvProperties }) => {
54
        this.properties = data.envSpecific;
55
        this.sub = this.route.queryParams.subscribe(params => {
56
          this.communityId = params['communityId'];
57

    
58
          if(!this.communityId){
59
            this.communityId  = ConnectHelper.getCommunityFromDomain(this.properties.domain);
60
          }
61

    
62
          this.fetchId = Session.getUserEmail();
63

    
64
          this.getEmailPreferences();
65

    
66
        });
67
    });
68

    
69
  }
70

    
71
  getEmailPreferences() {
72
    if(!Session.isLoggedIn()){
73
      //this.userValidMessage = "User session has expired. Please login again.";
74
      if(this.showSaveResetButtons) {
75
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
76
      }
77
    } else {
78
      this.status = this.errorCodes.LOADING;
79
      this.savedMessage = "";
80

    
81
      if(this.communityId && this.communityId != "openaire") {
82
        this.preferencesFor = "community";
83
        this._mailPrefsService.getUserEmailPreferencesForCommunity(this.communityId, this.properties.claimsAPIURL).subscribe(
84
          data => {
85
            if(data.code == "204") {
86
              this.status = this.errorCodes.NONE;
87
            } else {
88
              this.initialNotifications = data.data;
89
              this.notifications = JSON.parse(JSON.stringify( this.initialNotifications ));
90

    
91
              this.status = this.errorCodes.DONE;
92
            }
93
          },
94
          err => {
95
            this.handleErrors(err);
96
            this.handleError("Error getting user email preferences for community with id: "+this.communityId, err);
97
          }
98
        );
99
      } else {
100
        this.preferencesFor = "project";
101
        this._mailPrefsService.getUserEmailPreferencesForOpenaire(this.properties.claimsAPIURL).subscribe(
102
          data => {
103

    
104
            if(data.code == "204") {
105
              this.status = this.errorCodes.NONE;
106
            } else {
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
            this.handleError("Error getting user email preferences for openaire", err);
120
          }
121
        );
122
      }
123
    }
124
  }
125

    
126
  changeNotify(notification: any, checked: boolean, index: number) {
127
    if(!Session.isLoggedIn()){
128
      //this.userValidMessage = "User session has expired. Please login again.";
129
      if(this.showSaveResetButtons) {
130
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
131
      }
132
    } else {
133
      this.savedMessage = "";
134
      this.status = this.errorCodes.DONE;
135
      notification.notify = checked;
136
      this.prefsChanged[index] = true;
137
    }
138
  }
139

    
140
  changeFrequency(index: number) {
141
    if(!Session.isLoggedIn()){
142
      //this.userValidMessage = "User session has expired. Please login again.";
143
      if(this.showSaveResetButtons) {
144
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
145
      }
146
    } else {
147
      this.savedMessage = "";
148
      this.status = this.errorCodes.DONE;
149
      if(this.initialNotifications[index].frequency != this.notifications[index].frequency) {
150
        this.prefsChanged[index] = true;
151
      }
152
    }
153
  }
154

    
155
  saveNotification(index: number) {
156
    if(this.notifications.length > 0 && this.initialNotifications.length > 0) {
157
      if(!Session.isLoggedIn()){
158
          //this.userValidMessage = "User session has expired. Please login again.";
159
          if(this.showSaveResetButtons) {
160
            this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
161
          }
162
      } else {
163
        if(JSON.stringify(this.notifications[index]) != JSON.stringify(this.initialNotifications[index])) {
164

    
165
          this.status = this.errorCodes.LOADING;
166
          this.savedMessage = "";
167

    
168
          this._mailPrefsService.saveUserEmailPreferences(this.notifications[index], this.properties.claimsAPIURL).subscribe(
169
            data => {
170
              this.initialNotifications[index] = JSON.parse(JSON.stringify( this.notifications[index] ));
171

    
172
              this.status = this.errorCodes.DONE;
173

    
174
              /*UIkit.notification({
175
                  message : '<strong>Your email preferences for '+this.notifications[index].openaireName+' have been successfully changed<strong>',
176
                  status  : 'success',
177
                  timeout : 3000,
178
                  pos     : 'top-center'
179
              });*/
180
              this.savedMessage = "Notification settings for claims saved!";
181
            },
182
            err => {
183
              //console.log(err);
184
              this.handleError("Error saving user email preferences: "+JSON.stringify(this.notifications[index]), err);
185
              this.status = this.errorCodes.NOT_SAVED;
186
            }
187
          );
188
        }
189
        else {
190
          /*UIkit.notification({
191
              message : '<strong>No changes selected for '+this.notifications[index].openaireName+' email preferences<strong>',
192
              status  : 'primary',
193
              timeout : 3000,
194
              pos     : 'top-center'
195
          });*/
196
          this.savedMessage = "Notification settings for claims saved!";
197
        }
198
      }
199
    }
200
  }
201

    
202
  restoreNotification(index: number) {
203
    if(!Session.isLoggedIn()){
204
      //this.userValidMessage = "User session has expired. Please login again.";
205
      if(this.showSaveResetButtons) {
206
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
207
      }
208
    } else {
209
      if(this.notifications.length > 0 && this.initialNotifications.length > 0) {
210
        this.status = this.errorCodes.LOADING;
211
        this.savedMessage = "";
212
        this.notifications[index] = JSON.parse(JSON.stringify( this.initialNotifications[index] ));
213
        this.status = this.errorCodes.DONE;
214
        this.prefsChanged[index] = false;
215
      }
216
    }
217
  }
218
/*
219
  prefsChanged(index: number) : boolean {
220
    if(this.notifications.length > 0 && this.initialNotifications.length > 0) {
221
      if(JSON.stringify(this.notifications[index]) != JSON.stringify(this.initialNotifications[index])) {
222
        return true;
223
      }
224
    }
225
    return false;
226
  }
227
*/
228
  ngOnDestroy() {
229
    if(this.sub) {
230
      this.sub.unsubscribe();
231
    }
232
  }
233

    
234
  handleErrors(err){
235
    //this.showErrorMessage = true;
236
    //try{
237
      var code = "";
238
      if(!err.status) {
239
        var error =  err.json();
240
        code = error.code;
241
      } else {
242
        code = err.status;
243
      }
244
      this.status = this.errorMessages.getErrorCode(code);
245
    }
246

    
247
    private handleError(message: string, error) {
248
        console.error("User mail notification preferences Page (for claims): "+message, error);
249
    }
250

    
251

    
252
    //}catch (e) {
253
      //console.log("Couldn't parse answer as json")
254
      //this.showErrorMessage = true;
255
    //}
256
}
(2-2/4)