openaire-library/orcid/orcid.component.ts

267 lines
13 KiB
TypeScript

import { Component } from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {Subscriber, Subscription} from "rxjs";
import {OrcidService} from "./orcid.service";
import {ResultLandingService} from "../landingPages/result/resultLanding.service";
import {ResultLandingInfo} from "../utils/entities/resultLandingInfo";
import {properties} from "../../../environments/environment";
@Component({
selector: 'orcid',
template: `
<!-- <div class="uk-section uk-container">-->
<!-- <div *ngIf="!testingUserRecord" class="uk-margin-small-top">Testing user ORCID iD: {{testingUserOrcid}}</div>-->
<!-- <div *ngIf="testingUserToken">-->
<!--&lt;!&ndash; <div>Testing user token: {{testingUserToken}}</div>&ndash;&gt;-->
<!-- <button (click)="getRecord()" type="submit"-->
<!-- class="uk-button uk-padding uk-padding-remove-vertical uk-margin-left uk-margin-small-top uk-button-primary">-->
<!-- <span>Get record</span>-->
<!-- </button>-->
<!-- <div *ngIf="testingUserRecord" class="uk-margin-small-top">{{testingUserRecord | json}}</div>-->
<!-- <button *ngIf="!work" (click)="getLanding()" type="submit"-->
<!-- class="uk-button uk-padding uk-padding-remove-vertical uk-margin-left uk-margin-small-top uk-button-primary">-->
<!-- <span>Get landing and post work</span>-->
<!-- </button>-->
<!--&lt;!&ndash; <div *ngIf="resultLandingInfo" class="uk-margin-small-top">{{resultLandingInfo | json}}</div>&ndash;&gt;-->
<!-- <button *ngIf="!work" (click)="getWork()" type="submit"-->
<!-- class="uk-button uk-padding uk-padding-remove-vertical uk-margin-left uk-margin-small-top uk-button-primary">-->
<!-- <span>Get work</span>-->
<!-- </button>-->
<!-- <div *ngIf="work" class="uk-margin-small-top">{{work | json}}</div>-->
<!-- <button *ngIf="work" (click)="deleteWork()" type="submit"-->
<!-- class="uk-button uk-padding uk-padding-remove-vertical uk-margin-left uk-margin-small-top uk-button-primary">-->
<!-- <span>Delete work</span>-->
<!-- </button>-->
<!--&lt;!&ndash; <button (click)="getWorks()" type="submit"&ndash;&gt;-->
<!--&lt;!&ndash; class="uk-button uk-padding uk-padding-remove-vertical uk-margin-left uk-margin-small-top uk-button-primary">&ndash;&gt;-->
<!--&lt;!&ndash; <span>Get works</span>&ndash;&gt;-->
<!--&lt;!&ndash; </button>&ndash;&gt;-->
<!--&lt;!&ndash; <div *ngIf="works" class="uk-margin-small-top">{{works | json}}</div>&ndash;&gt;-->
<!-- </div>-->
<!-- <div *ngIf="!testingUserToken" class="uk-flex uk-flex-middle">-->
<!-- No token for testing user found. You need to grant access OpenAIRE.-->
<!-- <a href="{{tokenUrl}}" type="submit"-->
<!-- class="uk-button uk-padding uk-padding-remove-vertical uk-margin-left uk-button-primary">-->
<!-- <span>Get code and token</span>-->
<!-- </a>-->
<!-- </div>-->
<!-- </div>-->
<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 {
private clientId: string = "APP-A5M3KTX6NCN67L91";
//Client secret: 96b20d71-ae06-4286-bb00-9172536c1ad4
public tokenUrl: string = "";//"https://sandbox.orcid.org/oauth/authorize?client_id="+this.clientId+"&response_type=code&scope=/authenticate&redirect_uri="+location.href;
public testingUserToken: any = "ac2e24e2-cd7e-4874-8cb8-d56395b9f3e2";// = "70a0274a-5b28-49dc-b6fd-2b427727a0da";// = "94634687-f5ea-4a0d-91ab-da8268287f72";
public testingUserOrcid: string = "0000-0001-9541-4617";
public testingUserRecord: any;
public resultId: string = "dedup_wf_001::35c93a04a93b87f3a5954c1387aa7588";
public testingPutCode: string = "1167642";
// invalid:
//code: gpq2pG
// {"access_token":"c4a81d38-4a32-4160-887d-9b6f39ce2d75","token_type":"bearer","refresh_token":"0ba698e9-f73b-40ca-a0c3-f7b8e1123f3c","expires_in":631138518,"scope":"/authenticate","name":"OpenAIRE OpenAIRE","orcid":"0000-0001-9541-4617"}
// {"access_token":"f4d8f230-fe1a-4d4e-af52-6ee67556ad2c","token_type":"bearer","refresh_token":"dc041582-33f4-476a-851d-f8d7e44de84c","expires_in":631138518,"scope":"/authenticate","name":"OpenAIRE OpenAIRE","orcid":"0000-0001-9541-4617"}
// {"access_token":"91eced53-0c44-4e5b-b49e-b32cc4548988","token_type":"bearer","refresh_token":"d9c35f21-1f67-4c1a-99af-fc445ead37c4","expires_in":631138518,"scope":"/authenticate","name":"OpenAIRE OpenAIRE","orcid":"0000-0001-9541-4617"}
// {"access_token":"c953699e-06bf-41a3-98a2-4ad3241a6592","token_type":"bearer","refresh_token":"75341fe0-26f6-41b8-85b0-3add39d7891e","expires_in":631138518,"scope":"/authenticate /read-limited /activities/update /person/update","name":"OpenAIRE OpenAIRE","orcid":"0000-0001-9541-4617"}
// {"access_token":"3ad4cadd-3429-4a6d-acc8-95fecf110917","token_type":"bearer","refresh_token":"ff0d26c1-6f42-4f25-8eae-51b84b83f0cc","expires_in":631138518,"scope":"/authenticate /read-limited /activities/update /person/update","name":"OpenAIRE OpenAIRE","orcid":"0000-0001-9541-4617"}
// {"access_token":"f77deae6-a8cd-4070-b409-eb459ed22cd1","token_type":"bearer","refresh_token":"70c6ae8f-0de6-4ebf-ac0e-409beda5d17f","expires_in":631138518,"scope":"/authenticate","name":"OpenAIRE OpenAIRE","orcid":"0000-0001-9541-4617"}
// {"access_token":"d5c7d36e-fdbb-44b4-bb4e-1364286ba73a","token_type":"bearer","refresh_token":"db85dc40-c8dc-4ed5-81de-e98fb8d35228","expires_in":631138518,"scope":"/authenticate /read-limited /activities/update /person/update","name":"OpenAIRE OpenAIRE","orcid":"0000-0001-9541-4617"}
// {"access_token":"94634687-f5ea-4a0d-91ab-da8268287f72","token_type":"bearer","refresh_token":"1d4c01f2-a1ef-4f01-8187-3fc26cbc5b78","expires_in":631138518,"scope":"/authenticate /read-limited /activities/update /person/update","name":"OpenAIRE OpenAIRE","orcid":"0000-0001-9541-4617"}
// {"access_token":"70a0274a-5b28-49dc-b6fd-2b427727a0da","token_type":"bearer","refresh_token":"b973c6fd-99b3-415e-af0c-6944500f8af3","expires_in":631138518,"scope":"/activities/update","name":"OpenAIRE OpenAIRE","orcid":"0000-0001-9541-4617"}
// {"access_token":"d1847264-a7e0-4f26-98e5-f193362ee7e2","token_type":"bearer","refresh_token":"2d1e1bfe-54d0-4e7c-984b-8af16609f6ae","expires_in":631138518,"scope":"/authenticate /read-limited /activities/update /person/update","name":"OpenAIRE OpenAIRE","orcid":"0000-0001-9541-4617"}
// {"access_token":"77c17e47-bbe7-4a05-a0e6-5c7920a8cc26","token_type":"bearer","refresh_token":"6ae41ee9-ef94-4f8b-bb08-a241e720d3c5","expires_in":631138518,"scope":"/authenticate","name":"OpenAIRE OpenAIRE","orcid":"0000-0001-9541-4617"}
public subscriptions: Subscription[] = [];
public resultLandingInfo: ResultLandingInfo;
public work: any;
public works: any;
public showLoading: boolean = false;
public message: string = "";
public orcidMessage: string = "";
constructor(private route: ActivatedRoute,
private _router: Router,
private orcidService: OrcidService,
private resultLandingService: ResultLandingService) {
if(typeof location !== undefined) {
console.debug(location);
this.tokenUrl = "https://sandbox.orcid.org/oauth/authorize?"
+ "client_id="+this.clientId
// + "&response_type=code&scope=/activities/update"
+ "&response_type=code&scope=/authenticate /activities/update /person/update /read-limited"
+ "&redirect_uri="+location.origin+"/orcid";
}
}
ngOnInit() {
this.subscriptions.push(this.route.queryParams.subscribe(params => {
if (params['code']) {
this.getToken(params['code']);
} 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) {
console.debug("subscription closing...");
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(
tokens => {
console.debug(tokens);
this.testingUserToken = tokens;
if(tokens == null) {
this.showLoading = false;
this.message = "An error occured while trying to connect your ORCID iD with OpenAIRE. Please try again!";
} else {
window.opener.postMessage("success");
window.close();
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;
}
)
}
private getRecord() {
//this.orcidService.getMyRecord(this.testingUserToken, this.testingUserOrcid).subscribe(
this.subscriptions.push(this.orcidService.getRecord().subscribe(
record => {
this.testingUserRecord = record;
console.debug(record);
},
error => {
console.error("Error getting record info for ORCID iD: "+this.testingUserOrcid + " with token: "+this.testingUserToken, error);
}
));
}
private getLanding() {
this.subscriptions.push(this.resultLandingService.getResultLandingInfo(this.resultId, null, "publication", null, properties).subscribe(
data => {
this.resultLandingInfo = data;
this.postWork();
console.debug(this.resultLandingInfo);
},
error => {
console.error("Error getting landing info for publication id: "+this.resultId, error);
}
));
}
private postWork() {
let pids: string[] = [];
if(this.resultLandingInfo && this.resultLandingInfo.identifiers) {
for (let key of Array.from(this.resultLandingInfo.identifiers.keys())) {
pids = pids.concat(this.resultLandingInfo.identifiers.get(key));
}
}
this.subscriptions.push(this.orcidService.saveWork(this.resultLandingInfo, pids.join()).subscribe(
response => {
console.debug(response);
//this.work = this.orcidService.parse(this.resultLandingInfo);
//this.work['put-code'] = response;
this.work = response;
},
error => {
console.error("Error posting landing info", error);
}
));
}
// private updateWork() {
// this.subscriptions.push(this.orcidService.updateWork(this.resultLandingInfo, this.testingUserOrcid, this.testingPutCode).subscribe(
// response => {
// console.debug(response);
// //this.work = this.orcidService.parse(this.resultLandingInfo);
// //this.work['put-code'] = response;
// this.work = response;
// },
// error => {
// console.error("Error updating landing info", error);
// }
// ));
// }
// private getWorks() {
// this.subscriptions.push(this.orcidService.getWorks().subscribe(
// response => {
// console.debug(response);
// this.works = response;
// },
// error => {
// console.error("Error getting works", error);
// }
// ));
// }
// private getWork() {
// this.subscriptions.push(this.orcidService.getWork(this.resultId).subscribe(
// response => {
// console.debug(response);
// this.work = response;
// },
// error => {
// console.error("Error getting work", error);
// }
// ));
// }
private deleteWork() {
let put_code = this.testingPutCode;
if(this.work && this.work['put-code']) {
put_code = this.work['put-code'];
}
this.subscriptions.push(this.orcidService.deleteWork(put_code).subscribe(
response => {
console.debug(response);
this.work = null;
},
error => {
console.error("Error deleting work", error);
}
));
}
}