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

266 lines
10 KiB
TypeScript
Raw Normal View History

import {Component, ElementRef, Input, OnInit} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {ActivatedRoute, Router} from '@angular/router';
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
import {Session, User} from '../../openaireLibrary/login/utils/helper.class';
import {ManageUserNotificationsService} from './manage-user-notifications.service';
import {UserNotificationsRights} from './userNotificationsRights';
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
import {HelperFunctions} from '../../openaireLibrary/utils/HelperFunctions.class';
import {UserManagementService} from '../../openaireLibrary/services/user-management.service';
import {Title} from '@angular/platform-browser';
import {CommunityService} from '../../openaireLibrary/connect/community/community.service';
import {properties} from '../../../environments/environment';
import { Subscriber} from 'rxjs';
import {MailPrefsService} from '../../openaireLibrary/connect/userEmailPreferences/mailPrefs.service';
declare var UIkit;
@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;
public userNotifications:UserNotificationsRights = null;
public initialUserNotifications:UserNotificationsRights = null;
public notifications = null;
public initialNotifications = [];
public userEmail = null;
public showLoading = true;
public hasChanged = false;
public user: User;
private subscriptions = [];
frequencyOptions = [{label:"Daily", value: 24}, {label:"Every two days", value: 48}, {label:"Weekly", value: 168}]
constructor(private route: ActivatedRoute, private _router: Router, public _fb: FormBuilder,
private title: Title,
private _manageUserNotificationsService: ManageUserNotificationsService,
private element: ElementRef, private userManagementService: UserManagementService, private communityService: CommunityService, private _mailPrefsService: MailPrefsService,) {
}
ngOnInit() {
this.properties = properties;
HelperFunctions.scroll();
this.subscriptions.push(this.communityService.getCommunityAsObservable().subscribe(community => {
if(community) {
this.communityId =community.communityId;
this.title.setTitle( this.communityId.toUpperCase() + ' | User Notifications');
this.showLoading = true;
this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
this.user = user;
if (this.user) {
this.userEmail = this.user.email;
this.subscriptions.push(this._manageUserNotificationsService.getUserNotifications(this.properties, this.communityId).subscribe(
userNotifications => {
this.initialUserNotifications = userNotifications;
if (this.initialUserNotifications['notifyForNewManagers'] == null ||
this.initialUserNotifications['notifyForNewSubscribers'] == null) {
if(Session.isManager("community",this.communityId, this.user)) {
this.initialUserNotifications = new UserNotificationsRights(true, true, "");
}else{
this.initialUserNotifications = new UserNotificationsRights(false, false, "");
}
}
this.userNotifications = JSON.parse(JSON.stringify(this.initialUserNotifications));
this.getClaimsNotifications();
},
error => {
if (error.status === 404) {
if(Session.isManager("community",this.communityId, this.user)) {
this.initialUserNotifications = new UserNotificationsRights(true, true, "");
}else{
this.initialUserNotifications = new UserNotificationsRights(false, false, "");
}
this.userNotifications = JSON.parse(JSON.stringify(this.initialUserNotifications));
} else {
this.handleError('System error retrieving user notifications', error);
}
this.getClaimsNotifications();
}
));
}
}));
}
}));
}
getClaimsNotifications(){
this.subscriptions.push(this._mailPrefsService.getUserEmailPreferencesForCommunity(this.communityId, this.properties.claimsAPIURL).subscribe(
data => {
this.initialNotifications = data.data;
this.notifications = JSON.parse(JSON.stringify( this.initialNotifications ));
this.showLoading = false;
},
err => {
if(err.status === 404) {
if(Session.isManager("community",this.communityId, this.user)) {
this.initialNotifications = [{notify: true, frequency:24, openaireId: this.communityId}];
}else{
this.initialNotifications = [{notify: false, frequency:24, openaireId: this.communityId}];
}
this.notifications = JSON.parse(JSON.stringify( this.initialNotifications ));
}else{
this.handleError("Error getting user email preferences for community with id: "+this.communityId, err);
}
this.showLoading = false;
}
));
}
ngOnDestroy() {
this.subscriptions.forEach(value => {
if (value instanceof Subscriber) {
value.unsubscribe();
}
});
}
public updateUserNotifications() {
if (!Session.isLoggedIn()) {
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;
const userNotifications = this.parseUpdatedUserNotifications();
this.subscriptions.push(this._manageUserNotificationsService.updateUserNotifications(this.properties, this.communityId, 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.subscriptions.push(this._mailPrefsService.saveUserEmailPreferences(this.notifications[0], this.properties.claimsAPIURL).subscribe(
data => {
this.initialNotifications[0] = JSON.parse(JSON.stringify( this.notifications[0] ));
this.handleSuccessfulSave('Claims notification settings saved!');
},
err => {
//console.log(err);
this.handleError("Error saving user email preferences: "+JSON.stringify(this.notifications[0]), err);
}
));
}
this.resetChange();
}
}
private parseUpdatedUserNotifications(): {} {
const userNotifications = {};
userNotifications['notifyForNewManagers'] = this.userNotifications.notifyForNewManagers;
userNotifications['notifyForNewSubscribers'] = this.userNotifications.notifyForNewSubscribers;
if (this.userNotifications.managerEmail) {
userNotifications['managerEmail'] = this.userNotifications.managerEmail;
}
// else {
// if (this.user) {
// userNotifications['managerEmail'] = this.user.email;
// }
// }
return userNotifications;
}
public resetForm() {
if (!Session.isLoggedIn()) {
this._router.navigate(['/user-info'], {
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
});
} else {
// this.mailPrefs.restoreNotification(0);
this.notifications[0] = JSON.parse(JSON.stringify( this.initialNotifications[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()) {
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()) {
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 = '';
}
private resetChange() {
this.hasChanged = 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);
UIkit.notification(message, {
status: 'danger',
timeout: 6000,
pos: 'bottom-right'
});
this.showLoading = false;
}
handleSuccessfulSave(message) {
this.showLoading = false;
// this.successfulSaveMessage = message;
UIkit.notification(message, {
status: 'success',
timeout: 6000,
pos: 'bottom-right'
});
}
changeNotify(notification: any, checked: boolean, index: number) {
notification.notify = checked;
this.change();
}
changeFrequency() {
this.change();
}
}