Project

General

Profile

1
import { Component, OnInit, ViewChild } from '@angular/core';
2
import { ActivatedRoute, Router } from '@angular/router';
3
import { ConfirmationDialogComponent } from '../../shared/reusablecomponents/confirmation-dialog.component';
4
import { PiwikService } from '../../services/piwik.service';
5
import { RepositoryService } from '../../services/repository.service';
6
import { PiwikInfo, Repository } from '../../domain/typeScriptClasses';
7
import { enabledMetricsError, enabledMetricsSuccess, enablingMetrics,
8
         loadingRepoError, loadingRepoMessage } from '../../domain/shared-messages';
9
import { AuthenticationService } from '../../services/authentication.service';
10

    
11
@Component ({
12
  selector: 'metrics-enable',
13
  templateUrl: 'metrics-enable.component.html'
14
})
15

    
16
export class MetricsEnableComponent implements OnInit {
17
  successMessage: string;
18
  errorMessage: string;
19
  loadingMessage: string;
20

    
21
  readonly analyticsUrl = 'https://analytics.openaire.eu/addsite.php?';
22
  readonly authenticationToken = '32846584f571be9b57488bf4088f30ea';  /* THE ACTUAL TOKEN WILL BE NEEDED EVENTUALLY!! */
23
  repo: Repository;
24
  oaId: string;
25

    
26
  modalTitle = 'Confirmation';
27
  modalButton = 'Yes, enable it';
28
  isModalShown: boolean;
29

    
30
  @ViewChild('confirmEnablingModal')
31
  public confirmEnablingModal: ConfirmationDialogComponent;
32

    
33

    
34
  constructor (
35
    private route: ActivatedRoute,
36
    private router: Router,
37
    private authService: AuthenticationService,
38
    private piwikService: PiwikService,
39
    private repoService: RepositoryService
40
  ) {}
41

    
42
  ngOnInit() {
43
    this.getRepo();
44
    this.isModalShown = false;
45
  }
46

    
47
  getRepo(): void {
48
    const id = this.route.snapshot.paramMap.get('id');
49
    this.loadingMessage = loadingRepoMessage;
50
    this.repoService.getRepositoryById(id).subscribe(
51
      repo => {
52
        this.repo = repo;
53
      },
54
      error => {
55
        console.log(error);
56
        this.errorMessage = loadingRepoError;
57
        this.loadingMessage = '';
58
      }, () => {
59
        if (this.repo) {
60
          this.getOAid();
61
        }
62
        this.loadingMessage = '';
63
        if ( this.authService.activateFrontAuthorization && (this.authService.getUserEmail() !== this.repo.registeredBy.trim()) ) {
64
          this.router.navigateByUrl('/403-forbidden', { skipLocationChange: true });
65
        }
66
      }
67
    );
68
  }
69

    
70
  getOAid () {
71
    this.piwikService.getOpenaireId(this.repo.id).subscribe(
72
      id => {
73
        this.oaId = id;
74
        console.log(`getOpenaireId responded: ${this.oaId}`);
75
      },
76
      error => console.log(`ERROR is ${error}`)
77
    );
78
  }
79

    
80
  confirmEnabling() {
81
    if (this.repo) {
82
      this.confirmEnablingModal.showModal();
83
    }
84
  }
85

    
86
  confirmedEnabling(event: any) {
87
    if (this.repo) {
88
      this.loadingMessage = enablingMetrics;
89
      const piwik: PiwikInfo = {
90
        repositoryId: this.repo.id,
91
        openaireId: this.oaId,
92
        repositoryName: this.repo.officialName,
93
        country: this.repo.countryName,
94
        siteId: '',
95
        authenticationToken: this.authenticationToken,
96
        creationDate: null,
97
        requestorName: this.authService.getUserName(),
98
        requestorEmail: this.authService.getUserEmail(),
99
        validated: false,
100
        validationDate: null,
101
        comment: ''
102
      };
103

    
104
      this.piwikService.enableMetricsForRepository(this.repo.officialName, this.repo.websiteUrl, piwik).subscribe(
105
        response => {
106
          console.log(`enableMetrics answered: ${response}`);
107
          this.successMessage = enabledMetricsSuccess;
108
          this.loadingMessage = '';
109
        },
110
        error => {
111
          console.log(error);
112
          this.errorMessage = enabledMetricsError;
113
          this.loadingMessage = '';
114
        },
115
        () => {
116
          this.router.navigate([`/getImpact/instructions/${this.repo.id}`]);
117
        }
118
      );
119
    }
120
  }
121
}
(2-2/17)