Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

127 wiersze
4.6 KiB

  1. import React from 'react'
  2. import Link from 'next/link'
  3. import * as Icon from 'react-feather'
  4. import { useSelector, useDispatch } from 'react-redux'
  5. import { useToasts } from 'react-toast-notifications'
  6. import { useRouter } from 'next/router'
  7. import QtyForm from './QtyForm'
  8. const CartContent = () => {
  9. const router = useRouter()
  10. const { addToast } = useToasts()
  11. const dispatch = useDispatch()
  12. const cart = useSelector((state) => state.cart)
  13. const total = useSelector((state) => state.total)
  14. // console.log(cart)
  15. const removeItem = (pId) => {
  16. dispatch({
  17. type: 'REMOVE_ITEM',
  18. id: pId
  19. })
  20. addToast('Cart Removed Successfully', { appearance: 'error' })
  21. }
  22. const reset = () => {
  23. dispatch({
  24. type: 'RESET'
  25. })
  26. addToast('Thanks for your order.', { appearance: 'success' })
  27. router.push('/')
  28. }
  29. return (
  30. <>
  31. <div className="cart-table table-responsive">
  32. <table className="table table-bordered">
  33. <thead>
  34. <tr>
  35. <th scope="col">Product</th>
  36. <th scope="col">Name</th>
  37. <th scope="col">Unit Price</th>
  38. <th scope="col">Quantity</th>
  39. <th scope="col">Total</th>
  40. </tr>
  41. </thead>
  42. <tbody>
  43. {cart.length ? cart.map(crt => (
  44. <tr key={crt.id}>
  45. <td className="product-thumbnail">
  46. <Link href="/product-details">
  47. <a>
  48. <img src={crt.image} alt="item" />
  49. </a>
  50. </Link>
  51. </td>
  52. <td className="product-name">
  53. <Link href="/product-details">
  54. <a>{crt.name}</a>
  55. </Link>
  56. </td>
  57. <td className="product-price">
  58. <span className="unit-amount">${crt.price}</span>
  59. </td>
  60. <td className="product-quantity">
  61. <QtyForm {...crt} />
  62. </td>
  63. <td className="product-subtotal">
  64. <span className="subtotal-amount">${(crt.quantity * crt.price).toFixed(2)}</span>
  65. <a className="remove" onClick={() => {removeItem(crt.id)}}>
  66. <Icon.Trash2 />
  67. </a>
  68. </td>
  69. </tr>
  70. )) : (
  71. <tr>
  72. <td colSpan="5" className="text-center">Empty</td>
  73. </tr>
  74. )}
  75. </tbody>
  76. </table>
  77. </div>
  78. <div className="cart-buttons">
  79. <div className="row align-items-center">
  80. <div className="col-lg-7 col-md-7 col-sm-7">
  81. <div className="continue-shopping-box">
  82. <a className="btn btn-light">Continue Shopping</a>
  83. </div>
  84. </div>
  85. <div className="col-lg-5 col-md-5 col-sm-5 text-right">
  86. <Link href="/cart">
  87. <a className="btn btn-light">Update Cart</a>
  88. </Link>
  89. </div>
  90. </div>
  91. </div>
  92. <div className="cart-totals">
  93. <h3>Cart Totals</h3>
  94. <ul>
  95. <li>Subtotal <span>${total.toFixed(2)}</span></li>
  96. <li>Shipping <span>$10.00</span></li>
  97. <li>Total <span><b>${(total + 10).toFixed(2)}</b></span></li>
  98. </ul>
  99. <Link href='/#'>
  100. <a onClick={e => {
  101. e.preventDefault();
  102. reset()
  103. }} className="btn btn-primary">Proceed to Checkout</a>
  104. </Link>
  105. </div>
  106. </>
  107. )
  108. }
  109. export default CartContent