-
Notifications
You must be signed in to change notification settings - Fork 0
/
Api-cars.html
186 lines (158 loc) · 4.98 KB
/
Api-cars.html
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>API</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<style>
* {
box-sizing: content-box;
padding: 0;
margin: 0;
}
</style>
<div class="container">
<div class="row justify-content-center m-3">
<div class="col-md-2 col-lg-4">
<select id="car-year" class="form-control mb-3 h-50">
<option value="">--All--</option>
</select>
</div>
<div class="col-md-2 col-lg-4">
<select id="car-color" class="form-control mb-3 h-50">
<option value="">--All color--</option>
</select>
</div>
<div class="col-md-2 col-lg-2">
<button type="button" class="btn btn-danger btn-block">Select car</button>
</div>
</div>
</div>
<div class="container">
<div id="results" class="row d-flex justify-content-center m-3">
</div>
</div>
<script>
let carsData;
var htmlcod="";
const apiUrl = 'https://freetestapi.com/api/v1/cars';
fetch(apiUrl)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
carsData = data;
const selectedData = [];
data.forEach(car => {
const { year, color } = car;
selectedData.push({ year, color });
});
selectedData.forEach(car => {
// console.log(car.year);
});
const set1 = new Set(selectedData.map(item => item.year));
const set2 = new Set(selectedData.map(item => item.color));
set1.forEach(year => {
myFunction(year);
});
set2.forEach(color => {
myFunction2(color);
});
//console.log(set1);
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
/////////////////////funksiya
function myFunction(m) {
var x = document.getElementById("car-year");
var option = document.createElement("option");
option.text = m;
x.add(option);
};
/////////////////////funksiya-2
function myFunction2(j) {
var x = document.getElementById("car-color");
var option = document.createElement("option");
option.text = j;
x.add(option);
};
var first_se = document.getElementById("car-year");
var second_se = document.getElementById("car-color");
var button = document.querySelector(".btn");
window.addEventListener("load", (event) => {
second_se.disabled=true;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
data.forEach(element => {
htmlcod+=`<div class="col-sm-6 col-md-4 col-lg-3">
<div class="card" style="width: 100%;">
<img class="card-img-top" src="${element.image}" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">${element.make}</h5>
<p class="card-text">${element.engine}</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
</div>`;
document.getElementById("results").innerHTML = htmlcod;
});
});
});
first_se.addEventListener("change",function(){
if (first_se.selectedIndex === 0) {
second_se.disabled=true;
}else {
second_se.disabled=false;
};
})
button.addEventListener("click", function() {
var selectedYear = first_se.value;
var selectedColor = second_se.value;
var htmlself = "";
var cars1 = carsData.filter(car => car.year == selectedYear && car.color == selectedColor);
var cars2 = carsData.filter(car => car.year == selectedYear);
if(second_se.selectedIndex == 0)
{
cars2.forEach(car => {
htmlself += `<div class="col-md-3">
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="${car.image}" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">${car.make}</h5>
<p class="card-text">${car.engine}</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
</div>`;
});
};
if (first_se.selectedIndex !== 0) {
document.getElementById("results").innerHTML = "";
cars1.forEach(car => {
htmlself += `<div class="col-md-3">
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="${car.image}" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">${car.make}</h5>
<p class="card-text">${car.engine}</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
</div>`;
});
document.getElementById("results").innerHTML = htmlself;
}else
{
document.getElementById("results").innerHTML = htmlcod;
};
});
</script>
</body>
</html>