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 {Country, PiwikInfo, Repository} from '../../../domain/typeScriptClasses';
7
import {
8
  enabledMetricsError, enabledMetricsSuccess, enablingMetrics,
9
  loadingRepoError, loadingRepoMessage, noServiceMessage
10
} from '../../../domain/shared-messages';
11
import { AuthenticationService } from '../../../services/authentication.service';
12
import { SharedService } from "../../../services/shared.service";
13

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

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

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

    
27
  repo: Repository;
28
  oaId: string;
29

    
30
  countries: Country[] = [];
31

    
32
  modalTitle = 'Confirmation';
33
  modalButton = 'Yes, enable it';
34
  isModalShown: boolean;
35

    
36
  @ViewChild('confirmEnablingModal', { static: true })
37
  public confirmEnablingModal: ConfirmationDialogComponent;
38

    
39

    
40
  constructor (
41
    private route: ActivatedRoute,
42
    private router: Router,
43
    private authService: AuthenticationService,
44
    private piwikService: PiwikService,
45
    private repoService: RepositoryService,
46
    private sharedService: SharedService
47
  ) {}
48

    
49
  ngOnInit() {
50

    
51
    if(this.sharedService.getRepository()) {
52
      this.repo = this.sharedService.getRepository();
53
      this.getOAid();
54
    }
55

    
56
    this.sharedService.repository$.subscribe(
57
      r => {
58
        this.repo = r;
59
        if (this.repo) {
60
          this.getOAid();
61
        }
62
      }
63
    );
64

    
65
    // this.getRepo();
66
    this.isModalShown = false;
67
    const body = document.getElementsByTagName('body')[0];
68
    body.classList.remove('top_bar_active');   // remove the class
69
    body.classList.remove('page_heading_active');
70
    body.classList.remove('landing');
71
    body.classList.add('dashboard');
72

    
73
    this.getCountries();
74
  }
75

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

    
96
  getOAid () {
97
    this.piwikService.getOpenaireId(this.repo.id).subscribe(
98
      id => {
99
        this.oaId = id;
100
        console.log(`getOpenaireId responded: ${this.oaId}`);
101
      },
102
      error => console.log(`ERROR is ${error}`)
103
    );
104
  }
105

    
106
  confirmEnabling() {
107
    if (this.repo) {
108
      this.confirmEnablingModal.showModal();
109
    }
110
  }
111

    
112
  confirmedEnabling(event: any) {
113
    if (this.repo) {
114
      this.loadingMessage = enablingMetrics;
115
      const piwik: PiwikInfo = {
116
        repositoryId: this.repo.id,
117
        openaireId: this.oaId,
118
        repositoryName: this.repo.officialname,
119
        country: this.getCountryName(this.repo.organizations[0].country),
120
        siteId: '',
121
        authenticationToken: this.authenticationToken,
122
        creationDate: null,
123
        requestorName: this.authService.getUserName(),
124
        requestorEmail: this.authService.getUserEmail(),
125
        validated: false,
126
        validationDate: null,
127
        comment: ''
128
      };
129

    
130
      this.piwikService.enableMetricsForRepository(this.repo.officialname, this.repo.websiteurl, piwik).subscribe(
131
        response => {
132
          console.log(`enableMetrics answered: ${response}`);
133
          this.successMessage = enabledMetricsSuccess;
134
          this.loadingMessage = '';
135

    
136
          // save piwik and update shareRepo
137
          this.repo.piwikInfo = piwik;
138
          this.sharedService.setRepository(this.repo);
139
        },
140
        error => {
141
          console.log(error);
142
          this.errorMessage = enabledMetricsError;
143
          this.loadingMessage = '';
144
        },
145
        () => {
146
          this.router.navigate([`../instructions/`]);
147
          // this.router.navigate([`/getImpact/instructions/${this.repo.id}`]);
148
        }
149
      );
150
    }
151
  }
152

    
153
  getCountries() {
154
    this.repoService.getCountries()
155
      .subscribe(
156
        countries => this.countries = countries.sort(function (a, b) {
157
          if (a.name < b.name) {
158
            return -1;
159
          } else if (a.name > b.name) {
160
            return 1;
161
          } else {
162
            return 0;
163
          }
164
        }),
165
        error => {
166
          this.loadingMessage = '';
167
          this.errorMessage = noServiceMessage;
168
          console.log(error);
169
        }
170
      );
171
  }
172

    
173
  getCountryName(countryCode): string {
174
    for (const country of Object.values(this.countries)) {
175
      if (country.code === countryCode) {
176
        return country.name;
177
      }
178
    }
179
  }
180

    
181
}
(2-2/15)