|
| 1 | +import { getCartItems, getShipping, getPayment } from '../localStorage'; |
| 2 | +import CheckoutSteps from '../components/CheckoutSteps'; |
| 3 | + |
| 4 | +const convertCartToOrder = () => { |
| 5 | + const orderItems = getCartItems(); |
| 6 | + if (orderItems.length === 0) { |
| 7 | + document.location.hash = '/cart'; |
| 8 | + } |
| 9 | + const shipping = getShipping(); |
| 10 | + if (!shipping.address) { |
| 11 | + document.location.hash = '/shipping'; |
| 12 | + } |
| 13 | + const payment = getPayment(); |
| 14 | + if (!payment.paymentMethod) { |
| 15 | + document.location.hash = '/payment'; |
| 16 | + } |
| 17 | + const itemsPrice = orderItems.reduce((a, c) => a + c.price * c.qty, 0); |
| 18 | + const shippingPrice = itemsPrice > 100 ? 0 : 10; |
| 19 | + const taxPrice = Math.round(0.15 * itemsPrice * 100) / 100; |
| 20 | + const totalPrice = itemsPrice + shippingPrice + taxPrice; |
| 21 | + return { |
| 22 | + orderItems, |
| 23 | + shipping, |
| 24 | + payment, |
| 25 | + itemsPrice, |
| 26 | + shippingPrice, |
| 27 | + taxPrice, |
| 28 | + totalPrice, |
| 29 | + }; |
| 30 | +}; |
| 31 | +const PlaceOrderScreen = { |
| 32 | + after_render: () => {}, |
| 33 | + render: () => { |
| 34 | + const { |
| 35 | + orderItems, |
| 36 | + shipping, |
| 37 | + payment, |
| 38 | + itemsPrice, |
| 39 | + shippingPrice, |
| 40 | + taxPrice, |
| 41 | + totalPrice, |
| 42 | + } = convertCartToOrder(); |
| 43 | + return ` |
| 44 | + <div> |
| 45 | + ${CheckoutSteps.render({ |
| 46 | + step1: true, |
| 47 | + step2: true, |
| 48 | + step3: true, |
| 49 | + step4: true, |
| 50 | + })} |
| 51 | + <div class="order"> |
| 52 | + <div class="order-info"> |
| 53 | + <div> |
| 54 | + <h2>Shipping</h2> |
| 55 | + <div> |
| 56 | + ${shipping.address}, ${shipping.city}, ${shipping.postalCode}, |
| 57 | + ${shipping.country} |
| 58 | + </div> |
| 59 | + </div> |
| 60 | + <div> |
| 61 | + <h2>Payment</h2> |
| 62 | + <div> |
| 63 | + Payment Method : ${payment.paymentMethod} |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + <div> |
| 67 | + <ul class="cart-list-container"> |
| 68 | + <li> |
| 69 | + <h2>Shopping Cart</h2> |
| 70 | + <div>Price</div> |
| 71 | + </li> |
| 72 | + ${orderItems |
| 73 | + .map( |
| 74 | + (item) => ` |
| 75 | + <li> |
| 76 | + <div class="cart-image"> |
| 77 | + <img src="${item.image}" alt="${item.name}" /> |
| 78 | + </div> |
| 79 | + <div class="cart-name"> |
| 80 | + <div> |
| 81 | + <a href="/#/product/${item.product}">${item.name} </a> |
| 82 | + </div> |
| 83 | + <div> Qty: ${item.qty} </div> |
| 84 | + </div> |
| 85 | + <div class="cart-price"> $${item.price}</div> |
| 86 | + </li> |
| 87 | + ` |
| 88 | + ) |
| 89 | + .join('\n')} |
| 90 | + </ul> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + <div class="order-action"> |
| 94 | + <ul> |
| 95 | + <li> |
| 96 | + <h2>Order Summary</h2> |
| 97 | + </li> |
| 98 | + <li><div>Items</div><div>$${itemsPrice}</div></li> |
| 99 | + <li><div>Shipping</div><div>$${shippingPrice}</div></li> |
| 100 | + <li><div>Tax</div><div>$${taxPrice}</div></li> |
| 101 | + <li class="total"><div>Order Total</div><div>$${totalPrice}</div></li> |
| 102 | + <li> |
| 103 | + <button class="primary fw"> |
| 104 | + Place Order |
| 105 | + </button> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + `; |
| 110 | + }, |
| 111 | +}; |
| 112 | +export default PlaceOrderScreen; |
0 commit comments