2018-09-24 12:08:33 +02:00
|
|
|
import {Component, ViewChild, Input} from '@angular/core';
|
|
|
|
import {Location} from '@angular/common';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {ModalLoading} from '../../utils/modal/loading.component';
|
|
|
|
import {AlertModal} from '../../utils/modal/alert';
|
|
|
|
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';
|
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',
|
|
|
|
providers:[MailPrefsService]
|
|
|
|
|
|
|
|
})
|
|
|
|
export class MailPrefsComponent {
|
|
|
|
properties:EnvProperties;
|
|
|
|
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;
|
|
|
|
public userValidMessage:string = "";
|
2018-11-01 18:21:10 +01:00
|
|
|
public savedMessage: string = "";
|
2018-09-24 12:08:33 +02:00
|
|
|
|
|
|
|
public fetchId:string;
|
|
|
|
|
|
|
|
private errorCodes: ErrorCodes;
|
|
|
|
|
2018-10-20 17:26:54 +02:00
|
|
|
@Input() showSaveResetButtons: boolean = true;
|
|
|
|
|
2018-09-24 12:08:33 +02:00
|
|
|
constructor (private _mailPrefsService: MailPrefsService, private route: ActivatedRoute, private _router:Router, private location: Location) {
|
|
|
|
this.errorCodes = new ErrorCodes();
|
|
|
|
this.status = this.errorCodes.LOADING;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.route.data
|
|
|
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.sub = this.route.queryParams.subscribe(params => {
|
|
|
|
this.communityId = params['communityId'];
|
|
|
|
|
|
|
|
if(!this.communityId){
|
|
|
|
this.communityId = ConnectHelper.getCommunityFromDomain(document.location.hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.fetchId = Session.getUserEmail();
|
|
|
|
console.info("email: "+this.fetchId);
|
|
|
|
console.info("communityId: " + this.communityId);
|
|
|
|
|
|
|
|
this.getEmailPreferences();
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getEmailPreferences() {
|
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this.userValidMessage = "User session has expired. Please login again.";
|
2018-11-01 18:21:10 +01:00
|
|
|
if(this.showSaveResetButtons) {
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
}
|
2018-09-24 12:08:33 +02:00
|
|
|
}else{
|
|
|
|
this.status = this.errorCodes.LOADING;
|
2018-11-01 18:21:10 +01:00
|
|
|
this.savedMessage = "";
|
2018-09-24 12:08:33 +02:00
|
|
|
|
|
|
|
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;
|
2018-10-15 11:51:01 +02:00
|
|
|
this.notifications = JSON.parse(JSON.stringify( this.initialNotifications ));
|
2018-09-24 12:08:33 +02:00
|
|
|
|
|
|
|
this.status = this.errorCodes.DONE;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
this.handleErrors(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.preferencesFor = "project";
|
|
|
|
this._mailPrefsService.getUserEmailPreferencesForOpenaire(this.properties.claimsAPIURL).subscribe(
|
|
|
|
data => {
|
|
|
|
console.info("email prefs returned");
|
|
|
|
|
|
|
|
if(data.code == "204") {
|
|
|
|
this.status = this.errorCodes.NONE;
|
|
|
|
} else {
|
|
|
|
console.info(data);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
this.status = this.errorCodes.DONE;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
//console.info(err);
|
|
|
|
this.handleErrors(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 18:21:10 +01:00
|
|
|
changeNotify(notification: any, checked: boolean, index: number) {
|
|
|
|
this.savedMessage = "";
|
|
|
|
this.status = this.errorCodes.DONE;
|
2018-09-24 12:08:33 +02:00
|
|
|
notification.notify = checked;
|
2018-11-01 18:21:10 +01:00
|
|
|
this.prefsChanged[index] = true;
|
2018-09-24 12:08:33 +02:00
|
|
|
}
|
|
|
|
|
2018-11-01 18:21:10 +01:00
|
|
|
changeFrequency(value: number, index: number) {
|
|
|
|
this.savedMessage = "";
|
|
|
|
this.status = this.errorCodes.DONE;
|
|
|
|
if(this.initialNotifications[index].frequency != value) {
|
|
|
|
this.prefsChanged[index] = true;
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 12:08:33 +02:00
|
|
|
|
2018-11-01 18:21:10 +01:00
|
|
|
saveNotification(index: number) {
|
|
|
|
if(this.notifications.length > 0 && this.initialNotifications.length > 0) {
|
2018-11-08 15:22:58 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
2018-11-01 18:21:10 +01:00
|
|
|
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-08 15:22:58 +01:00
|
|
|
}else{
|
|
|
|
if(JSON.stringify(this.notifications[index]) != JSON.stringify(this.initialNotifications[index])) {
|
|
|
|
|
2018-11-01 18:21:10 +01:00
|
|
|
this.status = this.errorCodes.LOADING;
|
|
|
|
this.savedMessage = "";
|
|
|
|
console.info("Send notification to db: ", this.notifications[index]);
|
2018-09-24 12:08:33 +02:00
|
|
|
|
2018-11-01 18:21:10 +01:00
|
|
|
this._mailPrefsService.saveUserEmailPreferences(this.notifications[index], this.properties.claimsAPIURL).subscribe(
|
|
|
|
data => {
|
|
|
|
console.info("Notification saved successfully");
|
|
|
|
this.initialNotifications[index] = JSON.parse(JSON.stringify( this.notifications[index] ));
|
2018-09-24 12:08:33 +02:00
|
|
|
|
2018-11-01 18:21:10 +01:00
|
|
|
this.status = this.errorCodes.DONE;
|
2018-09-24 12:08:33 +02:00
|
|
|
|
2018-11-01 18:21:10 +01:00
|
|
|
/*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.status = this.errorCodes.NOT_SAVED;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2018-11-08 15:22:58 +01:00
|
|
|
else {
|
|
|
|
console.info("Notification not changed: ", this.notifications[index]);
|
|
|
|
/*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-09-24 12:08:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 17:26:54 +02:00
|
|
|
restoreNotification(index: number) {
|
2018-11-01 18:21:10 +01:00
|
|
|
if(this.notifications.length > 0 && this.initialNotifications.length > 0) {
|
|
|
|
this.status = this.errorCodes.LOADING;
|
|
|
|
this.savedMessage = "";
|
|
|
|
console.info("Restore Notification");
|
|
|
|
console.info(this.notifications[index]);
|
|
|
|
this.notifications[index] = JSON.parse(JSON.stringify( this.initialNotifications[index] ));
|
|
|
|
console.info(this.initialNotifications[index]);
|
|
|
|
this.status = this.errorCodes.DONE;
|
|
|
|
this.prefsChanged[index] = false;
|
|
|
|
}
|
2018-09-24 12:08:33 +02:00
|
|
|
}
|
2018-11-01 18:21:10 +01:00
|
|
|
/*
|
|
|
|
prefsChanged(index: number) : boolean {
|
|
|
|
if(this.notifications.length > 0 && this.initialNotifications.length > 0) {
|
|
|
|
if(JSON.stringify(this.notifications[index]) != JSON.stringify(this.initialNotifications[index])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*/
|
2018-09-24 12:08:33 +02:00
|
|
|
ngOnDestroy() {
|
|
|
|
if(this.sub) {
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleErrors(err){
|
|
|
|
//this.showErrorMessage = true;
|
|
|
|
//try{
|
2018-11-01 18:21:10 +01:00
|
|
|
var code = "";
|
|
|
|
if(!err.status) {
|
|
|
|
var error = err.json();
|
|
|
|
code = error.code;
|
|
|
|
} else {
|
|
|
|
code = err.status;
|
|
|
|
}
|
2018-09-24 12:08:33 +02:00
|
|
|
console.info(code);
|
|
|
|
if(code == "403") {
|
|
|
|
this.status = this.errorCodes.FORBIDDEN;
|
|
|
|
}
|
|
|
|
else if(code == "204") {
|
|
|
|
this.status = this.errorCodes.NONE;
|
|
|
|
} else if(code == "404") {
|
|
|
|
this.status = this.errorCodes.NOT_FOUND;
|
|
|
|
} else if(code == "500") {
|
|
|
|
this.status = this.errorCodes.ERROR;
|
|
|
|
} else {
|
|
|
|
this.status = this.errorCodes.NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//}catch (e) {
|
|
|
|
//console.log("Couldn't parse answer as json")
|
|
|
|
//this.showErrorMessage = true;
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
}
|