import { useEffect, useState } from "react"; import Header from "../../components/Header"; import Layout from "../../components/Layout"; import Link from "../../components/Link"; import { createOrUpdateItemCart, deleteItemCart, getCart } from "../../helpers/cart"; import ChevronLeftIcon from "../../icons/chevron-left.svg"; import MinusIcon from "../../icons/minus.svg"; import PlusIcon from "../../icons/plus.svg"; import TrashIcon from "../../icons/trash.svg"; import { LazyLoadImage } from "react-lazy-load-image-component"; import apiOdoo from "../../helpers/apiOdoo"; import currencyFormat from "../../helpers/currencyFormat"; import { createSlug } from "../../helpers/slug"; import ConfirmAlert from "../../components/ConfirmAlert"; import { toast } from "react-hot-toast"; import 'react-lazy-load-image-component/src/effects/blur.css'; export default function Cart() { const [products, setProducts] = useState([]); const [totalPriceBeforeTax, setTotalPriceBeforeTax] = useState(0); const [totalTaxAmount, setTotalTaxAmount] = useState(0); const [totalDiscountAmount, setTotalDiscountAmount] = useState(0); const [deleteConfirmation, setDeleteConfirmation] = useState({ productId: null, show: false }); const getProducts = async () => { let cart = getCart(); let productIds = Object.keys(cart); if (productIds.length > 0) { productIds = productIds.join(','); let dataProducts = await apiOdoo('GET', `/api/v1/product_variant/${productIds}`); dataProducts = dataProducts.map((product) => ({ ...product, quantity: cart[product.id].quantity, to_process: false })); setProducts(dataProducts); } } useEffect(() => { getProducts(); }, []); useEffect(() => { const productsToProcess = products.filter((product) => product.to_process == true); let calculateTotalPriceBeforeTax = 0; let calculateTotalTaxAmount = 0; let calculateTotalDiscountAmount = 0; productsToProcess.forEach(product => { let priceBeforeTax = product.price.price / 1.11; calculateTotalPriceBeforeTax += priceBeforeTax * product.quantity; calculateTotalTaxAmount += (product.price.price - priceBeforeTax) * product.quantity; calculateTotalDiscountAmount += (product.price.price - product.price.price_discount) * product.quantity; }); setTotalPriceBeforeTax(calculateTotalPriceBeforeTax); setTotalTaxAmount(calculateTotalTaxAmount); setTotalDiscountAmount(calculateTotalDiscountAmount); }, [products]); const getProductsToProcess = () => { return products.filter((product) => product.to_process == true); } const updateCart = (productId, quantity) => { let productIndexToUpdate = products.findIndex((product) => product.id == productId); if (quantity != '') createOrUpdateItemCart(productId, quantity); let productsToUpdate = products; productsToUpdate[productIndexToUpdate].quantity = quantity; setProducts([...productsToUpdate]); }; const blurQuantity = (productId, quantity) => { quantity = quantity == ('' || 0) ? 1 : parseInt(quantity); updateCart(productId, quantity); }; const updateQuantity = (productId, quantity) => { quantity = quantity == '' ? '' : parseInt(quantity); updateCart(productId, quantity); }; const plusQuantity = (productId) => { let productIndexToUpdate = products.findIndex((product) => product.id == productId); let quantity = products[productIndexToUpdate].quantity + 1; updateCart(productId, quantity); } const minusQuantity = (productId) => { let productIndexToUpdate = products.findIndex((product) => product.id == productId); let quantity = products[productIndexToUpdate].quantity - 1; updateCart(productId, quantity); } const showDeleteConfirmation = (productId) => { setDeleteConfirmation({ productId: productId, show: true }); } const hideDeleteConfirmation = () => { setDeleteConfirmation({ productId: null, show: false }); } const deleteItem = () => { const productId = deleteConfirmation.productId; let productIndexToUpdate = products.findIndex((product) => product.id == productId); let productsToUpdate = products; productsToUpdate.splice(productIndexToUpdate, 1); setProducts([...productsToUpdate]); deleteItemCart(productId); hideDeleteConfirmation(); toast.success('Berhasil menghapus 1 barang dari keranjang', { duration: 1500 }); } const toggleProductToProcess = (productId) => { let productIndexToUpdate = products.findIndex((product) => product.id == productId); let productsToUpdate = products; productsToUpdate[productIndexToUpdate].to_process = !productsToUpdate[productIndexToUpdate].to_process; setProducts([...productsToUpdate]); } return ( <>
{/* jsx-start: Progress Bar */}
1

Keranjang

2

Pembayaran

3

Selesai

{/* [End] Progress Bar */}
{/* [Start] Title */}

Keranjang Saya

{/* [End] Title */} {/* [Start] Product List */}
{products.map((product, index) => (
{product.parent.name}

{product.code || '-'} {product.attributes.length > 0 ? ` | ${product.attributes.join(', ')}` : ''}

{currencyFormat(product.price.price_discount)}

{product.price.discount_percentage > 0 ? ( <> {product.price.discount_percentage}%

{currencyFormat(product.price.price)}

) : ''}

{currencyFormat(product.quantity * product.price.price_discount)}

blurQuantity(product.id, e.target.value)} onChange={(e) => updateQuantity(product.id, e.target.value)} value={product.quantity} />
))}
{/* [End] Product List */} {/* [Start] Review Order */} {products.length > 0 ? (

Ringkasan Pesanan

{getProductsToProcess().length > 0 ? (

{getProductsToProcess().length} Barang

) : ''}

Subtotal

{currencyFormat(totalPriceBeforeTax)}

PPN 11%

{currencyFormat(totalTaxAmount)}

Total Diskon

- {currencyFormat(totalDiscountAmount)}


Grand Total

{currencyFormat(totalPriceBeforeTax + totalTaxAmount - totalDiscountAmount)}

*) Belum termasuk biaya pengiriman

Dengan melakukan pembelian melalui website Indoteknik, saya menyetujui Syarat & Ketentuan yang berlaku

) : ''} {/* [End] Review Order */} {/* [Start] Submit Button */} {products.length > 0 ? (
) : ''} {/* [End] Submit Button */}
); }