-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
100 lines (82 loc) · 3.52 KB
/
script.js
File metadata and controls
100 lines (82 loc) · 3.52 KB
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
const baseUrl = "https://api.sampleapis.com/coffee";
let icedOrHot = "hot";
// Fetch coffee data from API based on type (hot or iced)
async function getCoffees(url, type) {
try {
const response = await fetch(`${url}/${type}`);
const json = await response.json();
//Store data in cards variable to display
let cards = document.getElementById("main");
// Clear existing cards before adding new ones
cards.innerHTML = '';
for (let i = 0; i < json.length; i++) {
// Create a container for each coffee item
const card = document.createElement("div");
card.classList.add("card")
// Create an image and add it to the container
const image = document.createElement("img");
image.src = json[i].image;
image.classList.add("image");
card.appendChild(image);
// Create a name and add it to the container
const name = document.createElement("h4");
name.innerText = json[i].title;
name.classList.add("name");
card.appendChild(name)
// Create a description and add it to the container
const description = document.createElement("p");
description.innerText = json[i].description;
description.classList.add("description");
card.appendChild(description);
// Create a container for ingredients
const ingredients = document.createElement("div");
ingredients.classList.add("ingredients");
// Create an ingredient header and add it to the container
const ingredientHeader = document.createElement("h4");
ingredientHeader.innerHTML = "Ingredients:";
ingredientHeader.classList.add("ingredient_header");
card.appendChild(ingredientHeader);
// Create an item for each ingredient and add it to the ingredients container
const item = document.createElement("span");
item.innerText = json[i].ingredients.join(", ");
item.classList.add("ingredient");
ingredients.appendChild(item);
//Add ingredients container to the container
card.appendChild(ingredients);
// Add the container to the main container
cards.appendChild(card);
//call the function for default value("hot")
activateButton()
}
}
catch (error) {
console.error('Error fetching coffee data:', error);
}
}
// Call the function on page load
getCoffees(baseUrl, icedOrHot)
// Event listener for the buttons
document.addEventListener("DOMContentLoaded", function () {
document.getElementById("buttonHot").addEventListener("click", function (event) {
event.preventDefault();
icedOrHot = "hot";
getCoffees(baseUrl, icedOrHot);
activateButton()
});
document.getElementById("buttonIced").addEventListener("click", function (event) {
event.preventDefault();
icedOrHot = "iced";
getCoffees(baseUrl, icedOrHot);
activateButton()
});
});
// Activate button state based on selection
function activateButton() {
if (icedOrHot === "hot") {
document.getElementById("buttonHot").classList.add("active");
document.getElementById("buttonIced").classList.remove("active");
} else {
document.getElementById("buttonHot").classList.remove("active");
document.getElementById("buttonIced").classList.add("active");
}
}