2018-11-01 18:21:10 +01:00
|
|
|
import {Component, OnInit, Input, ViewChild} from '@angular/core';
|
2018-10-30 16:31:16 +01:00
|
|
|
import {FormGroup, FormBuilder} from '@angular/forms';
|
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {CommonModule} from "@angular/common";
|
|
|
|
|
|
|
|
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
|
|
|
|
|
|
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
|
|
|
|
|
|
|
import {ManageUserNotificationsService} from './manage-user-notifications.service';
|
|
|
|
|
|
|
|
import {UserNotificationsRights} from './userNotificationsRights';
|
|
|
|
|
2018-11-01 18:21:10 +01:00
|
|
|
import {MailPrefsComponent} from '../../openaireLibrary/connect/userEmailPreferences/mailPrefs.component';
|
|
|
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'manage-user-notifications',
|
|
|
|
templateUrl: './manage-user-notifications.component.html',
|
|
|
|
})
|
|
|
|
|
|
|
|
export class ManageUserNotificationsComponent implements OnInit {
|
|
|
|
|
|
|
|
@Input('group')
|
|
|
|
myForm: FormGroup;
|
|
|
|
|
|
|
|
public properties: EnvProperties = null;
|
|
|
|
public communityId = null;
|
|
|
|
public userNotifications = null;
|
2018-11-01 18:21:10 +01:00
|
|
|
public initialUserNotifications = null;
|
2018-10-30 16:31:16 +01:00
|
|
|
public userEmail = null;
|
|
|
|
|
|
|
|
public showLoading: boolean = true;
|
|
|
|
public errorMessage: string = '';
|
|
|
|
public updateErrorMessage: string = '';
|
|
|
|
|
|
|
|
public successfulSaveMessage: string = '';
|
|
|
|
public successfulResetMessage: string = '';
|
|
|
|
|
|
|
|
public hasChanged: boolean = false;
|
|
|
|
|
2018-11-01 18:21:10 +01:00
|
|
|
@ViewChild (MailPrefsComponent) mailPrefs : MailPrefsComponent;
|
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
constructor (private route: ActivatedRoute, private _router: Router, public _fb: FormBuilder,
|
|
|
|
private _manageUserNotificationsService: ManageUserNotificationsService) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
|
|
|
this.route.queryParams.subscribe(
|
|
|
|
communityId => {
|
|
|
|
this.communityId = communityId['communityId'];
|
|
|
|
if (this.communityId != null && this.communityId != '') {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.updateErrorMessage = "";
|
|
|
|
this.errorMessage = "";
|
2018-11-01 18:21:10 +01:00
|
|
|
this.successfulSaveMessage = "";
|
2018-10-30 16:31:16 +01:00
|
|
|
|
|
|
|
if (Session.getUser()) {
|
|
|
|
this.userEmail = Session.getUserEmail();
|
|
|
|
|
|
|
|
this._manageUserNotificationsService.getUserNotifications(this.properties.adminToolsAPIURL + "community/" + this.communityId + "/notifications", this.userEmail).subscribe(
|
|
|
|
userNotifications => {
|
2018-11-01 18:21:10 +01:00
|
|
|
this.initialUserNotifications = userNotifications;
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2018-11-01 18:21:10 +01:00
|
|
|
if (this.initialUserNotifications['notifyForNewManagers'] == null || this.initialUserNotifications['notifyForNewSubscribers'] == null) {
|
|
|
|
this.initialUserNotifications = this.initiateUserNotifications();
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
2018-11-01 18:21:10 +01:00
|
|
|
this.userNotifications = JSON.parse(JSON.stringify( this.initialUserNotifications ));
|
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
//TODO remove after final testing
|
|
|
|
console.log("Before: ", userNotifications);
|
2018-11-01 18:21:10 +01:00
|
|
|
console.log("After: ", this.initialUserNotifications);
|
2018-10-30 16:31:16 +01:00
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
console.log(error.status);
|
|
|
|
if (error.status == '404') {
|
2018-11-01 18:21:10 +01:00
|
|
|
this.initialUserNotifications = this.initiateUserNotifications();
|
|
|
|
console.log(this.initialUserNotifications);
|
|
|
|
this.userNotifications = JSON.parse(JSON.stringify( this.initialUserNotifications ));
|
2018-10-30 16:31:16 +01:00
|
|
|
} else {
|
|
|
|
this.handleError('System error retrieving user notifications', error)
|
|
|
|
}
|
2018-11-01 18:21:10 +01:00
|
|
|
this.showLoading = false;
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public initiateUserNotifications() : UserNotificationsRights {
|
|
|
|
var notificationRights: UserNotificationsRights = new UserNotificationsRights();
|
|
|
|
|
|
|
|
notificationRights['notifyForNewManagers'] = true;
|
|
|
|
notificationRights['notifyForNewSubscribers'] = true;
|
|
|
|
notificationRights['managerEmail'] = this.userEmail;
|
|
|
|
|
|
|
|
return notificationRights;
|
|
|
|
}
|
|
|
|
|
|
|
|
public updateUserNotifications() {
|
2018-11-01 18:21:10 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
console.info(this._router.url);
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
|
|
|
if (this.communityId != null && this.communityId != '') {
|
|
|
|
this.mailPrefs.saveNotification(0);
|
|
|
|
|
|
|
|
this.successfulSaveMessage = "";
|
|
|
|
this.showLoading = true;
|
|
|
|
var userNotifications = this.parseUpdatedUserNotifications();
|
|
|
|
console.log(userNotifications);
|
|
|
|
|
|
|
|
this._manageUserNotificationsService.updateUserNotifications(this.properties.adminToolsAPIURL + "community/" + this.communityId + "/notifications", userNotifications).subscribe(
|
|
|
|
userNotifications => {
|
|
|
|
this.initialUserNotifications = JSON.parse(JSON.stringify( this.userNotifications ));
|
|
|
|
this.handleSuccessfulSave('Notification settings saved!')
|
|
|
|
},
|
|
|
|
error => this.handleUpdateError('System error updating user notifications', error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.resetChange();
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private parseUpdatedUserNotifications() : {} {
|
|
|
|
var userNotifications = {};
|
|
|
|
|
|
|
|
userNotifications["notifyForNewManagers"] = this.userNotifications.notifyForNewManagers;
|
|
|
|
userNotifications["notifyForNewSubscribers"] = this.userNotifications.notifyForNewSubscribers;
|
|
|
|
|
|
|
|
if (this.userNotifications.managerEmail) {
|
|
|
|
userNotifications["managerEmail"] = this.userNotifications.managerEmail;
|
|
|
|
} else {
|
|
|
|
if (Session.getUser()) {
|
|
|
|
userNotifications["managerEmail"] = Session.getUserEmail();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return userNotifications;
|
|
|
|
}
|
|
|
|
|
|
|
|
public resetForm(communityId:string) {
|
2018-11-01 18:21:10 +01:00
|
|
|
/*
|
2018-10-30 16:31:16 +01:00
|
|
|
if (communityId != null && communityId != '') {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.updateErrorMessage = "";
|
|
|
|
this.errorMessage = "";
|
|
|
|
|
2018-11-01 18:21:10 +01:00
|
|
|
this.mailPrefs.restoreNotification(0);
|
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
this._manageUserNotificationsService.getUserNotifications(this.properties.adminToolsAPIURL + "community/" + this.communityId + "/notifications", this.userEmail).subscribe(
|
|
|
|
userNotifications => {
|
|
|
|
this.userNotifications = userNotifications;
|
|
|
|
this.showLoading = false;
|
|
|
|
console.log(userNotifications);
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving user notifications', error)
|
|
|
|
);
|
|
|
|
}
|
2018-11-01 18:21:10 +01:00
|
|
|
|
|
|
|
*/
|
|
|
|
this.mailPrefs.restoreNotification(0);
|
|
|
|
|
|
|
|
if(this.userNotifications && this.initialUserNotifications) {
|
|
|
|
this.successfulSaveMessage = "";
|
|
|
|
this.showLoading = true;
|
|
|
|
this.userNotifications = JSON.parse(JSON.stringify( this.initialUserNotifications ));
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
this.resetChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
public changeValueForNewManagers(notifyForManagers : any) {
|
|
|
|
this.userNotifications.notifyForNewManagers = !notifyForManagers;
|
|
|
|
this.change();
|
|
|
|
}
|
|
|
|
|
|
|
|
public changeValueForNewSubscribers(notifyForSubscribers : any) {
|
|
|
|
this.userNotifications.notifyForNewSubscribers = !notifyForSubscribers;
|
|
|
|
this.change();
|
|
|
|
}
|
|
|
|
|
|
|
|
private change() {
|
|
|
|
this.hasChanged = true;
|
|
|
|
this.successfulSaveMessage = '';
|
|
|
|
this.successfulResetMessage = '';
|
|
|
|
// TODO remove after testing
|
|
|
|
console.log('I have changed: I AM TRUE');
|
|
|
|
}
|
|
|
|
|
|
|
|
private resetChange() {
|
|
|
|
this.hasChanged = false;
|
|
|
|
// TODO remove after testing
|
|
|
|
console.log('I have changed: I AM FALSE');
|
|
|
|
}
|
|
|
|
|
2018-11-01 18:21:10 +01:00
|
|
|
public mailPrefsChanged(): boolean {
|
|
|
|
return this.mailPrefs.prefsChanged["0"];//(0);
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
handleUpdateError(message: string, error) {
|
|
|
|
this.updateErrorMessage = message;
|
|
|
|
console.log('Server responded: ' +error);
|
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError(message: string, error) {
|
|
|
|
this.errorMessage = message;
|
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSuccessfulSave(message) {
|
|
|
|
this.showLoading = false;
|
|
|
|
this.successfulSaveMessage = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSuccessfulReset(message) {
|
|
|
|
this.showLoading = false;
|
|
|
|
this.successfulResetMessage = message;
|
|
|
|
}
|
|
|
|
}
|