From 3b2e9ebbee09c3f507230ca768fc59cd6d8fa6f1 Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Tue, 5 Jul 2022 11:49:34 +0300 Subject: [PATCH] Revert mailPrefs files used by explore. --- .../mailPrefs.component.html | 55 ++++ .../mailPrefs.component.ts | 259 ++++++++++++++++++ .../userEmailPreferences/mailsPrefs.module.ts | 23 ++ 3 files changed, 337 insertions(+) create mode 100644 connect/userEmailPreferences/mailPrefs.component.html create mode 100644 connect/userEmailPreferences/mailPrefs.component.ts create mode 100644 connect/userEmailPreferences/mailsPrefs.module.ts diff --git a/connect/userEmailPreferences/mailPrefs.component.html b/connect/userEmailPreferences/mailPrefs.component.html new file mode 100644 index 00000000..4bbe55a2 --- /dev/null +++ b/connect/userEmailPreferences/mailPrefs.component.html @@ -0,0 +1,55 @@ + +
+
+ User Email Preferences for Claims Notifications +
+ + + + + + +
+ + +
+ +
+
Notify + for new user links:
+ + +
+ +
+
Frequency:
+
+ +
+
+ +
+ + +
+ +
+ +
+ + + +
+ diff --git a/connect/userEmailPreferences/mailPrefs.component.ts b/connect/userEmailPreferences/mailPrefs.component.ts new file mode 100644 index 00000000..6243387f --- /dev/null +++ b/connect/userEmailPreferences/mailPrefs.component.ts @@ -0,0 +1,259 @@ +import {Component, Input} from '@angular/core'; +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'; +import {ErrorMessagesComponent} from '../../utils/errorMessages.component'; +import {LoginErrorCodes} from '../../login/utils/guardHelper.class'; +import {properties} from "../../../../environments/environment"; +import {Subscriber} from "rxjs"; + +declare var UIkit: any; + +@Component({ + selector: 'mailPrefs', + templateUrl: 'mailPrefs.component.html', + providers:[MailPrefsService] + +}) +export class MailPrefsComponent { + properties:EnvProperties; + subscriptions = []; + @Input() communityId: string; + public preferencesFor: string = "community"; + public status: number; + + public notifications = []; + public initialNotifications = []; + public prefsChanged = {}; + public hidden: boolean = true; + + //public showForbiddenMessage:boolean = false; + public userValidMessage:string = ""; + public savedMessage: string = ""; + + private errorCodes: ErrorCodes; + private errorMessages: ErrorMessagesComponent; + + @Input() showSaveResetButtons: boolean = true; + + constructor (private _mailPrefsService: MailPrefsService, private route: ActivatedRoute, private _router:Router, private location: Location) { + this.errorCodes = new ErrorCodes(); + this.errorMessages = new ErrorMessagesComponent(); + this.status = this.errorCodes.LOADING; + } + + ngOnInit() { + this.properties = properties; + this.subscriptions.push(this.route.params.subscribe(params => { + this.hidden = true; + console.debug(this.communityId); + if(!this.communityId){ + this.communityId = ConnectHelper.getCommunityFromDomain(this.properties.domain); + } + if(!this.communityId) { + this.communityId = params['community']; + } + console.debug(this.communityId, params) + this.getEmailPreferences(); + })); + + + } + + getEmailPreferences() { + 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"; + this.subscriptions.push(this._mailPrefsService.getUserEmailPreferencesForCommunity(this.communityId, this.properties.claimsAPIURL).subscribe( + data => { + if(data.code == "204") { + this.status = this.errorCodes.NONE; + this.initialNotifications = [{notify: true, frequency:24, openaireId: this.communityId}]; + } else { + this.initialNotifications = data.data; + } + this.notifications = JSON.parse(JSON.stringify( this.initialNotifications )); + + this.status = this.errorCodes.DONE; + this.hidden = false; + + }, + err => { + this.hidden = false; + this.handleErrors(err); + this.handleError("Error getting user email preferences for community with id: "+this.communityId, err); + } + )); + } else { + this.preferencesFor = "project"; + this.subscriptions.push(this._mailPrefsService.getUserEmailPreferencesForOpenaire(this.properties.claimsAPIURL).subscribe( + data => { + + if(data.code == "204") { + this.status = this.errorCodes.NONE; + } else { + + 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; + + this.status = this.errorCodes.DONE; + } + }, + err => { + //console.info(err); + this.handleErrors(err); + this.handleError("Error getting user email preferences for openaire", err); + } + )); + } + } + } + + changeNotify(notification: any, checked: boolean, 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 { + this.savedMessage = ""; + this.status = this.errorCodes.DONE; + notification.notify = checked; + this.prefsChanged[index] = true; + } + } + + changeFrequency(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 { + this.savedMessage = ""; + this.status = this.errorCodes.DONE; + if(this.initialNotifications[index].frequency != this.notifications[index].frequency) { + this.prefsChanged[index] = true; + } + } + } + + saveNotification(index: number) { + if(this.notifications.length > 0 && this.initialNotifications.length > 0) { + 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(JSON.stringify(this.notifications[index]) != JSON.stringify(this.initialNotifications[index])) { + + this.status = this.errorCodes.LOADING; + this.savedMessage = ""; + + this.subscriptions.push(this._mailPrefsService.saveUserEmailPreferences(this.notifications[index], this.properties.claimsAPIURL).subscribe( + data => { + this.initialNotifications[index] = JSON.parse(JSON.stringify( this.notifications[index] )); + + this.status = this.errorCodes.DONE; + + /*UIkit.notification({ + message : 'Your email preferences for '+this.notifications[index].openaireName+' have been successfully changed', + 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; + } + )); + } + else { + /*UIkit.notification({ + message : 'No changes selected for '+this.notifications[index].openaireName+' email preferences', + status : 'primary', + timeout : 3000, + pos : 'top-center' + });*/ + this.savedMessage = "Notification settings for claims saved!"; + } + } + } + } + + 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; + } + } + } + /* + prefsChanged(index: number) : boolean { + if(this.notifications.length > 0 && this.initialNotifications.length > 0) { + if(JSON.stringify(this.notifications[index]) != JSON.stringify(this.initialNotifications[index])) { + return true; + } + } + return false; + } + */ + ngOnDestroy() { + this.subscriptions.forEach(subscription => { + if (subscription instanceof Subscriber) { + subscription.unsubscribe(); + } + }); + } + + + handleErrors(err){ + //this.showErrorMessage = true; + //try{ + var code = ""; + if(!err.status) { + var error = err.json(); + code = error.code; + } else { + code = err.status; + } + this.status = this.errorMessages.getErrorCode(code); + } + + private handleError(message: string, error) { + console.error("User mail notification preferences Page (for claims): "+message, error); + } + + + //}catch (e) { + //console.log("Couldn't parse answer as json") + //this.showErrorMessage = true; + //} +} diff --git a/connect/userEmailPreferences/mailsPrefs.module.ts b/connect/userEmailPreferences/mailsPrefs.module.ts new file mode 100644 index 00000000..f670212e --- /dev/null +++ b/connect/userEmailPreferences/mailsPrefs.module.ts @@ -0,0 +1,23 @@ +import { NgModule} from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { RouterModule } from '@angular/router'; +import { FormsModule, ReactiveFormsModule } from '@angular/forms'; + +import { MailPrefsComponent } from './mailPrefs.component'; +import { MailPrefsService } from './mailPrefs.service'; +import {ErrorMessagesModule} from '../../utils/errorMessages.module'; +import { MatSlideToggleModule } from '@angular/material/slide-toggle'; + +@NgModule({ + imports: [ + CommonModule, RouterModule, FormsModule, ReactiveFormsModule, ErrorMessagesModule, MatSlideToggleModule + ], + declarations: [ + MailPrefsComponent + ], + providers:[MailPrefsService], + exports: [ + MailPrefsComponent + ] +}) +export class MailPrefsModule { }