2019-09-13 10:26:09 +02:00
|
|
|
import {Component, ViewChild, Input} from '@angular/core';
|
2018-09-24 12:08:33 +02:00
|
|
|
import {Location} from '@angular/common';
|
2019-09-13 10:26:09 +02:00
|
|
|
import {Observable} from 'rxjs';
|
2018-09-24 12:08:33 +02:00
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
2019-09-13 10:26:09 +02:00
|
|
|
import {ModalLoading} from '../../utils/modal/loading.component';
|
|
|
|
import {AlertModal} from '../../utils/modal/alert';
|
2018-09-24 12:08:33 +02:00
|
|
|
import {Session} from '../../login/utils/helper.class';
|
|
|
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
|
|
|
import {MailPrefsService} from './mailPrefs.service';
|
|
|
|
import {ConnectHelper} from '../connectHelper';
|
|
|
|
import {ErrorCodes} from '../../utils/properties/errorCodes';
|
2019-09-13 10:26:09 +02:00
|
|
|
import {ErrorMessagesComponent} from '../../utils/errorMessages.component';
|
2018-11-01 18:21:10 +01:00
|
|
|
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
|
2018-09-24 12:08:33 +02:00
|
|
|
|
|
|
|
declare var UIkit: any;
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'mailPrefs',
|
|
|
|
templateUrl: 'mailPrefs.component.html',
|
2019-09-13 10:26:09 +02:00
|
|
|
providers:[MailPrefsService]
|
2018-09-24 12:08:33 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
export class MailPrefsComponent {
|
2019-09-13 10:26:09 +02:00
|
|
|
properties:EnvProperties;
|
2018-09-24 12:08:33 +02:00
|
|
|
sub: any;
|
|
|
|
public communityId: string;
|
|
|
|
public preferencesFor: string = "community";
|
|
|
|
public status: number;
|
|
|
|
|
|
|
|
public notifications = [];
|
|
|
|
public initialNotifications = [];
|
2018-11-01 18:21:10 +01:00
|
|
|
public prefsChanged = {};
|
2018-09-24 12:08:33 +02:00
|
|
|
|
|
|
|
//public showForbiddenMessage:boolean = false;
|
2019-09-13 10:26:09 +02:00
|
|
|
public userValidMessage:string = "";
|
2018-11-01 18:21:10 +01:00
|
|
|
public savedMessage: string = "";
|
2019-09-13 10:26:09 +02:00
|
|
|
|
2018-09-24 12:08:33 +02:00
|
|
|
private errorCodes: ErrorCodes;
|
2019-02-18 15:00:43 +01:00
|
|
|
private errorMessages: ErrorMessagesComponent;
|
2018-09-24 12:08:33 +02:00
|
|
|
|
2018-10-20 17:26:54 +02:00
|
|
|
@Input() showSaveResetButtons: boolean = true;
|
|
|
|
|
2019-09-13 10:26:09 +02:00
|
|
|
constructor (private _mailPrefsService: MailPrefsService, private route: ActivatedRoute, private _router:Router, private location: Location) {
|
2018-09-24 12:08:33 +02:00
|
|
|
this.errorCodes = new ErrorCodes();
|
2019-02-18 15:00:43 +01:00
|
|
|
this.errorMessages = new ErrorMessagesComponent();
|
2018-09-24 12:08:33 +02:00
|
|
|
this.status = this.errorCodes.LOADING;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.route.data
|
|
|
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
2019-03-01 12:29:52 +01:00
|
|
|
this.sub = this.route.queryParams.subscribe(params => {
|
2019-09-13 10:26:09 +02:00
|
|
|
this.communityId = ConnectHelper.getCommunityFromDomain(this.properties.domain);
|
|
|
|
if(!this.communityId) {
|
2019-07-03 12:14:24 +02:00
|
|
|
this.communityId = params['communityId'];
|
2019-03-01 12:29:52 +01:00
|
|
|
}
|
2019-09-13 10:26:09 +02:00
|
|
|
|
|
|
|
this.getEmailPreferences();
|
|
|
|
|
2019-03-01 12:29:52 +01:00
|
|
|
});
|
2019-09-13 09:01:19 +02:00
|
|
|
});
|
2019-03-01 12:29:52 +01:00
|
|
|
|
2018-09-24 12:08:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getEmailPreferences() {
|
2019-09-13 10:26:09 +02:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
//this.userValidMessage = "User session has expired. Please login again.";
|
|
|
|
if(this.showSaveResetButtons) {
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.status = this.errorCodes.LOADING;
|
|
|
|
this.savedMessage = "";
|
|
|
|
|
|
|
|
if(this.communityId && this.communityId != "openaire") {
|
|
|
|
this.preferencesFor = "community";
|
|
|
|
this._mailPrefsService.getUserEmailPreferencesForCommunity(this.communityId, this.properties.claimsAPIURL).subscribe(
|
|
|
|
data => {
|
|
|
|
if(data.code == "204") {
|
|
|
|
this.status = this.errorCodes.NONE;
|
|
|
|
} else {
|
|
|
|
this.initialNotifications = data.data;
|
|
|
|
this.notifications = JSON.parse(JSON.stringify( this.initialNotifications ));
|
|
|
|
|
|
|
|
this.status = this.errorCodes.DONE;
|
2018-09-24 12:08:33 +02:00
|
|
|
}
|
2019-09-13 10:26:09 +02:00
|
|
|
},
|
|
|
|
err => {
|
|
|
|
this.handleErrors(err);
|
|
|
|
this.handleError("Error getting user email preferences for community with id: "+this.communityId, err);
|
|
|
|
}
|
|
|
|
);
|
2018-09-24 12:08:33 +02:00
|
|
|
} else {
|
2019-09-13 10:26:09 +02:00
|
|
|
this.preferencesFor = "project";
|
|
|
|
this._mailPrefsService.getUserEmailPreferencesForOpenaire(this.properties.claimsAPIURL).subscribe(
|
|
|
|
data => {
|
2018-09-24 12:08:33 +02:00
|
|
|
|
2019-09-13 10:26:09 +02:00
|
|
|
if(data.code == "204") {
|
|
|
|
this.status = this.errorCodes.NONE;
|
|
|
|
} else {
|
2019-09-13 09:01:19 +02:00
|
|
|
|
2019-09-13 10:26:09 +02:00
|
|
|
this.initialNotifications = data.data;
|
|
|
|
this.notifications = JSON.parse(JSON.stringify( this.initialNotifications ));
|
|
|
|
//this.notifications = this.initialNotifications.map(x => Object.assign({}, x));
|
|
|
|
//this.notifications = this.initialNotifications;
|
2018-09-24 12:08:33 +02:00
|
|
|
|
2019-09-13 10:26:09 +02:00
|
|
|
this.status = this.errorCodes.DONE;
|
2018-09-24 12:08:33 +02:00
|
|
|
}
|
2019-09-13 10:26:09 +02:00
|
|
|
},
|
|
|
|
err => {
|
|
|
|
//console.info(err);
|
|
|
|
this.handleErrors(err);
|
|
|
|
this.handleError("Error getting user email preferences for openaire", err);
|
|
|
|
}
|
|
|
|
);
|
2018-09-24 12:08:33 +02:00
|
|
|
}
|
2019-09-13 10:26:09 +02:00
|
|
|
}
|
2018-09-24 12:08:33 +02:00
|
|
|
}
|
|
|
|
|
2018-11-01 18:21:10 +01:00
|
|
|
changeNotify(notification: any, checked: boolean, index: number) {
|
2019-09-13 10:26:09 +02:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
//this.userValidMessage = "User session has expired. Please login again.";
|
|
|
|
if(this.showSaveResetButtons) {
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
2018-11-14 12:07:45 +01:00
|
|
|
}
|
2019-09-13 10:26:09 +02:00
|
|
|
} else {
|
|
|
|
this.savedMessage = "";
|
|
|
|
this.status = this.errorCodes.DONE;
|
|
|
|
notification.notify = checked;
|
|
|
|
this.prefsChanged[index] = true;
|
|
|
|
}
|
2018-09-24 12:08:33 +02:00
|
|
|
}
|
|
|
|
|
2019-01-22 12:16:02 +01:00
|
|
|
changeFrequency(index: number) {
|
2019-09-13 10:26:09 +02:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
//this.userValidMessage = "User session has expired. Please login again.";
|
|
|
|
if(this.showSaveResetButtons) {
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.savedMessage = "";
|
|
|
|
this.status = this.errorCodes.DONE;
|
|
|
|
if(this.initialNotifications[index].frequency != this.notifications[index].frequency) {
|
|
|
|
this.prefsChanged[index] = true;
|
2018-11-14 12:07:45 +01:00
|
|
|
}
|
2019-09-13 09:01:19 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-24 12:08:33 +02:00
|
|
|
|
2019-09-13 10:26:09 +02:00
|
|
|
saveNotification(index: number) {
|
|
|
|
if(this.notifications.length > 0 && this.initialNotifications.length > 0) {
|
|
|
|
if(!Session.isLoggedIn()){
|
2019-09-13 09:01:19 +02:00
|
|
|
//this.userValidMessage = "User session has expired. Please login again.";
|
2019-09-13 10:26:09 +02:00
|
|
|
if(this.showSaveResetButtons) {
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
2018-11-01 18:21:10 +01:00
|
|
|
}
|
2019-09-13 09:01:19 +02:00
|
|
|
} else {
|
2019-09-13 10:26:09 +02:00
|
|
|
if(JSON.stringify(this.notifications[index]) != JSON.stringify(this.initialNotifications[index])) {
|
|
|
|
|
2019-09-13 09:01:19 +02:00
|
|
|
this.status = this.errorCodes.LOADING;
|
|
|
|
this.savedMessage = "";
|
2019-09-13 10:26:09 +02:00
|
|
|
|
|
|
|
this._mailPrefsService.saveUserEmailPreferences(this.notifications[index], this.properties.claimsAPIURL).subscribe(
|
|
|
|
data => {
|
|
|
|
this.initialNotifications[index] = JSON.parse(JSON.stringify( this.notifications[index] ));
|
|
|
|
|
|
|
|
this.status = this.errorCodes.DONE;
|
|
|
|
|
|
|
|
/*UIkit.notification({
|
|
|
|
message : '<strong>Your email preferences for '+this.notifications[index].openaireName+' have been successfully changed<strong>',
|
|
|
|
status : 'success',
|
|
|
|
timeout : 3000,
|
|
|
|
pos : 'top-center'
|
|
|
|
});*/
|
|
|
|
this.savedMessage = "Notification settings for claims saved!";
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
//console.log(err);
|
|
|
|
this.handleError("Error saving user email preferences: "+JSON.stringify(this.notifications[index]), err);
|
|
|
|
this.status = this.errorCodes.NOT_SAVED;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*UIkit.notification({
|
|
|
|
message : '<strong>No changes selected for '+this.notifications[index].openaireName+' email preferences<strong>',
|
|
|
|
status : 'primary',
|
|
|
|
timeout : 3000,
|
|
|
|
pos : 'top-center'
|
|
|
|
});*/
|
|
|
|
this.savedMessage = "Notification settings for claims saved!";
|
2018-11-08 15:22:58 +01:00
|
|
|
}
|
2018-09-24 12:08:33 +02:00
|
|
|
}
|
2019-09-13 10:26:09 +02:00
|
|
|
}
|
2018-09-24 12:08:33 +02:00
|
|
|
}
|
|
|
|
|
2019-09-13 10:26:09 +02:00
|
|
|
restoreNotification(index: number) {
|
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
//this.userValidMessage = "User session has expired. Please login again.";
|
|
|
|
if(this.showSaveResetButtons) {
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(this.notifications.length > 0 && this.initialNotifications.length > 0) {
|
|
|
|
this.status = this.errorCodes.LOADING;
|
|
|
|
this.savedMessage = "";
|
|
|
|
this.notifications[index] = JSON.parse(JSON.stringify( this.initialNotifications[index] ));
|
|
|
|
this.status = this.errorCodes.DONE;
|
|
|
|
this.prefsChanged[index] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-13 09:01:19 +02:00
|
|
|
/*
|
|
|
|
prefsChanged(index: number) : boolean {
|
2018-11-14 12:07:45 +01:00
|
|
|
if(this.notifications.length > 0 && this.initialNotifications.length > 0) {
|
2019-09-13 09:01:19 +02:00
|
|
|
if(JSON.stringify(this.notifications[index]) != JSON.stringify(this.initialNotifications[index])) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-01 18:21:10 +01:00
|
|
|
}
|
2019-09-13 09:01:19 +02:00
|
|
|
return false;
|
2018-11-01 18:21:10 +01:00
|
|
|
}
|
2019-09-13 09:01:19 +02:00
|
|
|
*/
|
2018-09-24 12:08:33 +02:00
|
|
|
ngOnDestroy() {
|
2019-09-13 10:26:09 +02:00
|
|
|
if(this.sub) {
|
2018-09-24 12:08:33 +02:00
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 10:26:09 +02:00
|
|
|
handleErrors(err){
|
2018-09-24 12:08:33 +02:00
|
|
|
//this.showErrorMessage = true;
|
|
|
|
//try{
|
2019-09-13 09:01:19 +02:00
|
|
|
var code = "";
|
2019-09-13 10:26:09 +02:00
|
|
|
if(!err.status) {
|
|
|
|
var error = err.json();
|
2019-09-13 09:01:19 +02:00
|
|
|
code = error.code;
|
|
|
|
} else {
|
|
|
|
code = err.status;
|
2019-02-18 15:00:43 +01:00
|
|
|
}
|
2019-09-13 09:01:19 +02:00
|
|
|
this.status = this.errorMessages.getErrorCode(code);
|
|
|
|
}
|
2019-02-18 15:00:43 +01:00
|
|
|
|
2019-09-13 09:01:19 +02:00
|
|
|
private handleError(message: string, error) {
|
2019-09-13 10:26:09 +02:00
|
|
|
console.error("User mail notification preferences Page (for claims): "+message, error);
|
2019-09-13 09:01:19 +02:00
|
|
|
}
|
2018-09-24 12:08:33 +02:00
|
|
|
|
|
|
|
|
2019-09-13 09:01:19 +02:00
|
|
|
//}catch (e) {
|
|
|
|
//console.log("Couldn't parse answer as json")
|
|
|
|
//this.showErrorMessage = true;
|
|
|
|
//}
|
2018-09-24 12:08:33 +02:00
|
|
|
}
|