Repository navigation
Expand file tree
/
Copy pathsamplesoln.js
More file actions
114 lines (107 loc) · 3.63 KB
/
Copy pathsamplesoln.js
File metadata and controls
114 lines (107 loc) · 3.63 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
let productsHTML = '';
products.forEach((product) => {
productsHTML += `
<div class="product-container">
<div class="product-image-container">
<img class="product-image"
src="${product.image}">
</div>
<div class="product-name limit-text-to-2-lines">
${product.name}
</div>
<div class="product-rating-container">
<img class="product-rating-stars"
src="images/ratings/rating-${product.rating.stars * 10}.png">
<div class="product-rating-count link-primary">
${product.rating.count}
</div>
</div>
<div class="product-price">
$${(product.priceCents / 100).toFixed(2)}
</div>
<div class="product-quantity-container">
<select class="js-quantity-selector-${product.id}">
<option selected value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<div class="product-spacer"></div>
<div class="added-to-cart js-added-to-cart-${product.id}">
<img src="images/icons/checkmark.png">
Added
</div>
<button class="add-to-cart-button button-primary js-add-to-cart"
data-product-id="${product.id}">
Add to Cart
</button>
</div>
`;
});
document.querySelector('.js-products-grid').innerHTML = productsHTML;
// We're going to use an object to save the timeout ids.
// The reason we use an object is because each product
// will have its own timeoutId. So an object lets us
// save multiple timeout ids for different products.
// For example:
// {
// 'product-id1': 2,
// 'product-id2': 5,
// ...
// }
// (2 and 5 are ids that are returned when we call setTimeout).
const addedMessageTimeouts = {};
document.querySelectorAll('.js-add-to-cart')
.forEach((button) => {
button.addEventListener('click', () => {
const {productId} = button.dataset;
let matchingItem;
cart.forEach((item) => {
if (productId === item.productId) {
matchingItem = item;
}
});
const quantitySelector = document.querySelector(
`.js-quantity-selector-${productId}`
);
const quantity = Number(quantitySelector.value);
if (matchingItem) {
matchingItem.quantity += quantity;
} else {
cart.push({
productId,
quantity
});
}
let cartQuantity = 0;
cart.forEach((item) => {
cartQuantity += item.quantity;
});
document.querySelector('.js-cart-quantity')
.innerHTML = cartQuantity;
const addedMessage = document.querySelector(
`.js-added-to-cart-${productId}`
);
addedMessage.classList.add('added-to-cart-visible');
setTimeout(() => {
// Check if there's a previous timeout for this
// product. If there is, we should stop it.
const previousTimeoutId = addedMessageTimeouts[productId];
if (previousTimeoutId) {
clearTimeout(previousTimeoutId);
}
const timeoutId = setTimeout(() => {
addedMessage.classList.remove('added-to-cart-visible');
}, 2000);
// Save the timeoutId for this product
// so we can stop it later if we need to.
addedMessageTimeouts[productId] = timeoutId;
});
});