/** @jsxRuntime classic */ /** @jsx jsx */ import { Fragment, useMemo, useState, useRef, useEffect } from 'react'; import { jsx } from '@emotion/react'; import { useColors } from '../../hooks/useColors'; import { useTypography } from '../../hooks/useTypography'; import { ProductCardContainer } from './style'; import { ETypography, ETypographyColor, Icons } from '../../types/theme'; import { BnIcon, Button, Popup, SelectOptions, Tag, Typography, } from '../../components'; import { ProductPrice } from '../../types/entities/seasons'; import { Pricify } from '../../lib/price'; import { DiscountMethodsEnum, GroupItemsPricing, } from '../../types/entities/entitledPrice'; import dayjs from 'dayjs'; interface IAddon { price: number; name: string; prices: ProductPrice[]; defaultPriceId: number; } interface IOption { label: string; value: string; } export interface IProductCardProps { withCta?: boolean; icon?: Icons; state: { name?: string; description?: string; prices?: ProductPrice[]; isProRated?: boolean; entitledPrices?: GroupItemsPricing[]; downpayment?: number; addons?: IAddon[]; registrationStartDate?: string; registrationEndDate?: string; }; defaultPriceTitle?: string; moreMenu: { options?: IOption[]; handleActions?: (v: string) => void; }; handlePurchase?: (v?: any) => void; } interface IProductToOrder extends ProductPrice { ordinal: number; } export const ProductCard = ({ withCta = false, icon, state: { name: title, description, prices = [], registrationStartDate, registrationEndDate, isProRated, downpayment, addons = [], entitledPrices = [], }, defaultPriceTitle = '', moreMenu: { options = [], handleActions = () => {} }, handlePurchase, }: IProductCardProps) => { const { colors } = useColors(); const { typography } = useTypography(); const [height, setHeight] = useState(0); const collapsableRef = useRef(null); const [isExpand, setExpand] = useState(!withCta); const expandToggle = () => setExpand(!isExpand); if ( collapsableRef?.current?.scrollHeight && collapsableRef?.current?.scrollHeight !== height ) { setHeight(collapsableRef?.current?.scrollHeight); } const discounts = useMemo(() => { const d: number[] = []; const uniqueDiscounts: GroupItemsPricing[] = []; entitledPrices.forEach((entitledPrice) => { if (!d.includes(entitledPrice?.group?.id as number)) { uniqueDiscounts.push(entitledPrice); d.push(entitledPrice?.group?.id as number); } }); return uniqueDiscounts; }, [entitledPrices]); return (
{title}
Registration {dayjs(registrationStartDate).format('MMM DD')} -{' '} {dayjs(registrationEndDate).format('MMM DD, YYYY')} {withCta && (
)}
{description}
{prices .sort( (a, b) => dayjs(a.startDate).valueOf() - dayjs(b.startDate).valueOf() ) .map((price) => { let newPrice: IProductToOrder = { ...price, ordinal: 1 }; if (dayjs().valueOf() > dayjs(price.endDate).valueOf()) { newPrice['ordinal'] = 10; } return newPrice; }) .sort((a: IProductToOrder, b: IProductToOrder) => { return a.ordinal - b.ordinal; }) .map((price, index) => { return ( dayjs(price.endDate).valueOf() } state={price} key={index} defaultPriceTitle={defaultPriceTitle} /> ); })}
{isProRated && (
Prorated
)} {discounts.length > 0 &&
} {discounts.length > 0 && (
Discounts {discounts.map((discount, index) => { return ( ); })}
)} {downpayment !== 0 ? (
Payment plan
Deposit {Pricify(downpayment)}
{/* payment due Aug 12, 2021 */}
) : (
)} {addons.length > 0 && (
Addons {addons.map((addon: IAddon, index) => { let price = addon.price; if (!addon.price) { addon.prices.forEach((p) => { if (p.id === addon.defaultPriceId) { price = Number(p.price); } }); } return ( ); })}
)}
{withCta && ( )}
{withCta && (
)}
); }; const MoreMenu = ({ options, handleActions, }: { options: IOption[]; handleActions: (v: string) => void; }) => { const [isOpen, setOpen] = useState(false); return ( setOpen(v)} body={ } > ); }; const DiscountLine = ({ name, amount, type, }: { name: string; amount: number; type: DiscountMethodsEnum; }) => { return (
{name} {type === DiscountMethodsEnum.AMOUNT ? Pricify(Number(amount)) : `${amount}%`}
); }; export const PriceLine = ({ state, defaultPriceTitle, isDisabled, }: { state: ProductPrice; defaultPriceTitle: string; isDisabled: boolean; }) => { if ( state.name.toLowerCase().includes('late fee') || state.name.toLowerCase().includes('early bird') ) { return (
{state.name.toLowerCase().includes('late fee') ? ( ) : ( )} {state.discountValue && `( ${ state.discountMethod === DiscountMethodsEnum.PERCENT ? `${state.discountValue}%` : `$${state.discountValue}` } )`}
{`${dayjs(state.startDate).format('MMM DD')} - ${dayjs(state.endDate).format('MMM DD, YYYY')}`}
{Pricify(Number(state.price))}
); } else { return (
{defaultPriceTitle} {Pricify(Number(state.price))}
); } };