1. Add userEmailPreferences page, where user chooses for every openaireId, for which he/she is a manager, IF and HOW OFTEN prefers to receive email notifications.

2. errorCodes.ts & errorMessages.component: Add 'FORBIDDEN' errorCode and appropriate errorMessage.


git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@53231 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
konstantina.galouni 2018-09-24 10:08:33 +00:00
parent 5fe8c67e43
commit e775921012
6 changed files with 345 additions and 0 deletions

View File

@ -0,0 +1,63 @@
<div id="tm-main" class=" uk-section uk-margin-small-top tm-middle">
<div uk-grid uk-grid>
<div class="tm-main uk-width-1-1@s uk-width-1-1@m uk-width-1-1@l uk-row-first">
<div class="uk-container uk-margin-top">
<div class="uk-article-title custom-article-title">
User Email Preferences
</div>
<div *ngIf="userValidMessage.length > 0 " class = "uk-alert uk-alert-danger " >
User session is not valid. Please login again.
</div>
<errorMessages [status]="[status]" [type]="'notification preferences'"></errorMessages>
<ul class="custom-accordion" uk-accordion>
<!-- class="uk-open" -->
<!-- uk-card uk-card-default uk-card-body -->
<li *ngFor="let notification of notifications; let i=index" [class]="i==0 ? 'uk-open' : ''">
<div class="uk-accordion-title">Email preferences for {{preferencesFor}}: <strong>{{notification.openaireName}}</strong></div>
<div class="uk-accordion-content">
<form class="uk-form-horizontal"><!-- [formGroup]="myForm"-->
<!--[ngClass]="{'has-error':!myForm.controls.notify.valid && myForm.controls.notify.dirty}"-->
<div class="uk-margin">
<div class="uk-form-label">Do you want to receive e-mail notifications?</div>
<div class="uk-form-controls uk-form-controls-text">
<label>Yes <input class="uk-radio" type="radio" name="radio2" [checked]="notification.notify" (click)="changeNotify(notification, true)"></label>
<label>No <input class="uk-radio" type="radio" name="radio2" [checked]="!notification.notify" (click)="changeNotify(notification, false)"></label>
</div>
</div>
<!-- [ngClass]="{'has-error':!myForm.controls.frequency.valid && myForm.controls.frequency.dirty}" -->
<div class="uk-margin" *ngIf="notification.notify">
<label class="uk-form-label" for="form-horizontal-select">How often?</label>
<div class="uk-form-controls">
<select class="uk-select" id="form-horizontal-select" class="uk-select" [(ngModel)]="notification.frequency" name="select_frequency">
<option [ngValue]="24" >Daily</option>
<option [ngValue]="48" >Every two days</option>
<option [ngValue]="168">Weekly</option>
</select>
</div>
<!-- (click)="notifications[i].frequency = 168" -->
</div>
<div class="uk-float-right">
<button type="submit" class="uk-button" (click)="restoreNotification(notifications, i)">Reset</button>
<button type="submit" class="uk-button uk-button-primary" (click)="saveNotification(notifications, i)">Save Changes</button>
</div>
<!-- <span class="uk-margin">
<button class="uk-width-1-1 uk-button uk-button-primary uk-button-small" type="submit" (click)="saveNotification(notifications, i)">Save</button>
<button class="uk-width-1-1 uk-button uk-button-small" (click)="restoreNotification(notifications, i)">Restore</button>
</span> -->
<!-- {{notification.notify}} -- {{notification.frequency}} -->
</form>
</div>
</li>
</ul>
<!-- {{notifications | json}} -->
</div>
</div>
</div>
</div>

View File

