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

195 lines
7.4 KiB
TypeScript
Raw Normal View History

import {Component, OnInit, Input} 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';
@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 userEmail = null;
public showLoading: boolean = true;
public errorMessage: string = '';
public updateErrorMessage: string = '';
public successfulSaveMessage: string = '';
public successfulResetMessage: string = '';
public hasChanged: boolean = false;
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 = "";
if (Session.getUser()) {
this.userEmail = Session.getUserEmail();
this._manageUserNotificationsService.getUserNotifications(this.properties.adminToolsAPIURL + "community/" + this.communityId + "/notifications", this.userEmail).subscribe(
userNotifications => {
this.userNotifications = userNotifications;
if (this.userNotifications['notifyForNewManagers'] == null || this.userNotifications['notifyForNewSubscribers'] == null) {
this.userNotifications = this.initiateUserNotifications();
}
//TODO remove after final testing
console.log("Before: ", userNotifications);
console.log("After: ", this.userNotifications);
this.showLoading = false;
},
error => {
console.log(error.status);
if (error.status == '404') {
this.userNotifications = this.initiateUserNotifications();
console.log(this.userNotifications);
this.showLoading = false;
} else {
this.handleError('System error retrieving user notifications', error)
}
}
);
}
}
}
);
});
}
public initiateUserNotifications() : UserNotificationsRights {
var notificationRights: UserNotificationsRights = new UserNotificationsRights();
notificationRights['notifyForNewManagers'] = true;
notificationRights['notifyForNewSubscribers'] = true;
notificationRights['managerEmail'] = this.userEmail;
return notificationRights;
}
public updateUserNotifications() {
if (this.communityId != null && this.communityId != '') {
this.showLoading = true;
var userNotifications = this.parseUpdatedUserNotifications();
console.log(userNotifications);
this._manageUserNotificationsService.updateUserNotifications(this.properties.adminToolsAPIURL + "community/" + this.communityId + "/notifications", userNotifications).subscribe(
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._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)
);
}
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');
}
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;
}
}