/
repositories-list.component.ts
74 lines (66 loc) · 2.16 KB
/
repositories-list.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { Component, OnInit } from '@angular/core';
import { RepositoriesService } from '../shared/repositories.service';
import { Repository } from '../shared/repository.model';
import * as Fuse from 'fuse.js';
@Component({
selector: 'app-repositories-list',
templateUrl: './repositories-list.component.html',
styleUrls: ['./repositories-list.component.sass']
})
export class RepositoriesListComponent implements OnInit {
repos: Repository[] = [];
displayedRepos: Repository[] = [];
fetching: boolean;
fuse: any;
searchKeyword: string;
constructor(public service: RepositoriesService) { }
ngOnInit() {
this.fetchRepositories();
}
fetchRepositories(): void {
this.fetching = true;
this.service.fetchRepositories().subscribe(resp => {
if (resp && resp.data) {
this.repos = resp.data.map(repo => {
const provider = repo.repository_provider;
let provider_id = null;
switch (provider) {
case 'github': provider_id = repo.github_id; break;
case 'bitbucket': provider_id = repo.bitbucket_id; break;
case 'gitlab': provider_id = repo.gitlab_id; break;
case 'gogs': provider_id = repo.gogs_id; break;
}
return new Repository(
repo.id,
repo.name,
repo.full_name,
provider,
provider_id,
repo.html_url,
repo.api_url,
repo.default_branch,
repo.description,
Boolean(repo.fork),
Boolean(repo.public),
repo.access_tokens_id
);
});
}
this.displayedRepos = [...this.repos];
this.fetching = false;
});
}
onKeywordChanged(): void {
if (this.searchKeyword === '') {
this.displayedRepos = [...this.repos];
return;
}
const options = {
keys: ['name', 'full_name', 'repository_provider', 'description', 'html_url'] as any[],
id: 'id'
};
this.fuse = new Fuse(this.repos, options);
const ids = this.fuse.search(this.searchKeyword).map(id => Number(id));
this.displayedRepos = this.repos.filter(repo => ids.indexOf(Number(repo.id)) !== -1);
}
}