@ -0,0 +1,200 @@
import {Component, ViewChild, Input} from '@angular/core';
import {Location} from '@angular/common';
import {Observable} from 'rxjs/Observable';
import {ActivatedRoute, Router} from '@angular/router';
import {ModalLoading} from '../../utils/modal/loading.component';
import {AlertModal} from '../../utils/modal/alert';
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';
declare var UIkit: any;
@Component({
selector: 'mailPrefs',
templateUrl: 'mailPrefs.component.html',
providers:[MailPrefsService]
})
export class MailPrefsComponent {
properties:EnvProperties;
sub: any;
public communityId: string;
public preferencesFor: string = "community";
public status: number;
public notifications = [];
public initialNotifications = [];
public showErrorMessage:boolean = false;
//public showForbiddenMessage:boolean = false;
public userValidMessage:string = "";
public fetchId:string;
private errorCodes: ErrorCodes;
constructor (private _mailPrefsService: MailPrefsService, private route: ActivatedRoute, private _router:Router, private location: Location) {
this.errorCodes = new ErrorCodes();
this.status = this.errorCodes.LOADING;
}
ngOnInit() {
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
});
this.sub = this.route.queryParams.subscribe(params => {
this.communityId = params['communityId'];
if(!this.communityId){
this.communityId = ConnectHelper.getCommunityFromDomain(document.location.hostname);
}
this.fetchId = Session.getUserEmail();
console.info("email: "+this.fetchId);
console.info("communityId: " + this.communityId);
this.getEmailPreferences();
});
}
getEmailPreferences() {
if(!Session.isLoggedIn()){
this.userValidMessage = "User session has expired. Please login again.";
}else{
this.status = this.errorCodes.LOADING;
if(this.communityId && this.communityId != "openaire") {
this.preferencesFor = "community";
this._mailPrefsService.getUserEmailPreferencesForCommunity(this.communityId, this.properties.claimsAPIURL).subscribe(
data => {
if(data.code == "204") {
this.status = this.errorCodes.NONE;
} else {
this.initialNotifications = data.data;
this.notifications = this.initialNotifications;
this.status = this.errorCodes.DONE;
}
},
err => {
this.handleErrors(err);
}
);
} else {
this.preferencesFor = "project";
this._mailPrefsService.getUserEmailPreferencesForOpenaire(this.properties.claimsAPIURL).subscribe(
data => {
console.info("email prefs returned");
if(data.code == "204") {
this.status = this.errorCodes.NONE;
} else {
console.info(data);
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);
}
);
}
}
}
changeNotify(notification: any, checked: boolean) {
notification.notify = checked;
}
saveNotification(notifications: any, index: number) {
if(JSON.stringify(this.notifications[index]) != JSON.stringify(this.initialNotifications[index])) {
if(!Session.isLoggedIn()){
this.userValidMessage = "User session has expired. Please login again.";
}else{
this.status = this.errorCodes.LOADING;
console.info("Send notification to db: ", this.notifications[index]);
this._mailPrefsService.saveUserEmailPreferences(this.notifications[index], this.properties.claimsAPIURL).subscribe(
data => {
console.info("Notification saved successfully");
this.initialNotifications[index] = JSON.parse(JSON.stringify( this.notifications[index] ));
this.status = this.errorCodes.DONE;
UIkit.notification({
message : '<strong>Your email preferences for '+this.notifications[index].openaireName+' have been successfully changed<strong>',
status : 'success',
timeout : 3000,
pos : 'top-center'
});
},
err => {
console.log(err);
this.status = this.errorCodes.NOT_SAVED;
}
);
}
} else {
console.info("Notification not changed: ", this.notifications[index]);
UIkit.notification({
message : '<strong>No changes selected for '+this.notifications[index].openaireName+' email preferences<strong>',
status : 'primary',
timeout : 3000,
pos : 'top-center'
});
}
}
restoreNotification(notifications: any, index: number) {
this.notifications[index] = JSON.parse(JSON.stringify( this.initialNotifications[index] ));
}
ngOnDestroy() {
if(this.sub) {
this.sub.unsubscribe();
}
}
handleErrors(err){
//this.showErrorMessage = true;
//try{
var error = err.json()
//var code = error.code;
console.info(err);
var code = error.code;
console.info(code);
if(code == "403") {
this.status = this.errorCodes.FORBIDDEN;
}
else if(code == "204") {
this.status = this.errorCodes.NONE;
} else if(code == "404") {
this.status = this.errorCodes.NOT_FOUND;
} else if(code == "500") {
this.status = this.errorCodes.ERROR;
} else {
this.status = this.errorCodes.NOT_AVAILABLE;
}
//}catch (e) {
//console.log("Couldn't parse answer as json")
//this.showErrorMessage = true;
//}
}
}

