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
import { SharedService } from "../../../services/shared.service";
11

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

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

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

    
25
  repo: Repository;
26
  oaId: string;
27

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

    
32
  @ViewChild('confirmEnablingModal', { static: true })
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
    private sharedService: SharedService
43
  ) {}
44

    
45
  ngOnInit() {
46

    
47
    if(this.sharedService.getRepository()) {
48
      this.repo = this.sharedService.getRepository();
49
      this.getOAid();
50
    }
51

    
52
    this.sharedService.repository$.subscribe(
53
      r => {
54
        this.repo = r;
55
        if (this.repo) {
56
          this.getOAid();
57
        }
58
      }
59
    );
60

    
61
    // this.getRepo();
62
    this.isModalShown = false;
63
    let body = document.getElementsByTagName('body')[0];
64
    body.classList.remove("top_bar_active");   //remove the class
65
    body.classList.remove("page_heading_active");
66
    body.classList.remove("landing");
67
    body.classList.add("dashboard");
68
  }
69

    
70
  // getRepo(): void {
71
  //   const id = this.route.snapshot.paramMap.get('id');
72
  //   this.loadingMessage = loadingRepoMessage;
73
  //   this.repoService.getRepositoryById(id).subscribe(
74
  //     repo => {
75
  //       this.repo = repo;
76
  //     },
77
  //     error => {
78
  //       console.log(error);
79
  //       this.errorMessage = loadingRepoError;
80
  //       this.loadingMessage = '';
81
  //     }, () => {
82
  //       if (this.repo) {
83
  //         this.getOAid();
84
  //       }
85
  //       this.loadingMessage = '';
86
  //     }
87
  //   );
88
  // }
89

    
90
  getOAid () {
91
    this.piwikService.getOpenaireId(this.repo.id).subscribe(
92
      id => {
93
        this.oaId = id;
94
        console.log(`getOpenaireId responded: ${this.oaId}`);
95
      },
96
      error => console.log(`ERROR is ${error}`)
97
    );
98
  }
99

    
100
  confirmEnabling() {
101
    if (this.repo) {
102
      this.confirmEnablingModal.showModal();
103
    }
104
  }
105

    
106
  confirmedEnabling(event: any) {
107
    if (this.repo) {
108
      this.loadingMessage = enablingMetrics;
109
      const piwik: PiwikInfo = {
110
        repositoryId: this.repo.id,
111
        openaireId: this.oaId,
112
        repositoryName: this.repo.officialName,
113
        country: this.repo.countryName,
114
        siteId: '',
115
        authenticationToken: this.authenticationToken,
116
        creationDate: null,
117
        requestorName: this.authService.getUserName(),
118
        requestorEmail: this.authService.getUserEmail(),
119
        validated: false,
120
        validationDate: null,
121
        comment: ''
122
      };
123

    
124
      this.piwikService.enableMetricsForRepository(this.repo.officialName, this.repo.websiteUrl, piwik).subscribe(
125
        response => {
126
          console.log(`enableMetrics answered: ${response}`);
127
          this.successMessage = enabledMetricsSuccess;
128
          this.loadingMessage = '';
129

    
130
          //save piwik and update shareRepo
131
          this.repo.piwikInfo = piwik;
132
          this.sharedService.setRepository(this.repo);
133
        },
134
        error => {
135
          console.log(error);
136
          this.errorMessage = enabledMetricsError;
137
          this.loadingMessage = '';
138
        },
139
        () => {
140
          this.router.navigate([`../instructions/`]);
141
          // this.router.navigate([`/getImpact/instructions/${this.repo.id}`]);
142
        }
143
      );
144
    }
145
  }
146
}
(2-2/15)