|
1 | | -import { parseRequestUrl } from '../utils'; |
| 1 | +/* eslint-disable no-use-before-define */ |
| 2 | +import { parseRequestUrl, rerender } from '../utils'; |
2 | 3 | import { getProduct } from '../api'; |
3 | 4 | import { getCartItems, setCartItems } from '../localStorage'; |
4 | 5 |
|
5 | 6 | const addToCart = (item, forceUpdate = false) => { |
6 | 7 | let cartItems = getCartItems(); |
7 | 8 | const existItem = cartItems.find((x) => x.product === item.product); |
8 | 9 | if (existItem) { |
9 | | - cartItems = cartItems.map((x) => |
10 | | - x.product === existItem.product ? item : x |
11 | | - ); |
| 10 | + if (forceUpdate) { |
| 11 | + cartItems = cartItems.map((x) => |
| 12 | + x.product === existItem.product ? item : x |
| 13 | + ); |
| 14 | + } |
12 | 15 | } else { |
13 | 16 | cartItems = [...cartItems, item]; |
14 | 17 | } |
15 | 18 | setCartItems(cartItems); |
| 19 | + if (forceUpdate) { |
| 20 | + rerender(CartScreen); |
| 21 | + } |
| 22 | +}; |
| 23 | +const removeFromCart = (id) => { |
| 24 | + setCartItems(getCartItems().filter((x) => x.product !== id)); |
| 25 | + if (id === parseRequestUrl().id) { |
| 26 | + document.location.hash = '/cart'; |
| 27 | + } else { |
| 28 | + rerender(CartScreen); |
| 29 | + } |
16 | 30 | }; |
17 | 31 |
|
18 | 32 | const CartScreen = { |
19 | | - after_render: () => {}, |
| 33 | + after_render: () => { |
| 34 | + const qtySelects = document.getElementsByClassName('qty-select'); |
| 35 | + Array.from(qtySelects).forEach((qtySelect) => { |
| 36 | + qtySelect.addEventListener('change', (e) => { |
| 37 | + const item = getCartItems().find((x) => x.product === qtySelect.id); |
| 38 | + addToCart({ ...item, qty: Number(e.target.value) }, true); |
| 39 | + }); |
| 40 | + }); |
| 41 | + const deleteButtons = document.getElementsByClassName('delete-button'); |
| 42 | + Array.from(deleteButtons).forEach((deleteButton) => { |
| 43 | + deleteButton.addEventListener('click', () => { |
| 44 | + removeFromCart(deleteButton.id); |
| 45 | + }); |
| 46 | + }); |
| 47 | + document.getElementById('checkout-button').addEventListener('click', () => { |
| 48 | + document.location.hash = '/signin'; |
| 49 | + }); |
| 50 | + }, |
20 | 51 | render: async () => { |
21 | 52 | const request = parseRequestUrl(); |
22 | 53 | if (request.id) { |
@@ -58,9 +89,16 @@ const CartScreen = { |
58 | 89 | <div> |
59 | 90 | Qty: |
60 | 91 | <select class="qty-select" id="${item.product}"> |
61 | | - <option value="1">1</option> |
| 92 | + ${[...Array(item.countInStock).keys()].map((x) => |
| 93 | + item.qty === x + 1 |
| 94 | + ? `<option selected value="${x + 1}">${x + 1}</option>` |
| 95 | + : `<option value="${x + 1}">${x + 1}</option>` |
| 96 | + )} |
| 97 | +
|
62 | 98 | </select> |
63 | | - <button type="button" class="delete-button" id="${item.product}"> |
| 99 | + <button type="button" class="delete-button" id="${ |
| 100 | + item.product |
| 101 | + }"> |
64 | 102 | Delete |
65 | 103 | </button> |
66 | 104 | </div> |
|
0 commit comments