1
|
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
|
2
|
import { RepositoryService } from '../../../services/repository.service';
|
3
|
import {
|
4
|
loadingReposMessage,
|
5
|
noRepositoriesMessage,
|
6
|
noRepositoryChosenMsg,
|
7
|
noServiceMessage } from '../../../domain/shared-messages';
|
8
|
import { Country, Repository } from '../../../domain/typeScriptClasses';
|
9
|
|
10
|
@Component({
|
11
|
selector: 'register-datasource-shareable',
|
12
|
templateUrl: './register-datasource-shareable.component.html'
|
13
|
})
|
14
|
|
15
|
export class RegisterDatasourceShareableComponent implements OnInit {
|
16
|
countries: Country[] = [];
|
17
|
hasSelectedCountry: boolean;
|
18
|
selectedCountry: string;
|
19
|
countryRepos: Repository[] = [];
|
20
|
noRepositories: string;
|
21
|
hasSelectedRepo: boolean;
|
22
|
alertMessage: string;
|
23
|
showSpinner: boolean;
|
24
|
loadingMessage: string = loadingReposMessage;
|
25
|
repoId: string;
|
26
|
|
27
|
@Input()
|
28
|
mode: string;
|
29
|
|
30
|
@Input()
|
31
|
repoSourceUrl: string;
|
32
|
|
33
|
@Output() emmitObject: EventEmitter<string> = new EventEmitter<string>();
|
34
|
|
35
|
|
36
|
constructor(private repoService:RepositoryService) {}
|
37
|
|
38
|
ngOnInit() {
|
39
|
this.getCountries();
|
40
|
this.hasSelectedCountry = false;
|
41
|
this.selectedCountry = '';
|
42
|
}
|
43
|
|
44
|
getCountries(){
|
45
|
this.repoService.getCountries()
|
46
|
.subscribe(
|
47
|
countries => this.countries = countries.sort( function(a,b){
|
48
|
if(a.name<b.name){
|
49
|
return -1;
|
50
|
} else if(a.name>b.name){
|
51
|
return 1;
|
52
|
} else {
|
53
|
return 0;
|
54
|
}
|
55
|
} ),
|
56
|
error => {
|
57
|
this.alertMessage = noServiceMessage;
|
58
|
console.log(error);
|
59
|
});
|
60
|
}
|
61
|
|
62
|
getReposInCountry(country: string){
|
63
|
console.log(`I got ${country} and ${this.mode}`);
|
64
|
this.countryRepos = [];
|
65
|
this.hasSelectedCountry = true;
|
66
|
this.selectedCountry = country;
|
67
|
this.showSpinner = true;
|
68
|
this.noRepositories = '';
|
69
|
this.repoService.getRepositoriesOfCountry(country,this.mode).subscribe(
|
70
|
repos => this.countryRepos = repos,
|
71
|
error => console.log(error),
|
72
|
() => {
|
73
|
if (!this.countryRepos.length) this.noRepositories = noRepositoriesMessage;
|
74
|
this.showSpinner = false;
|
75
|
}
|
76
|
);
|
77
|
}
|
78
|
|
79
|
onChooseRepository(id: string){
|
80
|
this.hasSelectedRepo = true;
|
81
|
this.repoId = id;
|
82
|
}
|
83
|
|
84
|
public goToNextStep() {
|
85
|
if(!this.hasSelectedRepo || this.noRepositories){
|
86
|
this.alertMessage = noRepositoryChosenMsg;
|
87
|
} else {
|
88
|
this.emmitObject.emit(this.repoId);
|
89
|
}
|
90
|
}
|
91
|
|
92
|
}
|