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"; import {Session} from "../login/utils/helper.class"; import {LoginErrorCodes} from "../login/utils/guardHelper.class"; import {ExternalIDV3_0, WorkV3_0} from "./orcidWork"; import {EnvProperties} from "../utils/properties/env-properties"; 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; } ' + '.delete-orcid-button { background-color: #fff; color: #ff3d33; border: 1px solid #ff3d33 }' + '.delete-orcid-button:hover { color: #ff0d00; border-color: #ff0d00; } ' + ' a:not(:hover) .orcid-color { color: #a6ce39; }' + '.loading-action-button { width: 36px; height: 36px; }' + '.orcid-button { background-color: #fff; border: 1px solid #a6ce39; } ' + '.orcid-button:hover { background-color: #e0f0d5; } ' + 'a.orcid_icon_opacity:hover > .uk-icon { opacity: 0.8; } ' ], selector: 'orcid-work', template: ` Add to  ORCID  orcid{{" "}} Delete from  ORCID  {{" "}} add orcid
{{requestGrantMessage}}
ORCID   ACTIONS View ORCID work Update ORCID work Add to ORCID Delete from ORCID
{{work.title?.title?.value}}
{{work.title?.subtitle?.value}}
{{work['journal-title']?.value}}
{{work['publication-date']?.year?.value}} - {{work['publication-date']?.month?.value}} - {{work['publication-date']?.day?.value}} | {{work['type']}}

{{work['short-description']}}

{{contributor['credit-name']?.value}} ( {{contributor['contributor-attributes']['contributor-role']}} , {{contributor['contributor-attributes']['contributor-sequence']}} , {{contributor['contributor-orcid']?.path}} )
This research outcome is the result of merged research outcomes in OpenAIRE.

