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
import {Meta, Title} from "@angular/platform-browser";
8
import {UserManagementService} from "../services/user-management.service";
9

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

    
23
export class OrcidComponent {
24
  public   subscriptions: Subscription[] = [];
25

    
26
  public showLoading: boolean = false;
27
  public message: string = "";
28
  public orcidMessage: string = "";
29

    
30
  public source: string = "";
31
  public code: string = "";
32
  public gotToken: boolean = false;
33

    
34
  public routerHelper:RouterHelper = new RouterHelper();
35

    
36
  constructor(private route: ActivatedRoute,
37
              private _router: Router,
38
              private orcidService: OrcidService,
39
              private userManagementService: UserManagementService,
40
              private _meta: Meta, private _title: Title) {}
41

    
42
  ngOnInit() {
43
    var description = "Openaire, ORCID";
44
    this.updateTitle("Connect with ORCID");
45
    this.updateDescription(description);
46
    this.updateUrl( properties.domain + properties.baseLink + this.route.url);
47

    
48
    this.subscriptions.push(this.route.queryParams.subscribe(params => {
49
      this.gotToken = false;
50

    
51
      this.source = params['source'];
52
      this.code = params['code'];
53
      if (this.code) {
54
        if(this.source == "openaire") {
55
          this.getToken(params['code']);
56
        } else {
57
          this.getPersonalDetails();
58
        }
59
      } else if(params['error']) {
60
        this.showLoading = false;
61
        this.orcidMessage = params['error_description'];
62
        this.message = "<div>An error occured while trying to grant access OpenAIRE. </div>" +
63
          "<div>Please close this window and try again!</div>";
64
      } else {
65
        this.message = "No code provided to connect your ORCID with OpenAIRE. Please try again!"
66
      }
67
    }));
68
  }
69

    
70
  ngOnDestroy() {
71
    this.subscriptions.forEach(subscription => {
72
      if (subscription instanceof Subscriber) {
73
        subscription.unsubscribe();
74
      }
75
    });
76
  }
77

    
78
  // the following method uses client ID and client Secret, which are sessitive data.
79
  // Our API should return the response, without revealing the call to ORCID.
80
  private getToken(code: string) {
81
    this.showLoading = true;
82
    this.orcidService.getToken(code).subscribe(
83
      gotTokens => {
84
        this.gotToken = true;
85
        if(gotTokens == null || gotTokens['value'] == false) {
86
          this.showLoading = false;
87
          this.message = "<div>An error occured while trying to connect your ORCID iD with OpenAIRE. Please try again!</div>" +
88
            "<div class='uk-margin-small-top'>Need help? <a href='https://www.openaire.eu/support/helpdesk'>Contact us!</a></div>";
89
        } else {
90
          if(this.source == "openaire") {
91
            this.message = "<div>Thank you for connecting your ORCID iD with OpenAIRE!</div>" +
92
              "<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>";
93
            if(window && window.opener) {
94
              window.opener.postMessage("success");
95
              window.close();
96
            }
97
            setTimeout(() => {
98
              this.message += "<div class='uk-margin-top'>If this widnow does not close authomatically, please close it and continue!</div>";
99
            }, 3000);
100
          } else {
101
            this.message = "<div>Thank you for connecting your ORCID iD with OpenAIRE!</div>" +
102
              "<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>";
103

    
104
            this.getPersonalDetails();
105
          }
106
          // this.message = "Thank you for connecting your ORCID iD with OpenAIRE! Please close this window and continue!";
107
        }
108
        this.showLoading = false;
109
      },
110
      error => {
111
        this.showLoading = false;
112

    
113
        this.gotToken = true;
114

    
115
        console.error("Error getting token from code: "+code, error);
116
        this.message = "An error occured while trying to connect your ORCID iD with OpenAIRE. Please try again!";
117
      }
118
    )
119
  }
120

    
121
  private getPersonalDetails() {
122
    //get author name
123
    this.subscriptions.push(this.orcidService.getPersonalDetails().subscribe(
124
      details => {
125
        let author: string = "";
126

    
127
        if(details && details['name']) {
128
          let name: string = details['name'];
129
          if(name['given-names'] && name['given-names']['value']) {
130
            author = name['given-names']['value'];
131
          }
132
          if(name['family-name'] && name['family-name']['value']) {
133
            author += (author ? " " : "") + name['family-name']['value'];
134
          }
135
        }
136

    
137
        let params = this.routerHelper.createQueryParams(['f0', 'fv0'], ['resultauthor', author]);
138
        this._router.navigate([properties.searchLinkToAdvancedResults], {queryParams: params});
139
      },
140
      error => {
141
        console.error("Error getting personal details", error);
142
        if(this.gotToken) {
143
          this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
144
            if (user) {
145
              let params = this.routerHelper.createQueryParams(['f0', 'fv0'], ['resultauthor', user.fullname]);
146
              this._router.navigate([properties.searchLinkToAdvancedResults], {queryParams: params});
147
            } else {
148
              this._router.navigate([properties.searchLinkToAdvancedResults], {});
149
            }
150
          },
151
          error => {
152
            setTimeout(() => {
153
              this.message += "<div class='uk-margin-top'>If you are not authomatically redirected, please navigate to our search pages.</div>";
154
            }, 3000);
155
          }));
156
        } else {
157
          this.getToken(this.code);
158
        }
159
      }
160
    ));
161
  }
162

    
163
  private updateTitle(title: string) {
164
    this._title.setTitle(title);
165
    this._meta.updateTag({content: title}, "property='og:title'");
166
  }
167

    
168
  private updateDescription(description: string) {
169
    this._meta.updateTag({content: description}, "name='description'");
170
    this._meta.updateTag({content: description}, "property='og:description'");
171
  }
172

    
173
  private updateUrl(url: string) {
174
    this._meta.updateTag({content: url}, "property='og:url'");
175
  }
176
}
(2-2/5)