import Alert from '@/core/components/elements/Alert/Alert' import Divider from '@/core/components/elements/Divider/Divider' import Link from '@/core/components/elements/Link/Link' import useAuth from '@/core/hooks/useAuth' import { getItemAddress } from '@/core/utils/address' import addressesApi from '@/lib/address/api/addressesApi' import CartApi from '@/lib/cart/api/CartApi' import VariantCard from '@/lib/variant/components/VariantCard' import { ExclamationCircleIcon } from '@heroicons/react/24/outline' import { useEffect, useRef, useState } from 'react' import _ from 'lodash' import { deleteItemCart, getCart, getItemCart } from '@/core/utils/cart' import currencyFormat from '@/core/utils/currencyFormat' import { toast } from 'react-hot-toast' import getFileBase64 from '@/core/utils/getFileBase64' import checkoutApi from '../api/checkoutApi' import { useRouter } from 'next/router' import VariantGroupCard from '@/lib/variant/components/VariantGroupCard' const Checkout = () => { const router = useRouter() const auth = useAuth() const [selectedAddress, setSelectedAddress] = useState({ shipping: null, invoicing: null }) const [addresses, setAddresses] = useState(null) useEffect(() => { if (!auth) return const getAddresses = async () => { const dataAddresses = await addressesApi() setAddresses(dataAddresses) } getAddresses() }, [auth]) useEffect(() => { if (!addresses) return const matchAddress = (key) => { const addressToMatch = getItemAddress(key) const foundAddress = addresses.filter((address) => address.id == addressToMatch) if (foundAddress.length > 0) { return foundAddress[0] } return addresses[0] } setSelectedAddress({ shipping: matchAddress('shipping'), invoicing: matchAddress('invoicing') }) }, [addresses]) const [products, setProducts] = useState(null) const [totalAmount, setTotalAmount] = useState(0) const [totalDiscountAmount, setTotalDiscountAmount] = useState(0) useEffect(() => { const loadProducts = async () => { const cart = getCart() const variantIds = _.filter(cart, (o) => o.selected == true) .map((o) => o.productId) .join(',') const dataProducts = await CartApi({ variantIds }) const dataProductsQuantity = _.map(dataProducts, (o) => ({ ...o, quantity: getItemCart({ productId: o.id }).quantity })) setProducts(dataProductsQuantity) } loadProducts() }, []) useEffect(() => { if (products) { let calculateTotalAmount = 0 let calculateTotalDiscountAmount = 0 products.forEach((product) => { calculateTotalAmount += product.price.price * product.quantity calculateTotalDiscountAmount += (product.price.price - product.price.priceDiscount) * product.quantity }) setTotalAmount(calculateTotalAmount) setTotalDiscountAmount(calculateTotalDiscountAmount) } }, [products]) const [selectedPayment, setSelectedPayment] = useState(null) const poNumber = useRef('') const poFile = useRef('') const [isLoading, setIsLoading] = useState(false) const checkout = async () => { if (!selectedPayment) { toast.error('Pilih metode pembayaran', { position: 'bottom-center' }) return } const file = poFile.current.files[0] if (typeof file !== 'undefined' && file.size > 5000000) { toast.error('Maksimal ukuran file adalah 5MB', { position: 'bottom-center' }) return } setIsLoading(true) const productOrder = products.map((product) => ({ product_id: product.id, quantity: product.quantity })) let data = { partner_shipping_id: selectedAddress.shipping.id, partner_invoice_id: selectedAddress.invoicing.id, order_line: JSON.stringify(productOrder), type: 'sale_order' } if (poNumber.current.value) data.po_number = poNumber.current.value if (typeof file !== 'undefined') data.po_file = await getFileBase64(file) const isCheckouted = await checkoutApi({ data }) setIsLoading(false) if (isCheckouted?.id) { for (const product of products) deleteItemCart({ productId: product.id }) router.push(`/shop/checkout/finish?id=${isCheckouted.id}`) return } toast.error('Gagal melakukan transaksi, terjadi kesalahan internal') } return ( <>
*) Belum termasuk biaya pengiriman
Dengan melakukan pembelian melalui website Indoteknik, saya menyetujui{' '} Syarat & Ketentuan {' '} yang berlaku
Ukuran dokumen PO Maksimal 5MB
{address.name}
{address.mobile}
{address.street}, {address?.city?.name}