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
    let body = document.getElementsByTagName('body')[0];
46
    body.classList.remove("top_bar_active");   //remove the class
47
    body.classList.remove("page_heading_active");
48
    body.classList.remove("landing");
49
    body.classList.add("dashboard");
50
  }
51

    
52
  getRepo(): void {
53
    const id = this.route.snapshot.paramMap.get('id');
54
    this.loadingMessage = loadingRepoMessage;
55
    this.repoService.getRepositoryById(id).subscribe(
56
      repo => {
57
        this.repo = repo;
58
      },
59
      error => {
60
        console.log(error);
61
        this.errorMessage = loadingRepoError;
62
        this.loadingMessage = '';
63
      }, () => {
64
        if (this.repo) {
65
          this.getOAid();
66
        }
67
        this.loadingMessage = '';
68
      }
69
    );
70
  }
71

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

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

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

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