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 { 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
  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 authService: AuthenticationService,
37
    private piwikService: PiwikService,
38
    private repoService: RepositoryService
39
  ) {}
40

    
41
  ngOnInit() {
42
    this.getRepo();
43
    this.isModalShown = false;
44
  }
45

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

    
66
  getOAid () {
67
    this.piwikService.getOpenaireId(this.repo.id).subscribe(
68
      id => {this.oaId = id._body.toString(); console.log(this.oaId);},
69
      error => console.log(`ERROR is ${error}`)
70
    );
71
  }
72

    
73
  confirmEnabling() {
74
    if (this.repo) {
75
      this.confirmEnablingModal.showModal();
76
    }
77
  }
78

    
79
  confirmedEnabling() {
80
    if (this.repo) {
81
      this.loadingMessage = enablingMetrics;
82
      this.piwikService.savePiwikInfo(this.repo.id,
83
        this.oaId,
84
        this.repo.officialName,
85
        this.repo.countryName,
86
        this.authService.userFullName,
87
        this.authService.userEmail).subscribe(
88
        response => {
89
          console.log(`answered ${response}`);
90
          this.successMessage = enabledMetricsSuccess;
91
          this.loadingMessage = '';
92
        },
93
        error => {
94
          console.log(error);
95
          this.errorMessage = enabledMetricsError;
96
          this.loadingMessage = '';
97
        }
98
      );
99
    }
100
  }
101
}
(2-2/10)