Project

General

Profile

1 52768 myrto.kouk
import { Component, OnInit } from '@angular/core';
2 52995 myrto.kouk
import { ActivatedRoute, Router } from '@angular/router';
3 52768 myrto.kouk
import {AggregationDetails, Repository} from '../../domain/typeScriptClasses';
4
import { RepositoryService } from '../../services/repository.service';
5
import {
6
  loadingAggregationHistory,
7
  loadingAggregationHistoryError,
8
  loadingRepoError,
9
  loadingRepoMessage,
10
  noAggregationHistory
11
} from '../../domain/shared-messages';
12 52995 myrto.kouk
import { AuthenticationService } from '../../services/authentication.service';
13 52768 myrto.kouk
14
@Component ({
15
  selector: 'app-compatibility-fullHistory-monitor-repo',
16
  templateUrl: 'compatibility-monitor-fullHistory-repo.component.html'
17
})
18
19
export class CompatibilityMonitorFullHistoryRepoComponent implements OnInit {
20
  loadingMessage: string;
21
  errorMessage: string;
22
  noAggregations: string;
23
24
  repoId: string = '';
25
  repoName: string = '';
26
  repo: Repository;
27
28
  aggregationsMap: Map<string,AggregationDetails[]> = new Map<string,AggregationDetails[]>();
29
  years: string[] = [];
30
31
  constructor(private route: ActivatedRoute,
32 52995 myrto.kouk
              private router: Router,
33
              private repoService: RepositoryService,
34
              private authService: AuthenticationService) {}
35 52768 myrto.kouk
36
  ngOnInit() {
37
    this.readRepoId();
38
    this.getRepo();
39
  }
40
41
  readRepoId() {
42
    this.repoId = this.route.snapshot.paramMap.get('id');
43
    this.repoName = `repository with id \'${this.repoId}\'`;
44
  }
45
46
  getRepo(){
47
    if (this.repoId) {
48
      this.loadingMessage = loadingRepoMessage;
49
      this.repoService.getRepositoryById(this.repoId).subscribe(
50
        repo => {
51
          this.repo = repo;
52
        },
53
        error => {
54
          console.log(error);
55
          this.loadingMessage = '';
56
          this.errorMessage = loadingRepoError;
57
        },
58
        () => {
59
          this.loadingMessage = '';
60
          if (this.repo) {
61
            this.repoName = this.repo.officialName;
62 52995 myrto.kouk
            if ( this.authService.getUserEmail() !== this.repo.registeredBy ) {
63
              this.router.navigateByUrl('/403-forbidden', { skipLocationChange: true });
64
            } else {
65
              this.getAllAggregationHistory();
66
            }
67 52768 myrto.kouk
          } else {
68
            this.errorMessage = loadingRepoError;
69
          }
70
        }
71
      );
72
    }
73
  }
74
75
  getAllAggregationHistory() {
76
    this.loadingMessage = loadingAggregationHistory;
77
    this.repoService.getRepositoryAggregationsByYear(this.repo.id).subscribe(
78
      aggr => this.aggregationsMap = aggr,
79
      error => {
80
        this.loadingMessage = '';
81
        this.errorMessage = loadingAggregationHistoryError;
82
      },
83
      () => {
84
        this.loadingMessage = '';
85
        for (let key in this.aggregationsMap) {
86
          this.years.push(key);
87
        }
88
        if ( this.years.length === 0 ) {
89
          this.noAggregations = noAggregationHistory;
90 52995 myrto.kouk
        } else {
91
          this.years.sort( (a, b)  => ( a > b ? -1 : 1 ) );
92 52768 myrto.kouk
        }
93
      }
94
    );
95
  }
96
97
}