You have already added {{this.putCodes?.length}} works in your ORCID record related to the merged research outcome.
If you continue with delete action, all these works will be deleted.
If you continue with update action, all these works will be deleted and a new merged work will be added instead.
` }) export class OrcidWorkComponent { @Input() resultId: string = ""; @Input() resultTitle: string = ""; @Input() resultLandingInfo: ResultLandingInfo; @Input() pids: string = ""; @Input() identifiers: Map; @Input() type: string; @Input() putCodes: string[] = []; @Input() creationDates: string[] = []; @Input() updateDates: string[] = []; @Input() givenPutCode: boolean = false; @Input() pageType: string = "search"; public subscriptions: Subscription[] = []; @ViewChild('workModal') workModal; @ViewChild('saveWorkModal') saveWorkModal; @ViewChild('grantModal') grantModal; @ViewChild('messageModal') messageModal; @ViewChild('propagationModal') propagationModal; public requestGrant: boolean = false; public requestGrantMessage: string = "Please grant OpenAIRE to access and update your ORCID works."; private tokenUrl: string; public message: string = ""; public showLoading: boolean = false; public works: any[] = []; public orcidWorks: any[] = []; public window: any; public isLoggedIn: boolean = Session.isLoggedIn(); public currentAction: string = ""; public properties: EnvProperties = properties; constructor(private route: ActivatedRoute, private _router: Router, private orcidService: OrcidService, private resultLandingService: ResultLandingService) { if(typeof document !== 'undefined') { this.tokenUrl = properties.orcidTokenURL + "client_id="+properties.orcidClientId // + "&response_type=code&scope=/activities/update" // + "&response_type=code&scope=/authenticate /activities/update /person/update /read-limited" + "&response_type=code&scope=/activities/update /read-limited" + "&redirect_uri="+location.origin+"/orcid?source=openaire"; } } ngOnInit() { if(!this.givenPutCode && this.isLoggedIn) { this.getPutCode(); } } ngOnDestroy() { this.subscriptions.forEach(subscription => { if (subscription instanceof Subscriber) { // console.debug("subscription closing..."); subscription.unsubscribe(); } }); } public showUpdateDatesInTooltip() { const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; let response: string = ""; if(this.updateDates != null && this.updateDates.length > 0) { response += "

Last update in ORCID
"; for(let i=0; i { let identifiersMap: Map = new Map(); for(let identifier of identifiers) { if(!identifiersMap.has(identifier['external-id-type'])) { identifiersMap.set(identifier['external-id-type'], new Array()); } identifiersMap.get(identifier['external-id-type']).push(identifier['external-id-value']); } return identifiersMap; } openGrantWindow() { if(!Session.isLoggedIn()){ //this.userValidMessage = "User session has expired. Please login again."; this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} }); } else { // this.tokenUrl = location.origin+"/orcid"; this.window = window.open(this.tokenUrl, '_blank', 'location=yes,height=700,width=540,left=500,top=100,scrollbars=yes,status=yes'); // this.requestGrant = false; this.closeGrantModal(); let self = this; window.onmessage = function (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' }); if (self.currentAction == "add") { self.saveWorkPreparation(); } else if (self.currentAction == "delete") { self.deleteWorks(); } else if(self.currentAction == "update") { self.updateWorkPreparation(); } }; } } private getPutCode() { this.subscriptions.push(this.orcidService.getPutCode(this.pids).subscribe( putCodes => { this.putCodes = putCodes; }, error => { } )) } private getResultLandingInfo(action: string) { 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(); } if(action == "save") { this.saveWork(); } else if (action == "update") { this.updateWork(); } }, error => { this.handleError(error); console.error("Error getting landing info: ", error); } )) } private saveWorkPreparation() { if(!Session.isLoggedIn()){ //this.userValidMessage = "User session has expired. Please login again."; this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} }); } else { if (this.requestGrant) { this.openGrantModal("Add, delete or edit work in your ORCID record"); } else { this.showLoading = true; if (this.resultLandingInfo) { this.saveWork(); } else { this.getResultLandingInfo("save"); } } } } private saveWork() { this.subscriptions.push(this.orcidService.saveWork(this.resultLandingInfo, this.pids).subscribe( response => { // for testing only // this.openGrantModal("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); console.error("Error posting landing info: null"); } else { this.putCodes.push(""+response['put-code']); this.creationDates.push(response['created-date']['value']); this.updateDates.push(response['last-modified-date']['value']); // this.closeGrantModal(); // this.message = "You have successfully added work with pids: "+this.pids+" in your ORCID record!"; this.message = "You have successfully added work \""+this.resultTitle+"\" in your ORCID record!"; // this.openMessageModal("Work added successfully"); // message: 'You have successfully added work with pids: '+this.pids+' in your ORCID record!', UIkit.notification({ message: 'You have successfully added work "'+this.resultTitle+'" in your ORCID record!' // + // '

Manager your ORCID links' , // '

Manager your ORCID links', // '

Manager your ORCID links', status: 'success', timeout: 6000, pos: 'bottom-right' }); this.showLoading = false; } }, error => { this.handleError(error); console.error("Error posting landing info", error); } )); } goToOrcidLinksPage() { this._router.navigate([this.properties.myOrcidLinksPage]); } private updateWorkPreparation() { if(!Session.isLoggedIn()){ this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} }); } else { if (this.requestGrant) { this.openGrantModal("Add, delete or edit work in your ORCID record"); } else if(this.putCodes.length > 1) { this.openPropagationModal("Update ORCID work"); } else { this.showLoading = true; if (this.resultLandingInfo) { this.updateWork(); } else { this.getResultLandingInfo("update"); } } } } private updateWork() { this.subscriptions.push(this.orcidService.updateWork(this.resultLandingInfo, this.pids, this.putCodes[0]).subscribe( response => { if(response) { this.updateDates[0] = response['last-modified-date'].value; // message: 'You have successfully updated work with pids: ' + this.pids + ' in your ORCID record!', UIkit.notification({ message: 'You have successfully updated work "' + this.resultTitle + '" in your ORCID record!' // + // '

Manager your ORCID links' , status: 'success', timeout: 6000, pos: 'bottom-right' }); } this.showLoading = false; }, error => { this.handleError(error); console.error("Error updating landing info", error); } )); } public confirmedPropagation() { this.propagationModal.cancel(); this.deleteWorks(true); //} // if(this.currentAction == "update") { // this.saveWork(); // } } private getOrcidWorks() { if(!Session.isLoggedIn()){ this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} }); } else { this.showLoading = true; this.subscriptions.push(this.orcidService.getOrcidWorks(this.putCodes).subscribe( (response: WorkV3_0[]) => { this.orcidWorks = response; this.openWorkModal(); this.showLoading = false; }, error => { this.handleError(error); console.error("Error getting work", error); } )); } } private deleteWorks(confirmed: boolean = false) { if(!Session.isLoggedIn()){ this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} }); } else { if (this.requestGrant) { this.openGrantModal("Add, delete or edit work in your ORCID record"); } else if(this.putCodes.length > 1 && !confirmed) { this.openPropagationModal("Delete ORCID work"); } else { this.showLoading = true; if (!this.resultLandingInfo && this.identifiers) { let pidsArray: string[] = []; for (let key of Array.from(this.identifiers.keys())) { pidsArray = pidsArray.concat(this.identifiers.get(key)); } this.pids = pidsArray.join(); } this.subscriptions.push(this.orcidService.deleteWorks(this.putCodes).subscribe( 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.putCodes.splice(i, 1); this.creationDates.splice(i, 1); this.updateDates.splice(i, 1); // this.works.splice(i, 1); } } } else { deletedAll = false; } if (!deletedAll) { this.handleError(null); console.error("Error deleting landing info: null"); } else { if(this.currentAction == "update") { this.saveWork(); } else { // this.closeGrantModal(); // this.message = "You have successfully deleted work with pids: " + this.pids + " from your ORCID record!"; this.message = "You have successfully deleted work \"" + this.resultTitle + "\" from your ORCID record!"; // this.openMessageModal("Work deleted successfully"); // message: 'You have successfully deleted work with pids: ' + this.pids + ' from your ORCID record!', UIkit.notification({ message: 'You have successfully deleted work "' + this.resultTitle + '" from your ORCID record!' // + // '

Manager your ORCID links' , status: 'success', timeout: 6000, pos: 'bottom-right' }); this.showLoading = false; } } }, error => { this.handleError(error); console.error("Error deleting work", error); } )); } } } openGrantModal(title: string) { this.grantModal.cancelButton = false; this.grantModal.okButton = false; this.grantModal.alertTitle = title; this.grantModal.open(); } closeGrantModal() { this.grantModal.cancel(); } openWorkModal() { this.workModal.cancelButton = false; this.workModal.okButton = false; this.workModal.alertTitle = "Work in your ORCID record"; this.workModal.open(); } 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(); } openPropagationModal(title: string) { this.propagationModal.cancelButton = false; this.propagationModal.okButton = false; this.propagationModal.alertTitle = title; this.propagationModal.open(); } closePropagationModal() { this.propagationModal.cancel(); } handleError(error) { if(error && error.status == "401") { this.openGrantModal("Add, delete or 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.openGrantModal("Add, delete or edit work in your ORCID record"); this.requestGrant = true; this.requestGrantMessage = "Please login again to ORCID." // this.openGrantModal(); } else { this.message = ""; if(error && error.status == "409") { this.message = "There is already a work in your ORCID record with the same information of the work you are trying to add now.

"; } if(this.currentAction == "get") { // this.message += "There was an error getting work with pids: " + this.pids + " from your ORCID record.
Please try again later."; this.message += "There was an error getting work \"" + this.resultTitle + "\" from your ORCID record.
Please try again later."; } else if(this.currentAction == "add") { // this.message += "There was an error adding work with pids: "+this.pids+" to your ORCID record.
Please try again later."; this.message += "There was an error adding work with pids: \""+this.resultTitle+"\" to your ORCID record.
Please try again later."; } else if(this.currentAction == "update") { // this.message += "There was an error updating work with pids: "+this.pids+" to your ORCID record.
Please try again later."; this.message += "There was an error updating work with pids: \""+this.resultTitle+"\" to your ORCID record.
Please try again later."; } else if(this.currentAction == "delete") { // this.message += "There was an error deleting work with pids: "+this.pids+" from your ORCID record.
Please try again later."; this.message += "There was an error deleting work with pids: \""+this.resultTitle+"\" from your ORCID record.
Please try again later."; } else { this.message += "There was an error. Please try again later." } UIkit.notification({ message: this.message, status: 'danger', timeout: 6000, pos: 'bottom-right' }); // this.openMessageModal("An error occured"); } this.showLoading = false; } }