-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
71 lines (64 loc) · 1.36 KB
/
main.js
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
const listCars = document.querySelector('#listaCarros')
const detailsCar = document.querySelector('#detalheCarro')
function main(){
const cars = `{
cars{
id,
name,
doors,
price,
photo
}}`
reqApi(cars).then(res => showListCar(res.cars))
}
function getOneCar(id){
const carQuery = `{
car(id: ${id}){
id,
name,
doors,
price,
photo,
manufacture,
type
}
}`
reqApi(carQuery).then(res => showCar(res.car))
}
function showCar(car){
let template = `
<div class="carro-item">
<h1>${car.name}</h1>
<img src="${car.photo}" alt="${car.name}">
<ul class="carro-lista">
<li>Preço: ${car.price}</li>
<li>Ano: ${car.manufacture}</li>
<li>Portas: ${car.doors}</li>
<li>Estado: ${car.type}</li>
</ul>
</div>
`
detailsCar.innerHTML = template
}
function showListCar(cars){
let template = ""
cars.forEach(
car =>
(template += `
<li class="carros-item" onclick="getOneCar('${car.id}')">
<p>${car.name}</p>
<p>${car.price}</p>
</li>
`)
)
listCars.innerHTML = `
<ul class="carros">
${template}
</ul>`
}
async function reqApi(query){
let response = await fetch(`http://localhost:3000/car?query=${query}`)
response = await response.json()
return response.data
}
main()