openaire-library/connect/userEmailPreferences/mailPrefs.component.ts

204 lines
6.4 KiB
TypeScript
Raw Normal View History

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';
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 = [];
public showErrorMessage:boolean = false;
//public showForbiddenMessage:boolean = false;
public userValidMessage:string = "";
public fetchId:string;
private errorCodes: ErrorCodes;
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.";
}else{
this.status = this.errorCodes.LOADING;
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;
}
},
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);
}
);
}
}
}
changeNotify(notification: any, checked: boolean) {
notification.notify = checked;
}
saveNotification(notifications: any, index: number) {
if(JSON.stringify(this.notifications[index]) != JSON.stringify(this.initialNotifications[index])) {
if(!Session.isLoggedIn()){
this.userValidMessage = "User session has expired. Please login again.";
}else{
this.status = this.errorCodes.LOADING;
console.info("Send notification to db: ", this.notifications[index]);
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] ));
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'
});
},
err => {
console.log(err);
this.status = this.errorCodes.NOT_SAVED;
}
);
}
} 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'
});
}
}
restoreNotification(notifications: any, index: number) {
console.info("Restore Notification");
console.info(this.notifications[index]);
this.notifications[index] = JSON.parse(JSON.stringify( this.initialNotifications[index] ));
console.info(this.initialNotifications[index]);
}
ngOnDestroy() {
if(this.sub) {
this.sub.unsubscribe();
}
}
handleErrors(err){
//this.showErrorMessage = true;
//try{
var error = err.json()
//var code = error.code;
console.info(err);
var code = error.code;
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;
//}
}
}