1
|
import {Component, OnInit} from "@angular/core";
|
2
|
import {UsageStatsService} from "../services/usage-stats.service";
|
3
|
import {UsageStat} from "../entities/usage-stat";
|
4
|
import {countries} from "../services/countries";
|
5
|
import {FormControl} from "@angular/forms";
|
6
|
import {Observable} from "rxjs";
|
7
|
import {map, startWith} from "rxjs/operators";
|
8
|
import {ActivatedRoute} from "@angular/router";
|
9
|
|
10
|
@Component({
|
11
|
selector: 'home',
|
12
|
templateUrl: 'home.component.html',
|
13
|
styleUrls: ['home.component.css'],
|
14
|
})
|
15
|
export class HomeComponent implements OnInit{
|
16
|
|
17
|
public stats: UsageStat[] = [];
|
18
|
public countryFb: FormControl = new FormControl();
|
19
|
public countries: Observable<string[]>;
|
20
|
public country: String;
|
21
|
public display: UsageStat;
|
22
|
public initSlide: number = 1;
|
23
|
|
24
|
constructor(private usageStatsService: UsageStatsService,
|
25
|
private route: ActivatedRoute) {}
|
26
|
|
27
|
ngOnInit() {
|
28
|
this.route.queryParams.subscribe(params => {
|
29
|
if(params['slide']) {
|
30
|
this.initSlide = params['slide'];
|
31
|
}
|
32
|
});
|
33
|
this.usageStatsService.getUsageStats().subscribe(stats => {
|
34
|
this.stats = stats;
|
35
|
this.search();
|
36
|
});
|
37
|
this.countries = this.countryFb.valueChanges
|
38
|
.pipe(
|
39
|
startWith(''),
|
40
|
map(value => this._filter(value))
|
41
|
);
|
42
|
}
|
43
|
|
44
|
public search() {
|
45
|
this.country = this.countryFb.value;
|
46
|
this.display = this.stats.filter(stat => !this.country || stat.country === this.country)[0];
|
47
|
}
|
48
|
|
49
|
private _filter(value: string): string[] {
|
50
|
const filterValue = value.toLowerCase();
|
51
|
return countries.map(value => value.name).filter(option => option.toLowerCase().includes(filterValue));
|
52
|
}
|
53
|
}
|