Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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