-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountry-list.component.ts
100 lines (83 loc) · 2.78 KB
/
country-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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {Component, OnInit} from '@angular/core';
import {Apollo} from "apollo-angular";
import gql from "graphql-tag";
@Component({
selector: 'app-country-list',
templateUrl: './country-list.component.html',
styleUrls: ['./country-list.component.scss']
})
export class CountryListComponent implements OnInit {
countries: any[] = [];
filterText: string = ''; // Kullanıcının girdiği metin
selectedCountry: any = null; // Seçilen ülke
groupBy: string = ''; // Gruplama ölçütü
groupedCountries: any = {}; // Gruplanmış ülkeler
filteredCountries: any[] = [];
groupedCountryKeys: string[] = [];
constructor(private apollo: Apollo) {
}
ngOnInit(): void {
// GraphQL sorgusunu tanımlayın
const query = gql`
query GetAllCountries {
countries {
code
name
capital
currency
languages {
name
}
}
}
`;
// Apollo Client'i kullanarak sorguyu API'ye gönderin
this.apollo.query<any>({
query: query
}).subscribe(response => {
// API'den gelen tüm ülke verilerini alın
this.countries = response.data.countries.map((country: any) => {
const languageNames = country.languages.map((lang: any) => lang.name).join(', ');
return { ...country, languageNames };
});
});
}
onFilterChange(): void {
// Metin sorgusu değiştiğinde bu işlev çağrılır
// Ülkeleri filtrelemek için filterText'i kullanabilirsiniz
this.filterCountries();
}
filterCountries(): void {
const searchText = this.filterText.toLowerCase();
this.filteredCountries = this.countries.filter((country: any) => {
return country.name.toLowerCase().includes(searchText);
});
this.groupedCountries = this.groupBy
? this.groupCountriesBy(this.filteredCountries, this.groupBy)
: {};
this.groupedCountryKeys = Object.keys(this.groupedCountries);
}
groupCountriesBy(countries: any[], key: string): any {
// Belirtilen özellikle ülkeleri gruplayan bir işlev
return countries.reduce((groups, country) => {
const groupKey = country[key] || 'Diğer'; // Eksik veya boş özelliği olanları "Diğer" grubuna ekler
if (!groups[groupKey]) {
groups[groupKey] = [];
}
groups[groupKey].push(country);
return groups;
}, {});
}
selectCountry(country: any): void {
// Seçilen ülkeyi işaretle ve arka plan rengini ayarla
this.selectedCountry = country;
this.filteredCountries.forEach(item => {
item.isSelected = item === country;
});
}
setAutomaticSelection(): void {
// Otomatik olarak 10. veya son ülkeyi seç
const index = Math.min(9, this.filteredCountries.length - 1);
this.selectCountry(this.filteredCountries[index]);
}
}