-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeverages-js.js
59 lines (48 loc) · 1.56 KB
/
beverages-js.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
const url =
'https://www.thecocktaildb.com/api/json/v1/1/filter.php?a=Non_Alcoholic';
let app = document.querySelector('#results');
const addDrinkToDOM = (drink) => {
let element = document.createElement('div');
let drinkName = document.createElement('h2');
let drinkThumbnail = document.createElement('img');
element.className = 'figure';
drinkName.textContent = drink.strDrink;
drinkThumbnail.width = '200';
drinkThumbnail.height = '200';
drinkThumbnail.src = drink.strDrinkThumb;
drinkThumbnail.alt = "A picture of " + drinkName.textContent;
element.append(drinkThumbnail);
element.append(drinkName);
element.style.display = 'flex';
element.style.flexDirection = 'column';
element.style.alignItems = 'center';
element.style.margin = '10px';
element.style.width = 'auto';
element.style.height = 'auto';
drinkName.style.margin = '10%';
app.append(element);
};
const fetchData = (url) => {
// Add your code here
fetch(url)
.then((response) => response.json())
.then((data) => {
console.log(data.drinks);
data.drinks.forEach((drink) => {
console.log(drink.strDrink, drink.strDrinkThumb);
addDrinkToDOM(drink);
});
})
.catch((error) => {
console.error(error);
let element = document.createElement('div');
element.textContent = 'An error occured. Please try again.';
element.className = 'error';
app.append(element);
})
.finally(() => {
let loader = document.querySelector('#loading');
app.removeChild(loader);
});
};
fetchData(url);