Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
94 lines (74 loc) · 3.04 KB
/
Copy pathscript.js
File metadata and controls
94 lines (74 loc) · 3.04 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
const searchBar = document.getElementById('search-bar');
const inStockCheckbox = document.getElementById('in-stock');
const productList = document.getElementById('product-list');
const fetchData = async () => {
const url = 'https://gist.githubusercontent.com/dupontdenis/b2e5e1b7460c239b39deb76f8d458908/raw/817a898940170ae4ea6d5b16ef462f959c4d38d1/gistfile1.txt';
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.text();
return data;
} catch (error) {
console.error('Error fetching data:', error);
throw error;
}
};
function getUniqueCategories(products) {
const categories = new Set();
products.forEach(product => categories.add(product.category));
return Array.from(categories);
}
function displayProducts(filteredProducts) {
productList.innerHTML = '';
const categories = getUniqueCategories(filteredProducts);
categories.forEach(category => {
const categoryRow = document.createElement('tr');
const categoryCell = document.createElement('td');
categoryCell.textContent = category;
categoryCell.classList.add('category');
categoryCell.setAttribute('colspan', 2);
categoryRow.appendChild(categoryCell);
productList.appendChild(categoryRow);
const categoryProducts = filteredProducts.filter(product => product.category === category);
categoryProducts.forEach(product => {
const productRow = document.createElement('tr');
const nameCell = document.createElement('td');
nameCell.textContent = product.name;
if (!product.stocked) {
nameCell.classList.add('out-of-stock');
}
const priceCell = document.createElement('td');
priceCell.textContent = product.price;
productRow.appendChild(nameCell);
productRow.appendChild(priceCell);
productList.appendChild(productRow);
});
});
}
function filterProducts(products) {
const searchTerm = searchBar.value.toLowerCase();
const inStockOnly = inStockCheckbox.checked;
const filteredProducts = products.filter(product => {
const matchesSearch = product.name.toLowerCase().includes(searchTerm);
const matchesStock = !inStockOnly || product.stocked;
return matchesSearch && matchesStock;
});
displayProducts(filteredProducts);
}
let array = []
const displayData = async () => {
try {
const data = await fetchData();
const arrayString = data.match(/\[.*\]/s)[0];
const products = eval(arrayString);
array = products;
displayProducts(array);
searchBar.addEventListener('input', () => filterProducts(array));
inStockCheckbox.addEventListener('change', () => filterProducts(array));
} catch (error) {
console.error('Error:', error);
}
};
displayData();