diff --git a/connect/userEmailPreferences/mailPrefs.component.html b/connect/userEmailPreferences/mailPrefs.component.html new file mode 100644 index 00000000..f8b2d33c --- /dev/null +++ b/connect/userEmailPreferences/mailPrefs.component.html @@ -0,0 +1,63 @@ +
+
+
+
+
+ User Email Preferences +
+ +
+ User session is not valid. Please login again. +
+ + +
    + + +
  • +
    Email preferences for {{preferencesFor}}: {{notification.openaireName}}
    +
    +
    + +
    +
    Do you want to receive e-mail notifications?
    +
    + + +
    +
    + + +
    + +
    + +
    + +
    + +
    + + +
    + + + +
    + +
    +
  • +
+ + +
+
+
+
diff --git a/connect/userEmailPreferences/mailPrefs.component.ts b/connect/userEmailPreferences/mailPrefs.component.ts new file mode 100644 index 00000000..7b3b0564 --- /dev/null +++ b/connect/userEmailPreferences/mailPrefs.component.ts @@ -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 : 'Your email preferences for '+this.notifications[index].openaireName+' have been successfully changed', + 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 : 'No changes selected for '+this.notifications[index].openaireName+' email preferences', + 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; + //} + } +} diff --git a/connect/userEmailPreferences/mailPrefs.service.ts b/connect/userEmailPreferences/mailPrefs.service.ts new file mode 100644 index 00000000..68aabf89 --- /dev/null +++ b/connect/userEmailPreferences/mailPrefs.service.ts @@ -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 => 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 => 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'); + } +} diff --git a/connect/userEmailPreferences/mailsPrefs.module.ts b/connect/userEmailPreferences/mailsPrefs.module.ts new file mode 100644 index 00000000..dc4e1ae3 --- /dev/null +++ b/connect/userEmailPreferences/mailsPrefs.module.ts @@ -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 { } diff --git a/utils/errorMessages.component.ts b/utils/errorMessages.component.ts index 019082a7..cebb5eed 100644 --- a/utils/errorMessages.component.ts +++ b/utils/errorMessages.component.ts @@ -29,6 +29,9 @@ import {ErrorCodes} from './properties/errorCodes';
Changes could not be saved
+
You are not allowed to access this page +
` }) diff --git a/utils/properties/errorCodes.ts b/utils/properties/errorCodes.ts index 899273f5..20a0b377 100644 --- a/utils/properties/errorCodes.ts +++ b/utils/properties/errorCodes.ts @@ -7,4 +7,5 @@ export class ErrorCodes { public OUT_OF_BOUND = 5; public NOT_FOUND = 6; public NOT_SAVED = 7; + public FORBIDDEN = 8; }