Skip to content

Commit 37cc23b

Browse files
committed
make update button in checkout.js fully interactive
1 parent d38cfbf commit 37cc23b

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

basic-projects/amazon-project/data/cart.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,14 @@ export function calculateCartQuantity(){
6161
cartQuantity += cartItem.quantity;
6262
})
6363
return cartQuantity;
64+
};
65+
66+
67+
export function updateQuantityLabel(newQuantity, itemId){
68+
cart.forEach((cartItem) => {
69+
if (cartItem.productId === itemId && newQuantity > 0){
70+
document.querySelector(`.js-quantity-label-${itemId}`).innerHTML = newQuantity;
71+
cartItem.quantity = newQuantity;
72+
}
73+
})
6474
};

basic-projects/amazon-project/scripts/checkout.js

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {cart, removeFromCart, calculateCartQuantity} from '../data/cart.js'
1+
import {cart, removeFromCart, calculateCartQuantity, updateQuantityLabel} from '../data/cart.js'
22
import {products} from '../data/products.js'
33
import { formatCurrency } from './utils/price.js';
44

@@ -38,13 +38,16 @@ cart.forEach((cartItem) => {
3838
</div>
3939
<div class="product-quantity">
4040
<span>
41-
Quantity: <span class="quantity-label">${cartItem.quantity}</span>
41+
Quantity: <span class="quantity-label js-quantity-label-${matchingProduct.id}">
42+
${cartItem.quantity}</span>
4243
</span>
4344
<span class="update-quantity-link link-primary js-link-primary js-update-button" data-item-id="${matchingProduct.id}">
4445
Update
4546
</span>
46-
<input class="input-quantity">
47-
<span class="save-quantity link-primary"> Save </span>
47+
<input class="input-quantity js-input-quantity-${matchingProduct.id}"
48+
type="number" min="1">
49+
<span class="save-quantity link-primary js-save-link" data-item-id="${matchingProduct.id}">
50+
Save </span>
4851
<span class="delete-quantity-link link-primary js-delete-button" data-item-id="${matchingProduct.id}">
4952
Delete
5053
</span>
@@ -130,8 +133,42 @@ document.querySelectorAll(`.js-update-button`)
130133
const {itemId} = button.dataset;
131134

132135
const container = document.querySelector(`.js-cart-item-${itemId}`)
133-
.classList.add('is-editing-quantity')
134136

137+
container.classList.add('is-editing-quantity');
138+
139+
const input = document.querySelector(`.js-input-quantity-${itemId}`)
140+
input.select();
141+
input.addEventListener('keydown', (event) => {
142+
if (event.key === 'Enter'){
143+
saveUpdatedQuantity(itemId);
144+
}
145+
})
146+
})
147+
});
148+
149+
150+
function saveUpdatedQuantity(itemId){
151+
const container = document.querySelector(`.js-cart-item-${itemId}`);
152+
container.classList.remove('is-editing-quantity');
153+
154+
155+
const quantityInput = document.querySelector(`.js-input-quantity-${itemId}`)
156+
157+
const newQuantity = Number(quantityInput.value);
158+
159+
updateQuantityLabel(newQuantity, itemId);
160+
updateCartQunatity();
161+
162+
quantityInput.value = null;
163+
};
164+
165+
166+
document.querySelectorAll('.js-save-link')
167+
.forEach((link) => {
168+
const {itemId} = link.dataset;
169+
link.addEventListener('click', () => {
170+
171+
saveUpdatedQuantity(itemId);
135172

136173
})
137-
})
174+
});

basic-projects/amazon-project/styles/pages/checkout/checkout.css

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@
211211
}
212212

213213
.input-quantity {
214-
width: 30px;
214+
width: 45px;
215215
}
216216

217217
.save-quantity, .input-quantity {
@@ -223,4 +223,11 @@
223223
.input-quantity {
224224
display: initial;
225225
}
226+
}
227+
228+
.is-editing-quantity {
229+
.js-link-primary,
230+
.quantity-label {
231+
display: none;
232+
}
226233
}

0 commit comments

Comments
 (0)