-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (43 loc) · 1.7 KB
/
app.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
document.addEventListener('DOMContentLoaded', () => {
const startBtn = document.querySelector('.beer-button')
const randomBeer = document.querySelector('.random-beer')
const descriptionDisplay = document.querySelector('.description')
const myImage = document.querySelector('#beerImage')
function getData(e) {
e.preventDefault()
fetch('https://api.punkapi.com/v2/beers/random')
.then((response) => {
return response.json()
})
.then((data) => {
console.log(data)
const name = data[0].name
const description = data[0].description
const {volume} = data[0]
const volumeValue = volume.value
const volumeUnit = volume.unit
const imgUrl = data[0].image_url
// const imagePath = 'https://images.punkapi.com/v2/'
// const filename = imgUrl.substring(imgUrl.lastIndexOf('/')+1)
// console.log(imagePath + filename);
randomBeer.innerHTML = name + ' ' + volumeValue + ' ' + volumeUnit
descriptionDisplay.innerHTML = description
myImage.src = imgUrl
// fetch(imagePath + filename)
// .then((response) => {
// return response.blob()
// })
// .then((images) => {
// console.log(images)
// myImage = URL.createObjectURL(images);
// })
})
.catch(err => {
if (err.status === 404) {
//myImage.src = "beer-icon.png"
console.log("Did not find image")
}
});
};
startBtn.addEventListener('click', getData)
})