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 {
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 router: Router,
39
    private authService: AuthenticationService,
40
    private piwikService: PiwikService,
41
    private repoService: RepositoryService
42
  ) {}
43

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

    
49
  getRepo(): void {
50
    let id = this.route.snapshot.paramMap.get('id');
51
    this.loadingMessage = loadingRepoMessage;
52
    this.repoService.getRepositoryById(id).subscribe(
53
      repo => {
54
        this.repo = repo;
55
      },
56
      error => {
57
        console.log(error);
58
        this.errorMessage = loadingRepoError;
59
        this.loadingMessage = '';
60
      }, () => {
61
        if (this.repo) {
62
          this.getOAid();
63
        }
64
        this.loadingMessage = '';
65
        if ( this.authService.getUserEmail() !== this.repo.registeredBy ) {
66
          this.router.navigateByUrl('/403-forbidden', { skipLocationChange: true });
67
        }
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
      error => console.log(`ERROR is ${error}`)
78
    );
79
  }
80

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

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

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