1
|
/*
|
2
|
* updated by myrto on 19/12/2018
|
3
|
*/
|
4
|
|
5
|
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
|
6
|
import { RepositoryService } from '../../../services/repository.service';
|
7
|
import { loadingReposMessage, noRepositoriesFound, noRepositoryChosenMsg, noServiceMessage } from '../../../domain/shared-messages';
|
8
|
import { Country, RepositorySnippet } from '../../../domain/typeScriptClasses';
|
9
|
|
10
|
@Component({
|
11
|
selector: 'app-select-existing-datasource',
|
12
|
templateUrl: './register-datasource-select-existing.component.html'
|
13
|
})
|
14
|
|
15
|
export class RegisterDatasourceSelectExistingComponent implements OnInit {
|
16
|
countries: Country[] = [];
|
17
|
hasSelectedCountry: boolean;
|
18
|
selectedCountry: Country;
|
19
|
countryRepos: RepositorySnippet[] = [];
|
20
|
hasSelectedRepo: boolean;
|
21
|
|
22
|
noRepositories: string;
|
23
|
alertMessage: string;
|
24
|
loadingMessage: string = loadingReposMessage;
|
25
|
|
26
|
repoId: string;
|
27
|
|
28
|
sourceUrl: string;
|
29
|
sourceTitle: string;
|
30
|
latestUpdate: string;
|
31
|
|
32
|
@Input() mode: string;
|
33
|
|
34
|
@Output() emitRepoId: EventEmitter<string> = new EventEmitter();
|
35
|
@Output() promptNext: EventEmitter<boolean> = new EventEmitter();
|
36
|
|
37
|
searchBox = '';
|
38
|
|
39
|
constructor(private repoService: RepositoryService) {}
|
40
|
|
41
|
ngOnInit() {
|
42
|
this.setUpSourceInfo();
|
43
|
this.getCountries();
|
44
|
this.hasSelectedCountry = false;
|
45
|
}
|
46
|
|
47
|
setUpSourceInfo() {
|
48
|
if (this.mode === 'opendoar') {
|
49
|
this.sourceUrl = 'http://v2.sherpa.ac.uk/opendoar/';
|
50
|
this.sourceTitle = 'OpenDOAR';
|
51
|
} else if (this.mode === 're3data') {
|
52
|
this.sourceUrl = 'https://www.re3data.org/';
|
53
|
this.sourceTitle = 'Re3data';
|
54
|
}
|
55
|
this.getLatestUpdate();
|
56
|
}
|
57
|
|
58
|
getCountries() {
|
59
|
this.repoService.getCountries()
|
60
|
.subscribe(
|
61
|
countries => {
|
62
|
// TODO: check again getCountries null return values
|
63
|
/*/!* check for null values *!/
|
64
|
let nullVals = countries.filter(el => el.name === null);
|
65
|
/!* remove null values from array *!/
|
66
|
for (let nullVal of nullVals) {
|
67
|
let i = countries.findIndex(el => el === nullVal);
|
68
|
/!* remove null value from array *!/
|
69
|
if (i !== -1) { countries.splice(i, 1); }
|
70
|
}*/
|
71
|
|
72
|
/* sort countries array */
|
73
|
this.countries = countries.sort( function(a, b) {
|
74
|
if (a.name < b.name) {
|
75
|
return -1;
|
76
|
} else if (a.name > b.name) {
|
77
|
return 1;
|
78
|
} else {
|
79
|
return 0;
|
80
|
}
|
81
|
} );
|
82
|
},
|
83
|
error => {
|
84
|
this.alertMessage = noServiceMessage;
|
85
|
console.log(error);
|
86
|
});
|
87
|
}
|
88
|
|
89
|
getReposInCountry(i: number) {
|
90
|
setTimeout( () => {
|
91
|
const country = this.countries[i];
|
92
|
console.log(`I got ${country} and ${this.mode}`);
|
93
|
this.countryRepos = [];
|
94
|
this.selectedCountry = country;
|
95
|
this.hasSelectedCountry = false;
|
96
|
this.loadingMessage = loadingReposMessage;
|
97
|
this.noRepositories = '';
|
98
|
this.repoService.getRepositoriesOfCountry(country.code, this.mode).subscribe (
|
99
|
repos => {
|
100
|
this.countryRepos = repos;
|
101
|
},
|
102
|
error => {
|
103
|
console.log(error.statusText);
|
104
|
this.loadingMessage = '';
|
105
|
this.alertMessage = noServiceMessage;
|
106
|
this.countryRepos = [];
|
107
|
},
|
108
|
() => {
|
109
|
if (!this.countryRepos || !this.countryRepos.length) {
|
110
|
this.noRepositories = noRepositoriesFound;
|
111
|
} else {
|
112
|
this.noRepositories = '';
|
113
|
if (this.selectedCountry.code === country.code) {
|
114
|
/* to make sure that the correct set of repositories is displayed - in case of consequent country selections */
|
115
|
this.hasSelectedCountry = true;
|
116
|
} else {
|
117
|
this.countryRepos = [];
|
118
|
}
|
119
|
}
|
120
|
this.loadingMessage = '';
|
121
|
this.alertMessage = '';
|
122
|
console.log('this.selectedCountry became', JSON.stringify(this.selectedCountry));
|
123
|
}
|
124
|
);
|
125
|
}, 500);
|
126
|
}
|
127
|
|
128
|
getLatestUpdate() {
|
129
|
return this.repoService.getListLatestUpdate(this.mode).subscribe (
|
130
|
responseDate => this.latestUpdate = responseDate['lastCollectionDate'],
|
131
|
error => console.log(error)
|
132
|
);
|
133
|
}
|
134
|
|
135
|
onChooseRepository(id: string) {
|
136
|
this.hasSelectedRepo = true;
|
137
|
this.repoId = id;
|
138
|
}
|
139
|
|
140
|
pushedNext() {
|
141
|
this.promptNext.emit(true);
|
142
|
}
|
143
|
|
144
|
public goToNextStep(): boolean {
|
145
|
if (!this.hasSelectedRepo || this.noRepositories) {
|
146
|
this.alertMessage = noRepositoryChosenMsg;
|
147
|
return false;
|
148
|
} else {
|
149
|
this.emitRepoId.emit(this.repoId);
|
150
|
return true;
|
151
|
}
|
152
|
}
|
153
|
|
154
|
searchTerm(event: any) {
|
155
|
this.searchBox = event.target.value;
|
156
|
}
|
157
|
|
158
|
}
|