import {Component, Input, ViewChild} from '@angular/core'; import {ActivatedRoute, Router} from "@angular/router"; import {Subscriber, Subscription} from "rxjs"; import {OrcidService} from "./orcid.service"; import {ResultLandingInfo} from "../utils/entities/resultLandingInfo"; import {ResultLandingService} from "../landingPages/result/resultLanding.service"; import {properties} from "../../../environments/environment"; declare var UIkit: any; @Component({ styles: [ '.add-orcid-button { background-color: #a6ce39; color: #fff; border: 1px solid #a6ce39; } ' + '.add-orcid-button:hover { background-color: #b6d65c; border-color: #b6d65c; } ' + '.remove-orcid-button { background-color: #fff; color: #ff3d33; border: 1px solid #ff3d33 }' + '.remove-orcid-button:hover { color: #ff0d00; border-color: #ff0d00; } ' + ' a:not(:hover) .orcid-color { color: #a6ce39; }' ], selector: 'orcid-work', template: ` Add to ORCID Remove from ORCID Add to ORCID Remove from ORCID
{{requestGrantMessage}}
` }) export class OrcidWorkComponent { @Input() resultId: string = "dedup_wf_001::35c93a04a93b87f3a5954c1387aa7588"; @Input() resultLandingInfo: ResultLandingInfo; @Input() pids: string = ""; @Input() type: string; @Input() putCodes: string[]; @Input() givenPutCode: boolean = false; @Input() pageType: string = "search"; public subscriptions: Subscription[] = []; @ViewChild('workModal') workModal; @ViewChild('saveWorkModal') saveWorkModal; @ViewChild('grantModal') grantModal; @ViewChild('messageModal') messageModal; public requestGrant: boolean = false; public requestGrantMessage: string = "Please grant OpenAIRE to access and update your ORCID record and works."; private clientId: string = "APP-A5M3KTX6NCN67L91"; private tokenUrl: string; public message: string = ""; public showLoading: boolean = false; public works: any[] = []; public window: any; 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() { if(!this.givenPutCode) { this.getPutCode(); } } ngOnDestroy() { this.subscriptions.forEach(subscription => { if (subscription instanceof Subscriber) { console.debug("subscription closing..."); subscription.unsubscribe(); } }); } openGrantWindow() { // this.tokenUrl = location.origin+"/orcid"; this.window = window.open(this.tokenUrl, '_blank', 'location=yes,height=600,width=500,left=500,top=100,scrollbars=yes,status=yes'); // this.requestGrant = false; console.debug("before closing"); this.closeWorkModal(); console.debug("should be closed"); // var timer = setInterval(function() { // console.debug("check"); // if(this.window.closed) { // clearInterval(timer); // console.debug('closed'); // } // }, 1000); let self = this; window.onmessage = function (ev) { console.debug("onmessage", ev); if(ev.isTrusted && ev.origin !== location.origin && ev.data !== 'success') return; self.requestGrant = false; UIkit.notification({ message: 'Thank you for connecting your ORCID iD with OpenAIRE!', status: 'success', timeout: 6000, pos: 'bottom-right' }); self.saveWorkPreparation(); }; // window.addEventListener("message", (eventt) => { // // if(event.origin !== location.origin+"/orcid") // // return; // console.debug("EVEEEENT", eventt); // }, false); // this.window.onbeforeunload = function () { // console.debug("ONBEFOREUNLOAD!!!!"); // UIkit.notification({ // message: 'Thank you for connecting your ORCID iD with OpenAIRE!', // status: 'success', // timeout: 6000, // pos: 'bottom-right' // }); // } } private getPutCode() { console.debug(this.pids); this.subscriptions.push(this.orcidService.getPutCode(this.pids).subscribe( putCodes => { this.putCodes = putCodes; console.debug(this.putCodes); }, error => { } )) } private saveWorkPreparation() { if(this.requestGrant) { this.openWorkModal("Edit work in your ORCID record"); } else { this.showLoading = true; if (this.resultLandingInfo) { this.saveWork(); } else { this.subscriptions.push(this.resultLandingService.getResultLandingInfo(this.resultId, null, this.type, null, properties).subscribe( resultLandingInfo => { this.resultLandingInfo = resultLandingInfo; if (this.resultLandingInfo && this.resultLandingInfo.identifiers) { let pidsArray: string[] = []; for (let key of Array.from(this.resultLandingInfo.identifiers.keys())) { pidsArray = pidsArray.concat(this.resultLandingInfo.identifiers.get(key)); } this.pids = pidsArray.join(); } this.saveWork(); }, error => { this.handleError(error, "There was an error adding this work to your ORCID record. Please try again later."); console.error("Error getting landing info: ", error); } )) } } } private saveWork() { this.subscriptions.push(this.orcidService.saveWork(this.resultLandingInfo, this.pids).subscribe( response => { console.debug(response); // for testing only // this.openWorkModal("Add work in your ORCID record"); // this.requestGrant = true; // this.requestGrantMessage = "Please grant OpenAIRE to access and update your ORCID record and works."; if(response == null) { this.handleError(null, "There was an error adding this work to your ORCID record. Please try again later."); console.error("Error posting landing info: null"); } else { //this.work = this.orcidService.parse(this.resultLandingInfo); //this.work['put-code'] = response; this.works.push(response); this.putCodes.push(this.works[this.works.length - 1]['put-code']); // this.closeWorkModal(); this.message = "You have successfully added this work in your ORCID record!"; // this.openMessageModal("Work added successfully"); UIkit.notification({ message: 'You have successfully added this work in your ORCID record!', status: 'success', timeout: 6000, pos: 'bottom-right' }); this.showLoading = false; } }, error => { this.handleError(error, "There was an error adding this work to your ORCID record. Please try again later."); console.error("Error posting landing info", error); } )); } // private updateWork() { // this.subscriptions.push(this.orcidService.updateWork(this.resultLandingInfo, this.putCode).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.showLoading = true; this.subscriptions.push(this.orcidService.getWorks(this.pids).subscribe( response => { console.debug(response); this.works = response; if(this.works) { this.works.forEach(work => this.putCodes.push(work['put-code'])); } else { this.putCodes = []; } this.closeWorkModal(); this.showLoading = false; }, error => { this.handleError(error, "There was an error getting this work from your ORCID record. Please try again later."); console.error("Error getting work", error); } )); } private deleteWorks() { // let put_code;// = this.testingPutCode; // if(this.work && this.work['put-code']) { // put_code = this.work['put-code']; // } if(this.requestGrant) { this.openWorkModal("Edit work in your ORCID record"); } else { this.showLoading = true; this.subscriptions.push(this.orcidService.deleteWorks(this.putCodes).subscribe( deletedPutCodes => { console.debug(deletedPutCodes); let deletedAll: boolean = true; if (deletedPutCodes) { for (let i = 0; i < deletedPutCodes.length; i++) { let deletedPutCode = deletedPutCodes[i]; if (deletedPutCode == null) { deletedAll = false; } else { this.works.splice(i, 1); this.putCodes.splice(i, 1); } } } else { deletedAll = false; } if (!deletedAll) { this.handleError(null, "There was an error deleting this work from your ORCID record. Please try again later."); console.error("Error deleting landing info: null"); } else { // this.closeWorkModal(); this.message = "You have successfully deleted this work from your ORCID record!"; // this.openMessageModal("Work deleted successfully"); UIkit.notification({ message: 'You have successfully deleted this work from your ORCID record!', status: 'success', timeout: 6000, pos: 'bottom-right' }); this.showLoading = false; } }, error => { this.handleError(error, "There was an error deleting this work to your ORCID record. Please try again later."); console.error("Error deleting work", error); } )); } } openWorkModal(title: string) { this.workModal.cancelButton = false; this.workModal.okButton = false; this.workModal.alertTitle = title; this.workModal.open(); } closeWorkModal() { console.debug("closeWorkModal"); this.workModal.cancel(); } openMessageModal(title: string) { this.messageModal.cancelButton = false; this.messageModal.okButton = false; this.messageModal.alertTitle = title; this.messageModal.open(); } closeMessageModal() { this.messageModal.cancel(); } handleError(error, errorMsg: string) { if(error && error.status == "401") { this.openWorkModal("Edit work in your ORCID record"); this.requestGrant = true; this.requestGrantMessage = "Please grant OpenAIRE to access and update your ORCID record and works. " + "If you have already granted OpenAIRE, you just need to login again to ORCID!"; } else if(error && error.status == "403") { this.openWorkModal("Edit work in your ORCID record"); this.requestGrant = true; this.requestGrantMessage = "Please login again to ORCID." // this.openGrantModal(); } else { this.message = errorMsg; UIkit.notification({ message: ''+this.message+'', status: 'warning', timeout: 6000, pos: 'bottom-right' }); // this.openMessageModal("An error occured"); } this.showLoading = false; } }