diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/helpers/cart.js | 4 | ||||
| -rw-r--r-- | src/pages/shop/cart.js | 36 | ||||
| -rw-r--r-- | src/pages/shop/checkout.js | 8 |
3 files changed, 26 insertions, 22 deletions
diff --git a/src/helpers/cart.js b/src/helpers/cart.js index 2cd8da7b..a3b43e96 100644 --- a/src/helpers/cart.js +++ b/src/helpers/cart.js @@ -14,9 +14,9 @@ const getItemCart = (product_id) => { return cart[product_id]; } -const createOrUpdateItemCart = (product_id, quantity, to_process = false) => { +const createOrUpdateItemCart = (product_id, quantity, selected = false) => { let cart = getCart(); - cart[product_id] = { product_id, quantity, to_process }; + cart[product_id] = { product_id, quantity, selected }; setCart(cart); return true; } diff --git a/src/pages/shop/cart.js b/src/pages/shop/cart.js index c003502d..b2dbba3e 100644 --- a/src/pages/shop/cart.js +++ b/src/pages/shop/cart.js @@ -53,7 +53,7 @@ export default function Cart() { dataProducts = dataProducts.map((product) => ({ ...product, quantity: cart[product.id].quantity, - to_process: cart[product.id].to_process, + selected: cart[product.id].selected, })); setProducts(dataProducts); } @@ -64,13 +64,13 @@ export default function Cart() { useEffect(() => { for (const product of products) { - if (product.quantity != '') createOrUpdateItemCart(product.id, product.quantity, product.to_process); + if (product.quantity != '') createOrUpdateItemCart(product.id, product.quantity, product.selected); } - const productsToProcess = products.filter((product) => product.to_process == true); + const productsSelected = products.filter((product) => product.selected == true); let calculateTotalPriceBeforeTax = 0; let calculateTotalTaxAmount = 0; let calculateTotalDiscountAmount = 0; - productsToProcess.forEach(product => { + productsSelected.forEach(product => { let priceBeforeTax = product.price.price / 1.11; calculateTotalPriceBeforeTax += priceBeforeTax * product.quantity; calculateTotalTaxAmount += (product.price.price - priceBeforeTax) * product.quantity; @@ -81,8 +81,8 @@ export default function Cart() { setTotalDiscountAmount(calculateTotalDiscountAmount); }, [products]); - const getProductsToProcess = () => { - return products.filter((product) => product.to_process == true); + const getProductsSelected = () => { + return products.filter((product) => product.selected == true); } const updateCart = (productId, quantity) => { @@ -143,10 +143,10 @@ export default function Cart() { toast.success('Berhasil menghapus 1 barang dari keranjang', { duration: 1500 }); } - const toggleProductToProcess = (productId) => { + const toggleProductSelected = (productId) => { let productIndexToUpdate = products.findIndex((product) => product.id == productId); let productsToUpdate = products; - productsToUpdate[productIndexToUpdate].to_process = !productsToUpdate[productIndexToUpdate].to_process; + productsToUpdate[productIndexToUpdate].selected = !productsToUpdate[productIndexToUpdate].selected; setProducts([...productsToUpdate]); } @@ -198,12 +198,16 @@ export default function Cart() { <LineDivider/> <div className="p-4 flex flex-col gap-y-6"> + <div className="flex justify-between items-center"> + <h2>Daftar Produk Belanja</h2> + <Link href="/" className="text-caption-1">Cari Produk Lain</Link> + </div> {products.map((product, index) => ( <div className="flex gap-x-3" key={index}> <div className="w-4/12 flex items-center gap-x-2"> <button - className={'p-2 rounded border-2 ' + (product.to_process ? 'border-yellow_r-9 bg-yellow_r-9' : 'border-gray_r-12')} - onClick={() => toggleProductToProcess(product.id)} + className={'p-2 rounded border-2 ' + (product.selected ? 'border-yellow_r-9 bg-yellow_r-9' : 'border-gray_r-12')} + onClick={() => toggleProductSelected(product.id)} ></button> <Image src={product.parent.image} @@ -264,8 +268,8 @@ export default function Cart() { <div className="p-4 bg-gray_r-1 sticky bottom-0 border-t-4 border-gray_r-4"> <div className="flex"> <p>Total</p> - <p className="text-gray_r-11 ml-1">{getProductsToProcess().length > 0 && ( - <>({ getProductsToProcess().length } Barang)</> + <p className="text-gray_r-11 ml-1">{getProductsSelected().length > 0 && ( + <>({ getProductsSelected().length } Barang)</> )}</p> <p className="font-semibold text-red_r-11 ml-auto">{currencyFormat(totalPriceBeforeTax + totalTaxAmount - totalDiscountAmount)}</p> </div> @@ -273,16 +277,16 @@ export default function Cart() { <div className="flex gap-x-3 mt-4"> <button className="flex-1 btn-light" - disabled={getProductsToProcess().length == 0} + disabled={getProductsSelected().length == 0} > - Quotation {getProductsToProcess().length > 0 && `(${getProductsToProcess().length})`} + Quotation {getProductsSelected().length > 0 && `(${getProductsSelected().length})`} </button> <button className="flex-1 btn-yellow" - disabled={getProductsToProcess().length == 0} + disabled={getProductsSelected().length == 0} onClick={() => router.push('/shop/checkout')} > - Checkout {getProductsToProcess().length > 0 && `(${getProductsToProcess().length})`} + Checkout {getProductsSelected().length > 0 && `(${getProductsSelected().length})`} </button> </div> </div> diff --git a/src/pages/shop/checkout.js b/src/pages/shop/checkout.js index 5a47dd5e..54b9d598 100644 --- a/src/pages/shop/checkout.js +++ b/src/pages/shop/checkout.js @@ -47,7 +47,7 @@ export default function Checkout() { let cart = getCart(); let productIds = Object .values(cart) - .filter((itemCart) => itemCart.to_process == true) + .filter((itemCart) => itemCart.selected == true) .map((itemCart) => itemCart.product_id); if (productIds.length > 0) { productIds = productIds.join(','); @@ -55,7 +55,7 @@ export default function Checkout() { dataProducts = dataProducts.map((product) => ({ ...product, quantity: cart[product.id].quantity, - to_process: cart[product.id].to_process, + selected: cart[product.id].selected, })); setProducts(dataProducts); } @@ -69,11 +69,11 @@ export default function Checkout() { useEffect(() => { if (products) { - const productsToProcess = products.filter((product) => product.to_process == true); + const productsSelected = products.filter((product) => product.selected == true); let calculateTotalPriceBeforeTax = 0; let calculateTotalTaxAmount = 0; let calculateTotalDiscountAmount = 0; - productsToProcess.forEach(product => { + productsSelected.forEach(product => { let priceBeforeTax = product.price.price / 1.11; calculateTotalPriceBeforeTax += priceBeforeTax * product.quantity; calculateTotalTaxAmount += (product.price.price - priceBeforeTax) * product.quantity; |
