Project

General

Profile

1
import { Component, EventEmitter, Input, OnInit, Output } 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
  @Output() emitNoRepos: EventEmitter<boolean> = new EventEmitter<boolean>();
21

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

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

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

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

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

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

    
102
  toggleTiles() {
103
    this.tilesView = !this.tilesView;
104
  }
105

    
106
}
(8-8/9)