-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchapi.js
74 lines (64 loc) · 2.04 KB
/
fetchapi.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
async function getProducts() {
let urlProducts = "https://northwind.vercel.app/api/products";
try {
let result = await fetch(urlProducts);
return await result.json();
} catch (error) {
console.log(error);
}
}
async function getSuppliers() {
let urlSupplier = `https://northwind.vercel.app/api/suppliers`;
try {
let response = await fetch(urlSupplier);
return await response.json();
} catch (error) {
console.log(error);
}
}
async function getCategories() {
let urlCategory = `https://northwind.vercel.app/api/categories`;
try {
let res = await fetch(urlCategory);
return await res.json();
} catch (error) {
console.log(error);
}
}
async function renderProducts() {
let products = await getProducts();
let html = "";
let suppliers = await getSuppliers();
let categories = await getCategories();
// Another way to render the supplier and categories lists
// let suppliers = await fetch("https://northwind.vercel.app/api/suppliers").then(response => response.json()).catch(error => console.log(error)));
// let categories = await fetch("https://northwind.vercel.app/api/categories").then(response => response.json()).catch(error => console.log(error)));
products.forEach(async (product) => {
let supplier = suppliers.find((s) => s.id === product.supplierId);
let category = categories.find((c) => c.id === product.categoryId);
let color = '';
if(product.unitsInStock<5){
color = '#F89B9C';
}
else if(product.unitsInStock<10){
color = '#FFB486';
}
else if(product.unitsInStock<20){
color = '#FFF06F';
}
else{
color = '#82FA91';
}
let htmlSegment = `<tr style="background-color:${color};">
<td>${product.id}</td>
<td>${product.name}</td>
<td>${supplier.companyName}</td>
<td>${category.name}</td>
<td>${product.unitsInStock}</td>
<td>${product.unitPrice}</td></tr>`;
html += htmlSegment;
});
let container = document.querySelector("tbody");
container.innerHTML = html;
}
renderProducts();