Project

General

Profile

1
import { Component } from '@angular/core';
2
import {ActivatedRoute, Router} from "@angular/router";
3
import {Subscriber, Subscription} from "rxjs";
4
import {OrcidService} from "./orcid.service";
5
import {properties} from "../../../environments/environment";
6
import {RouterHelper} from "../utils/routerHelper.class";
7

    
8
@Component({
9
  selector: 'orcid',
10
  template: `    
11
    <div class="uk-section uk-container">
12
      <div *ngIf="orcidMessage">{{orcidMessage}}</div>
13
      <div *ngIf="message" [innerHTML]="message"></div>
14
      <div *ngIf="showLoading" class="uk-animation-fade uk-margin-top  uk-width-1-1" role="alert">
15
        <span class="loading-gif  uk-align-center"></span>
16
      </div>
17
    </div>
18
  `
19
})
20

    
21
export class OrcidComponent {
22
  public   subscriptions: Subscription[] = [];
23

    
24
  public showLoading: boolean = false;
25
  public message: string = "";
26
  public orcidMessage: string = "";
27

    
28
  public source: string = "";
29

    
30
  public routerHelper:RouterHelper = new RouterHelper();
31

    
32
  constructor(private route: ActivatedRoute,
33
              private _router: Router,
34
              private orcidService: OrcidService) {}
35

    
36
  ngOnInit() {
37
    this.subscriptions.push(this.route.queryParams.subscribe(params => {
38
      this.source = params['source'];
39
      if (params['code']) {
40
        this.getToken(params['code']);
41
      } else if(params['error']) {
42
        this.showLoading = false;
43
        this.orcidMessage = params['error_description'];
44
        this.message = "<div>An error occured while trying to grant access OpenAIRE. </div>" +
45
          "<div>Please close this window and try again!</div>";
46
      } else {
47
        this.message = "No code provided to connect your ORCID with OpenAIRE. Please try again!"
48
      }
49
    }));
50
  }
51

    
52
  ngOnDestroy() {
53
    this.subscriptions.forEach(subscription => {
54
      if (subscription instanceof Subscriber) {
55
        subscription.unsubscribe();
56
      }
57
    });
58
  }
59

    
60
  // the following method uses client ID and client Secret, which are sessitive data.
61
  // Our API should return the response, without revealing the call to ORCID.
62
  private getToken(code: string) {
63
    this.showLoading = true;
64
    this.orcidService.getToken(code).subscribe(
65
      gotTokens => {
66
        if(gotTokens == null || gotTokens['value'] == false) {
67
          this.showLoading = false;
68
          this.message = "<div>An error occured while trying to connect your ORCID iD with OpenAIRE. Please try again!</div>" +
69
            "<div class='uk-margin-small-top'>Need help? <a href='https://www.openaire.eu/support/helpdesk'>Contact us!</a></div>";
70
        } else {
71
          if(this.source == "openaire") {
72
            this.message = "<div>Thank you for connecting your ORCID iD with OpenAIRE!</div>" +
73
              "<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>";
74
            if(window && window.opener) {
75
              window.opener.postMessage("success");
76
              window.close();
77
            }
78
            setTimeout(() => {
79
              this.message += "<div class='uk-margin-top'>If this widnow does not close authomatically, please close it and continue!</div>";
80
            }, 3000);
81
          } else {
82
            this.message = "<div>Thank you for connecting your ORCID iD with OpenAIRE!</div>" +
83
              "<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>";
84

    
85
            //get author name
86
            this.subscriptions.push(this.orcidService.getPersonalDetails().subscribe(
87
              details => {
88
                let author: string = "";
89

    
90
                if(details && details['name']) {
91
                  let name: string = details['name'];
92
                  if(name['given-names'] && name['given-names']['value']) {
93
                    author = name['given-names']['value'];
94
                  }
95
                  if(name['family-name'] && name['family-name']['value']) {
96
                    author += (author ? " " : "") + name['family-name']['value'];
97
                  }
98
                }
99

    
100
                let params = this.routerHelper.createQueryParams(['f0', 'fv0'], ['resultauthor', author]);
101
                this._router.navigate([properties.searchLinkToAdvancedResults], {queryParams: params});
102
              },
103
              error => {
104
                console.error("Error getting personal details", error);
105
                this._router.navigate([properties.searchLinkToAdvancedResults], {});
106
              },
107
              () => {
108
                setTimeout(() => {
109
                  this.message += "<div class='uk-margin-top'>If you are not authomatically redirected, please navigate to our search pages.</div>";
110
                }, 3000);
111
              }
112
            ));
113
          }
114
          // this.message = "Thank you for connecting your ORCID iD with OpenAIRE! Please close this window and continue!";
115
        }
116
      },
117
      error => {
118
        console.error("Error getting token from code: "+code, error);
119
        this.message = "An error occured while trying to connect your ORCID iD with OpenAIRE. Please try again!";
120
      },
121
      () => {
122
        this.showLoading = false;
123
      }
124
    )
125
  }
126
}
(2-2/5)