Project

General

Profile

1
import { Component, OnInit, ViewChild } from '@angular/core';
2
import { ActivatedRoute } 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 {
8
  enabledMetricsError, enabledMetricsSuccess, enablingMetrics, loadingRepoError,
9
  loadingRepoMessage
10
} from '../../domain/shared-messages';
11
import { AuthenticationService } from '../../services/authentication.service';
12

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

    
18
export class MetricsEnableComponent implements OnInit {
19
  successMessage: string;
20
  errorMessage: string;
21
  loadingMessage: string;
22

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

    
28
  modalTitle = "Confirmation";
29
  modalButton = "Yes, enable it";
30
  isModalShown: boolean;
31

    
32
  @ViewChild('confirmEnablingModal')
33
  public confirmEnablingModal: ConfirmationDialogComponent;
34

    
35

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

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

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

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

    
77
  confirmEnabling() {
78
    if (this.repo) {
79
      this.confirmEnablingModal.showModal();
80
    }
81
  }
82

    
83
  confirmedEnabling(event : any) {
84
    if (this.repo) {
85
      this.loadingMessage = enablingMetrics;
86
      let piwik: PiwikInfo = {
87
        repositoryId: this.repo.id,
88
        openaireId: this.oaId,
89
        repositoryName: this.repo.officialName,
90
        country: this.repo.countryName,
91
        siteId: '',
92
        authenticationToken: this.authenticationToken,
93
        creationDate: null,
94
        requestorName: this.authService.getUserName(),
95
        requestorEmail: this.authService.getUserEmail(),
96
        validated: false,
97
        validationDate: null,
98
        comment: ''
99
      };
100
      /*siteId: `${this.analyticsUrl}siteName=${encodeURIComponent(this.repo.officialName)}&url=${encodeURIComponent(this.repo.websiteUrl)}`,*/
101

    
102
      /*this.piwikService.savePiwikInfo(piwik).subscribe(*/
103
      this.piwikService.enableMetricsForRepository(this.repo.officialName,this.repo.websiteUrl,piwik).subscribe(
104
        response => {
105
          console.log(`savePiwikInfo answered: ${response}`);
106
          this.successMessage = enabledMetricsSuccess;
107
          this.loadingMessage = '';
108
        },
109
        error => {
110
          console.log(error);
111
          this.errorMessage = enabledMetricsError;
112
          this.loadingMessage = '';
113
        }
114
      );
115
    }
116
  }
117
}
(2-2/10)