2021-01-13 19:23:31 +01:00
import { Component } from '@angular/core' ;
import { ActivatedRoute , Router } from "@angular/router" ;
import { Subscriber , Subscription } from "rxjs" ;
import { OrcidService } from "./orcid.service" ;
import { properties } from "../../../environments/environment" ;
2021-02-10 10:01:55 +01:00
import { RouterHelper } from "../utils/routerHelper.class" ;
2021-02-20 10:23:59 +01:00
import { Meta , Title } from "@angular/platform-browser" ;
2021-01-13 19:23:31 +01:00
@Component ( {
selector : 'orcid' ,
2021-02-10 10:01:55 +01:00
template : `
2021-01-13 19:23:31 +01:00
< div class = "uk-section uk-container" >
< div * ngIf = "orcidMessage" > { { orcidMessage } } < / div >
< div * ngIf = "message" [ innerHTML ] = " message " > < / div >
< div * ngIf = "showLoading" class = "uk-animation-fade uk-margin-top uk-width-1-1" role = "alert" >
< span class = "loading-gif uk-align-center" > < / span >
< / div >
< / div >
`
} )
export class OrcidComponent {
public subscriptions : Subscription [ ] = [ ] ;
public showLoading : boolean = false ;
public message : string = "" ;
public orcidMessage : string = "" ;
2021-02-10 10:01:55 +01:00
public source : string = "" ;
2021-02-23 16:50:37 +01:00
public code : string = "" ;
public gotToken : boolean = false ;
2021-02-10 10:01:55 +01:00
public routerHelper :RouterHelper = new RouterHelper ( ) ;
2021-01-13 19:23:31 +01:00
constructor ( private route : ActivatedRoute ,
private _router : Router ,
2021-02-20 10:23:59 +01:00
private orcidService : OrcidService ,
private _meta : Meta , private _title : Title ) { }
2021-01-13 19:23:31 +01:00
ngOnInit() {
2021-02-20 10:23:59 +01:00
var description = "Openaire, ORCID" ;
this . updateTitle ( "Connect with ORCID" ) ;
this . updateDescription ( description ) ;
this . updateUrl ( properties . domain + properties . baseLink + this . route . url ) ;
2021-01-13 19:23:31 +01:00
this . subscriptions . push ( this . route . queryParams . subscribe ( params = > {
2021-02-23 16:50:37 +01:00
this . gotToken = false ;
2021-02-10 10:01:55 +01:00
this . source = params [ 'source' ] ;
2021-02-23 16:50:37 +01:00
this . code = params [ 'code' ] ;
if ( this . code ) {
if ( this . source == "openaire" ) {
this . getToken ( params [ 'code' ] ) ;
} else {
this . getPersonalDetails ( ) ;
}
2021-01-13 19:23:31 +01:00
} else if ( params [ 'error' ] ) {
this . showLoading = false ;
this . orcidMessage = params [ 'error_description' ] ;
this . message = "<div>An error occured while trying to grant access OpenAIRE. </div>" +
"<div>Please close this window and try again!</div>" ;
} else {
this . message = "No code provided to connect your ORCID with OpenAIRE. Please try again!"
}
} ) ) ;
}
ngOnDestroy() {
this . subscriptions . forEach ( subscription = > {
if ( subscription instanceof Subscriber ) {
subscription . unsubscribe ( ) ;
}
} ) ;
}
// the following method uses client ID and client Secret, which are sessitive data.
// Our API should return the response, without revealing the call to ORCID.
private getToken ( code : string ) {
this . showLoading = true ;
this . orcidService . getToken ( code ) . subscribe (
2021-02-10 10:01:55 +01:00
gotTokens = > {
2021-02-23 16:50:37 +01:00
this . gotToken = true ;
2021-02-10 10:01:55 +01:00
if ( gotTokens == null || gotTokens [ 'value' ] == false ) {
2021-01-13 19:23:31 +01:00
this . showLoading = false ;
2021-02-10 10:01:55 +01:00
this . message = "<div>An error occured while trying to connect your ORCID iD with OpenAIRE. Please try again!</div>" +
"<div class='uk-margin-small-top'>Need help? <a href='https://www.openaire.eu/support/helpdesk'>Contact us!</a></div>" ;
2021-01-13 19:23:31 +01:00
} else {
2021-02-10 10:01:55 +01:00
if ( this . source == "openaire" ) {
this . message = "<div>Thank you for connecting your ORCID iD with OpenAIRE!</div>" +
"<div class='uk-margin-small-top'>This window will automatically close and you will be ready to link OpenAIRE research results with your ORCID iD.</div>" ;
if ( window && window . opener ) {
window . opener . postMessage ( "success" ) ;
window . close ( ) ;
}
setTimeout ( ( ) = > {
this . message += "<div class='uk-margin-top'>If this widnow does not close authomatically, please close it and continue!</div>" ;
} , 3000 ) ;
} else {
this . message = "<div>Thank you for connecting your ORCID iD with OpenAIRE!</div>" +
"<div class='uk-margin-small-top'>You will automatically be redirected to our advanced search page where you can link OpenAIRE research results with your ORCID iD.</div>" ;
2021-02-23 16:50:37 +01:00
this . getPersonalDetails ( ) ;
2021-02-10 10:01:55 +01:00
}
// this.message = "Thank you for connecting your ORCID iD with OpenAIRE! Please close this window and continue!";
2021-01-13 19:23:31 +01:00
}
} ,
error = > {
2021-02-23 16:50:37 +01:00
this . gotToken = true ;
2021-01-13 19:23:31 +01:00
console . error ( "Error getting token from code: " + code , error ) ;
this . message = "An error occured while trying to connect your ORCID iD with OpenAIRE. Please try again!" ;
} ,
( ) = > {
this . showLoading = false ;
}
)
}
2021-02-20 10:23:59 +01:00
2021-02-23 16:50:37 +01:00
private getPersonalDetails() {
//get author name
this . subscriptions . push ( this . orcidService . getPersonalDetails ( ) . subscribe (
details = > {
let author : string = "" ;
if ( details && details [ 'name' ] ) {
let name : string = details [ 'name' ] ;
if ( name [ 'given-names' ] && name [ 'given-names' ] [ 'value' ] ) {
author = name [ 'given-names' ] [ 'value' ] ;
}
if ( name [ 'family-name' ] && name [ 'family-name' ] [ 'value' ] ) {
author += ( author ? " " : "" ) + name [ 'family-name' ] [ 'value' ] ;
}
}
let params = this . routerHelper . createQueryParams ( [ 'f0' , 'fv0' ] , [ 'resultauthor' , author ] ) ;
this . _router . navigate ( [ properties . searchLinkToAdvancedResults ] , { queryParams : params } ) ;
} ,
error = > {
console . error ( "Error getting personal details" , error ) ;
if ( this . gotToken ) {
this . _router . navigate ( [ properties . searchLinkToAdvancedResults ] , { } ) ;
} else {
this . getToken ( this . code ) ;
}
} ,
( ) = > {
if ( this . gotToken ) {
setTimeout ( ( ) = > {
this . message += "<div class='uk-margin-top'>If you are not authomatically redirected, please navigate to our search pages.</div>" ;
} , 3000 ) ;
}
}
) ) ;
}
2021-02-20 10:23:59 +01:00
private updateTitle ( title : string ) {
this . _title . setTitle ( title ) ;
this . _meta . updateTag ( { content : title } , "property='og:title'" ) ;
}
private updateDescription ( description : string ) {
this . _meta . updateTag ( { content : description } , "name='description'" ) ;
this . _meta . updateTag ( { content : description } , "property='og:description'" ) ;
}
private updateUrl ( url : string ) {
this . _meta . updateTag ( { content : url } , "property='og:url'" ) ;
}
2021-01-13 19:23:31 +01:00
}