2020-11-12 16:41:03 +01:00
|
|
|
import {Component, Input} from '@angular/core';
|
2018-09-24 12:08:33 +02:00
|
|
|
import {Location} from '@angular/common';
|
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
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';
|
2020-11-12 16:41:03 +01:00
|
|
|
import {properties} from "../../../../environments/environment";
|
|
|
|
import {Subscriber} from "rxjs";
|
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;
|
2020-11-12 16:41:03 +01:00
|
|
|
subscriptions = [];
|
2021-02-10 11:24:52 +01:00
|
|
|
@Input() communityId: string;
|
2018-09-24 12:08:33 +02:00
|
|
|
public preferencesFor: string = "community";
|
|
|
|
public status: number;
|
|
|
|
|
|
|
|
public notifications = [];
|
|
|
|
public initialNotifications = [];
|
2018-11-01 18:21:10 +01:00
|
|
|
public prefsChanged = {};
|
2021-01-12 13:09:21 +01:00
|
|
|
public hidden: boolean = true;
|
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() {
|
2020-11-12 16:41:03 +01:00
|
|
|
this.properties = properties;
|
2021-02-10 11:24:52 +01:00
|
|
|
this.subscriptions.push(this.route.params.subscribe(params => {
|
2021-01-12 13:09:21 +01:00
|
|
|
this.hidden = true;
|
2021-02-10 11:24:52 +01:00
|
|
|
console.debug(this.communityId);
|
|
|
|
if(!this.communityId){
|
|
|
|
this.communityId = ConnectHelper.getCommunityFromDomain(this.properties.domain);
|
|
|
|
}
|
2020-11-12 16:41:03 +01:00
|
|
|
if(!this.communityId) {
|
2021-02-10 11:24:52 +01:00
|
|
|
this.communityId = params['community'];
|
2020-11-12 16:41:03 +01:00
|
|
|
}
|
2021-02-10 11:24:52 +01:00
|
|
|
console.debug(this.communityId, params)
|
2020-11-12 16:41:03 +01:00
|
|
|
this.getEmailPreferences();
|
|
|
|
}));
|
2019-09-13 10:26:09 +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";
|
2020-11-12 16:41:03 +01:00
|
|
|
this.subscriptions.push(this._mailPrefsService.getUserEmailPreferencesForCommunity(this.communityId, this.properties.claimsAPIURL).subscribe(
|
2019-09-13 10:26:09 +02:00
|
|
|
data => {
|
|
|
|
if(data.code == "204") {
|
|
|
|
this.status = this.errorCodes.NONE;
|
2021-02-10 11:24:52 +01:00
|
|
|
this.initialNotifications = [{notify: true, frequency:24, openaireId: this.communityId}];
|
2019-09-13 10:26:09 +02:00
|
|
|
} else {
|
|
|
|
this.initialNotifications = data.data;
|
2021-02-10 11:24:52 +01:00
|
|
|
}
|
2019-09-13 10:26:09 +02:00
|
|
|
this.notifications = JSON.parse(JSON.stringify( this.initialNotifications ));
|
|
|
|
|
|
|
|
this.status = this.errorCodes.DONE;
|
2021-01-12 13:09:21 +01:00
|
|
|
this.hidden = false;
|
2021-02-10 11:24:52 +01:00
|
|
|
|
2019-09-13 10:26:09 +02:00
|
|
|
},
|
|
|
|
err => {
|
2021-01-12 13:09:21 +01:00
|
|
|
this.hidden = false;
|
2019-09-13 10:26:09 +02:00
|
|
|
this.handleErrors(err);
|
|
|
|
this.handleError("Error getting user email preferences for community with id: "+this.communityId, err);
|
|
|
|
}
|
2020-11-12 16:41:03 +01:00
|
|
|
));
|
2018-09-24 12:08:33 +02:00
|
|
|
} else {
|
2019-09-13 10:26:09 +02:00
|
|
|
this.preferencesFor = "project";
|
2020-11-12 16:41:03 +01:00
|
|
|
this.subscriptions.push(this._mailPrefsService.getUserEmailPreferencesForOpenaire(this.properties.claimsAPIURL).subscribe(
|
2019-09-13 10:26:09 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-11-12 16:41:03 +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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-11-12 16:41:03 +01:00
|
|
|
this.subscriptions.push(this._mailPrefsService.saveUserEmailPreferences(this.notifications[index], this.properties.claimsAPIURL).subscribe(
|
2019-09-13 10:26:09 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-11-12 16:41:03 +01:00
|
|
|
));
|
2019-09-13 10:26:09 +02:00
|
|
|
}
|
|
|
|
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() {
|
2020-11-12 16:41:03 +01:00
|
|
|
this.subscriptions.forEach(subscription => {
|
|
|
|
if (subscription instanceof Subscriber) {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
2018-09-24 12:08:33 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 16:41:03 +01:00
|
|
|
|
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
|
|
|
}
|