connect-admin/src/app/pages/usernotifications/manage-user-notifications.c...

262 lines
10 KiB
TypeScript

import {Component, OnInit, Input, ViewChild, ElementRef} from '@angular/core';
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';
import {MailPrefsComponent} from '../../openaireLibrary/connect/userEmailPreferences/mailPrefs.component';
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
@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;
public initialUserNotifications = null;
public userEmail = null;
public showLoading: boolean = true;
public errorMessage: string = '';
public updateErrorMessage: string = '';
public successfulSaveMessage: string = '';
public successfulResetMessage: string = '';
public hasChanged: boolean = false;
@ViewChild (MailPrefsComponent) mailPrefs : MailPrefsComponent;
constructor (private route: ActivatedRoute, private _router: Router, public _fb: FormBuilder,
private _manageUserNotificationsService: ManageUserNotificationsService, private element: ElementRef) {
}
ngOnInit() {
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
this.route.queryParams.subscribe(
communityId => {
this.scroll();
this.communityId = communityId['communityId'];
if (this.communityId != null && this.communityId != '') {
this.showLoading = true;
this.updateErrorMessage = "";
this.errorMessage = "";
this.successfulSaveMessage = "";
if (Session.getUser()) {
this.userEmail = Session.getUserEmail();
this._manageUserNotificationsService.getUserNotifications(this.properties.adminToolsAPIURL + "community/" + this.communityId + "/notifications", this.userEmail).subscribe(
userNotifications => {
this.initialUserNotifications = userNotifications;
if (this.initialUserNotifications['notifyForNewManagers'] == null || this.initialUserNotifications['notifyForNewSubscribers'] == null) {
this.initialUserNotifications = this.initiateUserNotifications();
}
this.userNotifications = JSON.parse(JSON.stringify( this.initialUserNotifications ));
//TODO remove after final testing
console.log("Before: ", userNotifications);
console.log("After: ", this.initialUserNotifications);
this.showLoading = false;
},
error => {
console.log(error.status);
if (error.status == '404') {
this.initialUserNotifications = this.initiateUserNotifications();
console.log(this.initialUserNotifications);
this.userNotifications = JSON.parse(JSON.stringify( this.initialUserNotifications ));
} else {
this.handleError('System error retrieving user notifications', error)
}
this.showLoading = false;
}
);
}
}
}
);
});
}
public initiateUserNotifications() : UserNotificationsRights {
var notificationRights: UserNotificationsRights = new UserNotificationsRights();
notificationRights['notifyForNewManagers'] = true;
notificationRights['notifyForNewSubscribers'] = true;
notificationRights['managerEmail'] = this.userEmail;
return notificationRights;
}
public updateUserNotifications() {
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();
}
}
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) {
/*
if (communityId != null && communityId != '') {
this.showLoading = true;
this.updateErrorMessage = "";
this.errorMessage = "";
this.mailPrefs.restoreNotification(0);
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)
);
}
*/
if(!Session.isLoggedIn()){
console.info(this._router.url);
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
} else {
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;
}
this.resetChange();
}
}
public changeValueForNewManagers(notifyForManagers : any) {
if(!Session.isLoggedIn()){
console.info(this._router.url);
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
} else {
this.userNotifications.notifyForNewManagers = !notifyForManagers;
this.change();
}
}
public changeValueForNewSubscribers(notifyForSubscribers : any) {
if(!Session.isLoggedIn()){
console.info(this._router.url);
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
} else {
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');
}
public mailPrefsChanged(): boolean {
if(!Session.isLoggedIn()){
console.info(this._router.url);
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
} else {
return this.mailPrefs.prefsChanged["0"];//(0);
}
}
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;
}
public scroll() {
console.info("scroll into view");
if (typeof document !== 'undefined') {
this.element.nativeElement.scrollIntoView();
}
}
}