-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
47 lines (40 loc) · 1.54 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
// Import classes
import Product from "./class/Product.js";
import Cart from "./class/Cart.js";
import Customer from "./class/Customer.js";
// Import router
import { handleRouteChange, clickRouter, initRouter } from "./router.js";
// Initialize variables
const productList = []
const categoryList = []
const cart = new Cart();
const customer = new Customer("John Doe");
// Add router events
document.querySelectorAll('.route').forEach(link => {link.addEventListener('click', clickRouter)});
function closeModal() {
document.querySelector("#modal").classList.add("--hidden");
}
document.querySelector("#modal--close-button").addEventListener("click", closeModal);
// API call for product information
fetch('https://fakestoreapi.com/products')
.then(res=>res.json())
.then(json=> {
json.forEach(item => {
productList.push(
new Product(
item.id,
item.title,
item.price,
item.category,
item.description,
item.image
)
)
if (!categoryList.includes(item.category)) {
categoryList.push(item.category);
}
})
categoryList.sort((a, b) => a.localeCompare(b));
initRouter(productList, categoryList, cart, customer);
handleRouteChange();
})