Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

221 linhas
9.1 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. //library yarn
  9. import NumberFormat from 'react-number-format';
  10. //sweet alert
  11. import swal from 'sweetalert';
  12. const CartContent = function ({ backend, cart_product, ...props }) {
  13. const router = useRouter()
  14. const { addToast } = useToasts()
  15. const dispatch = useDispatch()
  16. const cart = useSelector((state) => state.cart)
  17. const total = useSelector((state) => state.total)
  18. // console.log(cart)
  19. const removeItem = () => {
  20. dispatch({
  21. type: 'REMOVE_ITEM',
  22. id: pId
  23. })
  24. addToast('Cart Removed Successfully', { appearance: 'error' })
  25. }
  26. const reset = () => {
  27. dispatch({
  28. type: 'RESET'
  29. })
  30. addToast('Thanks for your order.', { appearance: 'success' })
  31. router.push('/')
  32. }
  33. const [formValue, setFormValue] = React.useState({
  34. transaction_id: GenerateID(),
  35. product_img: "",
  36. product_name: "",
  37. product_color: "",
  38. product_quantity: 1,
  39. product_price: "",
  40. });
  41. function GenerateID() {
  42. var dt = new Date().getTime();
  43. var uuid = 'Trx-Ord-yyyyyyyy'.replace(/[y]/g, function (c) {
  44. var r = (dt + Math.random() * 16) % 16 | 0;
  45. dt = Math.floor(dt / 16);
  46. return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  47. });
  48. return uuid;
  49. }
  50. return (
  51. <form
  52. onSubmit={async (e) => {
  53. e.preventDefault();
  54. var newformValue = {
  55. ...formValue,
  56. product_name: cart_product[0].product_name,
  57. product_color: cart_product[0].product_color,
  58. product_price: cart_product[0].product_price,
  59. }
  60. setFormValue(newformValue)
  61. console.log(JSON.stringify(newformValue));
  62. const response = await fetch(
  63. "/api/transaction/AddToCheckout",
  64. {
  65. method: "POST",
  66. headers: {
  67. 'Content-Type': 'application/json'
  68. },
  69. body: JSON.stringify(newformValue),
  70. }
  71. );
  72. if (response.ok) {
  73. var res = await response.json();
  74. console.log("cek response :", res);
  75. if (res["STATUS"] === 1) {
  76. res["DATA"]["checkout"];
  77. swal("Produk Berhasil Ditambah ke Keranjang", "Silahkan Cek Keranjang Belanja Anda", "success");
  78. router.push("/yamaha/Shop/Checkout");
  79. }
  80. else {
  81. swal("Produk Gagal di Checkout", "Silahkan Coba Lagi", "error");
  82. }
  83. } else {
  84. swal("Transaksi Gagal", "Silahkan Coba Lagi", "error");
  85. }
  86. return false;
  87. }}
  88. >
  89. <div className="cart-table table-responsive">
  90. <table className="table table-bordered">
  91. <thead>
  92. <tr>
  93. <th scope="col">Product</th>
  94. <th scope="col">Nama Product</th>
  95. <th scope="col">Warna Product</th>
  96. <th scope="col">Harga Product</th>
  97. <th scope="col">Jumlah</th>
  98. <th scope="col">Total</th>
  99. </tr>
  100. </thead>
  101. <tbody>
  102. {cart_product.length ? cart_product.map(data => (
  103. <tr key={data.id}>
  104. <td className="product-thumbnail">
  105. <Link href="/product-details">
  106. <a>
  107. {/* <img src={`${backend}${data.product_img["url"]}`} alt="item" /> */}
  108. </a>
  109. </Link>
  110. </td>
  111. <td className="product-name">
  112. <Link href="/product-details">
  113. <a>{data.product_name}</a>
  114. </Link>
  115. </td>
  116. <td className="product-name">
  117. <Link href="/product-details">
  118. <a>{data.product_color}</a>
  119. </Link>
  120. </td>
  121. <td className="product-price">
  122. <span className="unit-amount"><NumberFormat value={data.product_price} displayType={'text'} thousandSeparator={true} prefix={'Rp.'} /></span>
  123. </td>
  124. <td className="product-quantity">
  125. <QtyForm {...data}
  126. // name="product_quantity"
  127. // onInput={(e) => {
  128. // setFormValue({
  129. // ...formValue,
  130. // product_quantity: e.target.value(),
  131. // })
  132. // }}
  133. />
  134. </td>
  135. {/* <td className="product-subtotal">
  136. <span className="subtotal-amount"><NumberFormat value={(data.quantity * data.product_price).toFixed(2)} displayType={'text'} thousandSeparator={true} prefix={'Rp.'} /></span>
  137. <a href="#" className="remove" onClick={() => {removeItem(data.id)}}>
  138. <Icon.Trash2 />
  139. </a>
  140. </td> */}
  141. <td className="product-subtotal">
  142. <span className="subtotal-amount">
  143. <NumberFormat
  144. value={(2 * data.product_price).toFixed(2)}
  145. displayType={'text'} thousandSeparator={true}
  146. prefix={'Rp.'}
  147. // name="product_price"
  148. // onInput={(e) => {
  149. // setFormValue({
  150. // ...formValue,
  151. // product_price: e.target.value(),
  152. // })
  153. // }}
  154. />
  155. </span>
  156. <a href="#" className="remove" onClick={() => { removeItem(data.id) }}>
  157. <Icon.Trash2 />
  158. </a>
  159. </td>
  160. </tr>
  161. )) : (
  162. <tr>
  163. <td colSpan="5" className="text-center">Tidak Ada Product di Keranjang</td>
  164. </tr>
  165. )}
  166. </tbody>
  167. </table>
  168. </div>
  169. <div className="cart-buttons">
  170. <div className="row align-items-center">
  171. <div className="col-lg-7 col-md-7 col-sm-7">
  172. <div className="continue-shopping-box">
  173. <a href="/yamaha/Product/Motor" className="btn btn-light" style={{ color: 'white' }}>Continue Shopping</a>
  174. </div>
  175. </div>
  176. </div>
  177. </div>
  178. <div className="cart-totals">
  179. <h3>Cart Totals</h3>
  180. <ul>
  181. <li>Subtotal <span>${total.toFixed(2)}</span></li>
  182. <li>Total <span><b>${(total + 10).toFixed(2)}</b></span></li>
  183. </ul>
  184. {/* <Link href="/yamaha/Shop/Checkout">
  185. <a onClick={e => {
  186. e.preventDefault();
  187. reset()
  188. }} className="btn btn-primary">Proceed to Checkout</a>
  189. <button type="submit" className="btn btn-primary">Proceed to Checkout</button>
  190. </Link> */}
  191. <button type="submit" className="btn btn-primary">Proceed to Checkout</button>
  192. </div>
  193. </form>
  194. )
  195. }
  196. export default CartContent