import style from "../styles/card.module.css" import React, { useEffect, useMemo, useState } from 'react' import { InfoIcon, PlusIcon } from "lucide-react" import { Skeleton, Tooltip } from '@chakra-ui/react' import { motion } from "framer-motion" import { PROMO_CATEGORY } from "~/constants/promotion" import { getVariantById } from "~/services/productVariant" import { IProductVariantPromo, IPromotion } from '~/types/promotion' import formatCurrency from '~/libs/formatCurrency' import clsxm from '~/libs/clsxm' import ProductPromoItem from './Item' import ProductPromoAddToCart from "./AddToCart" import ProductPromoCardCountdown from "./CardCountdown" type Props = { promotion: IPromotion } const ProductPromoCard = ({ promotion }: Props) => { const [products, setProducts] = useState([]) useEffect(() => { const getProducts = async () => { const datas = [] for (const product of promotion.products) { const res = await getVariantById(product.product_id) res.data.qty = product.qty datas.push(res.data) } setProducts(datas) } getProducts() }, [promotion.products]) const [freeProducts, setFreeProducts] = useState([]) useEffect(() => { const getFreeProducts = async () => { const datas = [] for (const product of promotion.free_products) { const res = await getVariantById(product.product_id) res.data.qty = product.qty datas.push(res.data) } setFreeProducts(datas) } getFreeProducts() }, [promotion.free_products]) const priceTotal = useMemo(() => { let total = 0; [...products, ...freeProducts].forEach((product) => { total += product.price.price_discount * product.qty }) return total }, [products, freeProducts]) const allProducts = [...products, ...freeProducts] return (
{promotion.name}
Paket {PROMO_CATEGORY[promotion.type.value].alias}
0}> {allProducts.map((product, index) => ( <> products.length && promotion.type.value === 'merchandise'} /> {index + 1 < allProducts.length && (
)}
))}
0}> Rp{formatCurrency(priceTotal)} Hemat Rp {formatCurrency(priceTotal - promotion.price)}
Rp{formatCurrency(promotion.price)} (Total {promotion.total_qty} barang)
) } export default ProductPromoCard