-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.mjs
95 lines (82 loc) · 2.87 KB
/
app.mjs
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
import superFetch from "./js/api/super-fetch.mjs";
import { URL_PRODUCTS } from "./js/api/urls.mjs";
import { filter_data } from "./js/functions/filter/filter-data.mjs";
import { createFilterButtons } from "./js/functions/filter/create-filter-buttons.mjs";
import setProductLoading from "./js/functions/error/loading.mjs";
import handleNoResponse from "./js/functions/error/no-response.mjs";
import emptySearchResult from "./js/functions/filter/empty-search-result.mjs";
import { createProductCard } from "./js/functions/product-card-functions.mjs";
const path = window.location.pathname;
if (path === "/" || path === "/index.html") {
fetchProducts();
createFilterButtons(filter_data);
}
const container = document.querySelector("div.product-list");
/**
* @description - Fetch all products
* Remove loading
* if no response:
* Create error with retry button
*/
export async function fetchProducts() {
const response = await superFetch(URL_PRODUCTS);
if (response) {
getFilteredProducts(response);
setProductLoading(false);
} else {
setProductLoading(false);
const error = handleNoResponse();
container.appendChild(error)
}
}
//-----------------------------------------------------------------------
/**
*
* @param {*} products
* @description - Iterate through product-array and create card components for the products
*/
function createProductList(products) {
removeCurrentProductList();
products.forEach((product) => {
const card = createProductCard(product);
container.appendChild(card);
});
}
//-----------------------------------------------------------------------
/**
*
* @param {*} products
* @returns - Filtered / All products
* @description - Get all the filter buttons and create an array of objects with key/value pairs, filter out objects with empty values.
* Values set to lowercase for failsafe.
* key e.g "gender"
* value e.g "male"
* Then .filter() the product-list against the newly created filter-array.
*/
export function getFilteredProducts(products) {
const filterKeysAndValues = Array.from(
document.querySelectorAll(".filter-btn")
)
.map((button) => ({ key: button.getAttribute("key"), value: button.value }))
.filter(({ value }) => value);
const filteredProducts = products.filter((product) => {
return filterKeysAndValues.every(({ key, value }) => {
if (value === "true") {
return product[key] === (value === "true"); // Turn boolean-strings into actual booleans
} else {
return product[key].toLowerCase() === value.toLowerCase();
}
});
});
if (filteredProducts) createProductList(filteredProducts);
if (filteredProducts.length === 0) {
emptySearchResult()
}
}
//-----------------------------------------------------------------------
/**
* @description - Flushes the product-list
*/
function removeCurrentProductList() {
container.replaceChildren("");
}