import { useEffect, useState } from "react";
import { toast } from "react-hot-toast";
import Head from "next/head";
import {
TrashIcon,
PlusIcon,
MinusIcon,
ChevronLeftIcon,
} from "@heroicons/react/24/solid";
import {
ExclamationTriangleIcon,
HeartIcon,
HomeIcon
} from "@heroicons/react/24/outline";
// Helpers
import {
createOrUpdateItemCart,
deleteItemCart,
getCart
} from "../../helpers/cart";
import { createSlug } from "../../helpers/slug";
import apiOdoo from "../../helpers/apiOdoo";
import currencyFormat from "../../helpers/currencyFormat";
// Components
import ConfirmAlert from "../../components/ConfirmAlert";
import Image from "../../components/Image";
import Layout from "../../components/Layout";
import Link from "../../components/Link";
import Alert from "../../components/Alert";
import Spinner from "../../components/Spinner";
export async function getServerSideProps(context) {
let previousRoute = context.req.headers.referer || '/';
if (previousRoute.endsWith('/shop/cart')) previousRoute = '/';
return { props: { previousRoute } };
}
export default function Cart({ previousRoute }) {
const [isLoadingProducts, setIsLoadingProducts] = useState(true);
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);
}
setIsLoadingProducts(false);
}
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);
if (typeof quantity === 'number') {
quantity = parseInt(quantity);
quantity = Math.floor(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 (
<>
Keranjang
Pembayaran
Selesai
{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)}
{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