-
Notifications
You must be signed in to change notification settings - Fork 0
/
testSWAPI.html
96 lines (86 loc) · 3.01 KB
/
testSWAPI.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
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>testSWAPI</title>
<link rel="stylesheet" href="css/swapi.scss">
<script>
let button = document.querySelector('#button')
let name = document.querySelector('#name')
let population = document.querySelector('#population')
let climate = document.querySelector('#climate')
let terrain = document.querySelector('#terrain')
let gravity = document.querySelector('#gravity')
function getData() {
generateDataLoading()
let randomPlanet = Math.floor((Math.random() * 61) + 1)
let swApi = "https://swapi.co/api/planets/" + randomPlanet
axios.get(swApi).then(response => {
generateData(response.data)
}).catch(e => {
generateDataFail()
})
}
function generateData(data) {
name.innerText = data.name
population.innerText = data.population
climate.innerText = data.climate
terrain.innerText = data.terrain
gravity.innerText = data.gravity
}
function generateDataFail() {
name.innerText = 'Ops! Where is it?'
population.innerText = ''
climate.innerText = ''
terrain.innerText = ''
gravity.innerText = ''
}
function generateDataLoading() {
name.innerHTML = '<i class="fas fa-circle-notch fa-spin fa-sw"></i>'
population.innerText = ''
climate.innerText = ''
terrain.innerText = ''
gravity.innerText = ''
}
button.addEventListener('click', getData)
</script>
</head>
<body>
<div class="container">
<div class="box">
<div class="box__header">
<span>planet</span>
<h1 id="name">planet name</h1>
</div>
<div class="box__body">
<div class="box__body-infos">
<ul>
<li>
<span>population</span>
<p id="population">0000</p>
</li>
<li>
<span>climate</span>
<p id="climate">0000</p>
</li>
<li>
<span>terrain</span>
<p id="terrain">0000</p>
</li>
<li>
<span>gravity</span>
<p id="gravity">0000</p>
</li>
</ul>
</div>
</div>
</div>
<div class="next">
<button id="button" class="btn">
Next <i class="fas fa-long-arrow-alt-right"></i>
</button>
</div>
</div>
</body>
</html>