import style from '../styles/item.module.css'
import { Skeleton, SkeletonProps, Tooltip } from '@chakra-ui/react'
import { InfoIcon } from 'lucide-react'
import Image from 'next/image'
import Link from 'next/link'
import ImageNext from 'next/image';
import { useEffect, useState } from 'react';
import { PROMO_CATEGORY } from '~/constants/promotion'
import formatCurrency from '~/libs/formatCurrency'
import { createSlug } from '~/libs/slug'
import { CartItem as CartItemProps } from '~/types/cart'
import CartItemAction from './ItemAction'
import CartItemPromo from './ItemPromo'
import CartItemSelect from './ItemSelect'
type Props = {
item: CartItemProps
editable?: boolean
}
const CartItem = ({ item, editable = true }: Props) => {
const [isSni, setIsSni] = useState(false);
const [isTkdn, setTkdn] = useState(false);
useEffect(() => {
const fetchData = async () => {
try {
const responseSni = await fetch('URL_API_SNI');
const dataSni = await responseSni.json();
setIsSni(dataSni && dataSni.sni);
const responseTkdn = await fetch('URL_API_TKDN');
const dataTkdn = await responseTkdn.json();
setTkdn(dataTkdn && dataTkdn.tkdn);
} catch (error) {
console.error('Error fetching data:', error);
setIsSni(false);
setTkdn(false);
}
};
fetchData();
return () => {};
}, []);
return (
{item.cart_type === 'promotion' && (
{item.promotion_type?.value && (
Paket {PROMO_CATEGORY[item.promotion_type?.value].alias}
)}
Selamat! Pembelian anda lebih hemat {' '}
Rp{formatCurrency((item.package_price || 0) * item.quantity - item.subtotal)}
)}
{editable &&
}
{item.cart_type === 'promotion' && (
Rp {formatCurrency((item.package_price || 0))}
Rp {formatCurrency(item.price.price)}
)}
{item.cart_type === 'product' && (
{item.price.discount_percentage > 0 && (
Rp {formatCurrency((item.price.price || 0))}
)}
{item.price.price_discount > 0 && `Rp ${formatCurrency(item.price.price_discount)}`}
{item.price.price_discount === 0 && '-'}
)}
{item.cart_type === 'product' && item.code}
{Math.round(item.weight * 10) / 10} Kg
{editable &&
}
{!editable &&
{item.quantity}
}
{item.products?.map((product) => )}
{item.free_products?.map((product) => )}
)
}
CartItem.Image = function CartItemImage({ item, isSni, isTkdn }: { item: CartItemProps, isSni: boolean, isTkdn: boolean }) {
const image = item?.image || item?.parent?.image
return (
<>
{item.cart_type === 'promotion' && (
{image &&
}
{!image &&
No Image
}
)}
{item.cart_type === 'product' && (
{image && }
{/*
*/}
{!isSni && (
)}
{/*
*/}
{/*
*/}
{!isTkdn && (
)}
{/*
*/}
{!image && No Image
}
)}
>
)
}
CartItem.Name = function CartItemName({ item }: { item: CartItemProps }) {
return (
<>
{item.cart_type === 'promotion' && (
{item.name}
)}
{item.cart_type === 'product' && (
{item.name}
)}
>
)
}
CartItem.Skeleton = function CartItemSkeleton(props: SkeletonProps & { count: number }) {
return Array.from({ length: props.count }).map((_, index) => (
))
}
export default CartItem