Project

General

Profile

1
import { Component, Input, OnInit } from '@angular/core';
2
import { Repository } from '../../domain/typeScriptClasses';
3
import { RepositoryService } from '../../services/repository.service';
4
import { AuthenticationService } from '../../services/authentication.service';
5
import { loadingReposMessage, loadingUserRepoInfoEmpty, reposRetrievalError } from '../../domain/shared-messages';
6

    
7
@Component ({
8
  selector: 'repository-tiles',
9
  templateUrl: 'repository-tiles.component.html'
10
})
11

    
12
export class RepositoryTilesComponent implements OnInit {
13
  reposOfUser: Repository[] = [];
14
  tilesView: boolean;
15
  errorMessage: string;
16
  warningMessage: string;
17
  loadingMessage: string;
18

    
19
  @Input() parent: string = '';
20

    
21
  constructor(private authService: AuthenticationService,
22
              private repoService: RepositoryService) {}
23

    
24
  ngOnInit() {
25
    this.tilesView = true;
26
    this.getReposOfUser();
27
  }
28

    
29
  getReposOfUser(): void {
30
    this.loadingMessage = loadingReposMessage;
31
    this.repoService.getRepositoriesOfUser(this.authService.getUserEmail())
32
      .subscribe(
33
        repos => this.reposOfUser = repos.sort( function(a, b) {
34
          if (a.officialName < b.officialName) {
35
            return -1;
36
          } else if (a.officialName > b.officialName) {
37
            return 1;
38
          } else {
39
            return 0;
40
          }
41
        }),
42
        error => {
43
          console.log(error);
44
          this.loadingMessage = '';
45
          this.errorMessage = reposRetrievalError;
46
          },
47
        () => {
48
          this.loadingMessage = '';
49
          if (!this.reposOfUser || !this.reposOfUser.length) {
50
            this.warningMessage = loadingUserRepoInfoEmpty;
51
          }
52
        }
53
      );
54
  }
55

    
56
  getLinkToNext(repo: Repository): string {
57
    if (this.parent === 'metrics') {
58
      if (repo.piwikInfo) {
59
        if (repo.piwikInfo.validated === true) {
60
          return `show_metrics/${repo.id}`;
61
        } else if (repo.piwikInfo.validated === false) {
62
          return `instructions/${repo.id}`;
63
        }
64
      } else {
65
        return `enable/${repo.id}`;
66
      }
67
    } else if (this.parent === 'sourcesUpdate' || this.parent === 'compatibilityMonitor') {
68
      return repo.id;
69
    }
70
  }
71

    
72
  getBadgeCSS(repo: Repository): string {
73
    if (this.parent === 'metrics') {
74
      if (repo.piwikInfo) {
75
        if (repo.piwikInfo.validated === true) {
76
          return 'uk-badge uk-badge-success';
77
        } else if (repo.piwikInfo.validated === false) {
78
          return 'uk-badge uk-badge-warning';
79
        }
80
      } else {
81
        return 'uk-badge uk-badge-danger';
82
      }
83
    }
84
  }
85

    
86
  getBadgeText(repo: Repository): string {
87
    if (this.parent === 'metrics') {
88
      if (repo.piwikInfo) {
89
        if (repo.piwikInfo.validated === true) {
90
          return 'enabled';
91
        } else if ( repo.piwikInfo.validated === false ) {
92
          return 'enabling in progress';
93
        }
94
      } else {
95
        return 'not enabled';
96
      }
97
    }
98
  }
99

    
100
  toggleTiles() {
101
    this.tilesView = !this.tilesView;
102
  }
103

    
104
}
(7-7/8)