You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

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