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";
import {RouterHelper} from "../utils/routerHelper.class";
@Component({
selector: 'orcid',
template: `
`
})
export class OrcidComponent {
public subscriptions: Subscription[] = [];
public showLoading: boolean = false;
public message: string = "";
public orcidMessage: string = "";
public source: string = "";
public routerHelper:RouterHelper = new RouterHelper();
constructor(private route: ActivatedRoute,
private _router: Router,
private orcidService: OrcidService) {}
ngOnInit() {
this.subscriptions.push(this.route.queryParams.subscribe(params => {
this.source = params['source'];
if (params['code']) {
this.getToken(params['code']);
} else if(params['error']) {
this.showLoading = false;
this.orcidMessage = params['error_description'];
this.message = "An error occured while trying to grant access OpenAIRE.
" +
"Please close this window and try again!
";
} 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(
gotTokens => {
if(gotTokens == null || gotTokens['value'] == false) {
this.showLoading = false;
this.message = "An error occured while trying to connect your ORCID iD with OpenAIRE. Please try again!
" +
"";
} else {
if(this.source == "openaire") {
this.message = "Thank you for connecting your ORCID iD with OpenAIRE!
" +
"This window will automatically close and you will be ready to link OpenAIRE research results with your ORCID iD.
";
if(window && window.opener) {
window.opener.postMessage("success");
window.close();
}
setTimeout(() => {
this.message += "If this widnow does not close authomatically, please close it and continue!
";
}, 3000);
} else {
this.message = "Thank you for connecting your ORCID iD with OpenAIRE!
" +
"You will automatically be redirected to our advanced search page where you can link OpenAIRE research results with your ORCID iD.
";
//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);
this._router.navigate([properties.searchLinkToAdvancedResults], {});
},
() => {
setTimeout(() => {
this.message += "If you are not authomatically redirected, please navigate to our search pages.
";
}, 3000);
}
));
}
// this.message = "Thank you for connecting your ORCID iD with OpenAIRE! Please close this window and continue!";
}
},
error => {
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;
}
)
}
}