View File

@ -0,0 +1,56 @@
import {Injectable} from '@angular/core';
import {Http, RequestOptions, Headers, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
//import { HttpErrorResponse } from '@angular/common/http';
import {EnvProperties} from '../../utils/properties/env-properties';
import {CustomOptions} from '../../claims/claim-utils/service/customOptions.class';
@Injectable()
export class MailPrefsService {
constructor(private http: Http ) {}
getUserEmailPreferencesForCommunity (communityId:string, apiUrl:string):any {
let url = apiUrl +"users/notification"+"?communityId="+communityId;
return this.http.get(url, CustomOptions.getAuthOptions())
.map(request => <any> request.json())
.do(request => console.info("Get user email preferences for communityId= "+communityId ));
//.catch(this.handleError);
}
getUserEmailPreferencesForOpenaire (apiUrl:string):any {
let url = apiUrl +"users/notification";
//var error: HttpErrorResponse = new HttpErrorResponse({"error": {"code": 204}, "status" : 204}); // doesn't work
var response: Response = new Response({"body" : {"code":200, "data": []}, "status": 200, "headers": null, "url": "", "merge": null});
return this.http.get(url, CustomOptions.getAuthOptions())
//.timeoutWith(100, Observable.throw(response)) // goes to error
//.timeoutWith(100, Observable.of(response)) // goes to
.do(request => console.info(request))
.map(request => <any> request.json())
.do(request => console.info("Get user email preferences for OpenAIRE" ));
//.catch(this.handleError);
}
saveUserEmailPreferences (notification: any, apiUrl: string) {
let url = apiUrl +"users/notification/save";
let body = JSON.stringify( notification );
console.warn('Json body: : '+body);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post(url, body, CustomOptions.getAuthOptionsWithBody())
.map(res => res.json())
.do(request => console.info("Insert Response:"+request.status) );
//.catch(this.handleError);
}
private handleError (error: Response) {
// in a real world app, we may send the error to some remote logging infrastructure
// instead of just logging it to the console
console.log(error);
return Observable.throw(error || 'Server error');
}
}

View File

@ -0,0 +1,22 @@
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';
@NgModule({
imports: [
CommonModule, RouterModule, FormsModule, ReactiveFormsModule, ErrorMessagesModule
],
declarations: [
MailPrefsComponent
],
providers:[MailPrefsService],
exports: [
MailPrefsComponent
]
})
export class MailPrefsModule { }

View File

@ -29,6 +29,9 @@ import {ErrorCodes} from './properties/errorCodes';
<div *ngIf="status.some(checkErroCode(errorCodes.NOT_SAVED))"
[class]="(tab_error_class ? 'uk-margin-top' : 'uk-animation-fade') + ' uk-alert uk-alert-warning'" role="alert">Changes could not be saved
</div>
<div *ngIf="status.some(checkErroCode(errorCodes.FORBIDDEN))"
[class]="(tab_error_class ? 'uk-margin-top' : 'uk-animation-fade') + 'uk-alert uk-alert-danger'" role="alert">You are not allowed to access this page
</div>
`
})

View File

@ -7,4 +7,5 @@ export class ErrorCodes {
public OUT_OF_BOUND = 5;
public NOT_FOUND = 6;
public NOT_SAVED = 7;
public FORBIDDEN = 8;
}