-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
166 lines (148 loc) · 4.45 KB
/
script.js
File metadata and controls
166 lines (148 loc) · 4.45 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const products = [
{
name: 'Sony Playstation 5',
url: 'images/playstation_5.png',
type: 'games',
price: 499.99,
},
{
name: 'Samsung Galaxy',
url: 'images/samsung_galaxy.png',
type: 'smartphones',
price: 399.99,
},
{
name: 'Cannon EOS Camera',
url: 'images/cannon_eos_camera.png',
type: 'cameras',
price: 749.99,
},
{
name: 'Sony A7 Camera',
url: 'images/sony_a7_camera.png',
type: 'cameras',
price: 1999.99,
},
{
name: 'LG TV',
url: 'images/lg_tv.png',
type: 'televisions',
price: 799.99,
},
{
name: 'Nintendo Switch',
url: 'images/nintendo_switch.png',
type: 'games',
price: 299.99,
},
{
name: 'Xbox Series X',
url: 'images/xbox_series_x.png',
type: 'games',
price: 499.99,
},
{
name: 'Samsung TV',
url: 'images/samsung_tv.png',
type: 'televisions',
price: 1099.99,
},
{
name: 'Google Pixel',
url: 'images/google_pixel.png',
type: 'smartphones',
price: 499.99,
},
{
name: 'Sony ZV1F Camera',
url: 'images/sony_zv1f_camera.png',
type: 'cameras',
price: 799.99,
},
{
name: 'Toshiba TV',
url: 'images/toshiba_tv.png',
type: 'televisions',
price: 499.99,
},
{
name: 'iPhone 14',
url: 'images/iphone_14.png',
type: 'smartphones',
price: 999.99,
},
];
// get DOM elements
const productsWrapperEl = document.getElementById('products-wrapper');
const checkEls = document.querySelectorAll('.check');
const filtersContainer = document.getElementById('filters-container');
const searchInput = document.getElementById('search');
const cartButton = document.getElementById('cartButton');
const cartCount = document.getElementById('cartCount');
// initialize cart item count
let cartItemCount = 0;
// initialize products
const productsEls = [];
// loop over the products and create the product elements
products.forEach((product) => {
const productEl = createProductElement(product);
productsEls.push(productEl);
productsWrapperEl.appendChild(productEl);
});
// add filter event listeners
filtersContainer.addEventListener('change', filterProducts);
searchInput.addEventListener('input', filterProducts);
// create product element
function createProductElement(product) {
const productEl = document.createElement('div');
productEl.className = 'item space-y-2';
productEl.innerHTML = `<div class="bg-gray-100 flex justify-center relative overflow-hidden group cursor-pointer border">
<img src="${product.url}" alt="${product.name}" class="w-full h-full object-cover">
<span class="status bg-black text-white absolute bottom-0 left-0 right-0 text-center py-2 translate-y-full transition group-hover:translate-y-0">Add to cart</span>
</div>
<p class="text-xl">${product.name}</p>
<strong>$${product.price.toLocaleString()}</strong>`;
productEl.querySelector('.status').addEventListener('click', addToCart)
return productEl;
}
// toggle add/remove from cart
function addToCart(e) {
const statusEl = e.target;
if (statusEl.classList.contains('added')) {
// remove from cart
statusEl.classList.remove('added')
statusEl.innerText = 'Add from cart';
statusEl.classList.remove('bg-red-600');
statusEl.classList.add('bg-gray-800');
cartItemCount--;
} else {
// add to cart
statusEl.classList.add('added')
statusEl.innerText = 'Remove from cart';
statusEl.classList.remove('bg-gray-800');
statusEl.classList.add('bg-red-600');
cartItemCount++;
}
// update cart item count
cartCount.innerText = cartItemCount.toString();
}
// filter products by search or checkbox
function filterProducts() {
// get search term
const searchTerm = searchInput.value.trim().toLowerCase();
// get checked categories
const checkedCategories = Array.from(checkEls).filter((check) => check.checked).map((check) => check.id);
// loop over products and check for matches
productsEls.forEach((productEl, index) => {
const product = products[index];
//check to see if product matches the search or checked items
const matchesSearchTerm = product.name.toLowerCase().includes(searchTerm);
const isInCheckedCategory = checkedCategories.length === 0 || checkedCategories.includes(product.type);
// show/hide product based on matches
if (matchesSearchTerm && isInCheckedCategory) {
productEl.classList.remove('hidden');
} else {
productEl.classList.add('hidden');
}
});
}