Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

13.0 KB
Raw Blame
import {useState} from "react";
import {Collapse, Popover} from "@mantine/core";
import {IconChevronDown, IconInfoCircle, IconShieldCheck, IconTag} from "@tabler/icons-react";
import {t} from "@lingui/macro";
import classNames from "classnames";
import {Event, LocationType, Order} from "../../../types.ts";
import {formatCurrency} from "../../../utilites/currency.ts";
import {prettyDate} from "../../../utilites/dates.ts";
import {resolveEventLocation} from "../../../utilites/effectiveLocation.ts";
import classes from './InlineOrderSummary.module.scss';

interface InlineOrderSummaryProps {
    event: Event;
    order: Order;
    defaultExpanded?: boolean;
    showBuyerProtection?: boolean;
}

export const InlineOrderSummary = ({
    event,
    order,
    defaultExpanded = true,
    showBuyerProtection = true,
}: InlineOrderSummaryProps) => {
    const [expanded, setExpanded] = useState(defaultExpanded);

    const totalAmount = order.total_refunded
        ? order.total_gross - order.total_refunded
        : order.total_gross;

    const coverImage = event?.images?.find((image) => image.type === 'EVENT_COVER');
    const orderOccurrence = order.order_items?.[0]?.event_occurrence;
    const effective = resolveEventLocation(event, orderOccurrence);
    const venueName = effective?.type === LocationType.InPerson
        ? (effective.location?.name || effective.location?.structured_address?.venue_name || null)
        : null;
    const city = effective?.type === LocationType.InPerson
        ? effective.location?.structured_address?.city ?? null
        : null;
    const location = city || venueName || null;

    const totalFee = order.taxes_and_fees_rollup?.fees?.reduce((sum, fee) => sum + fee.value, 0) || 0;
    const totalTax = order.taxes_and_fees_rollup?.taxes?.reduce((sum, tax) => sum + tax.value, 0) || 0;
    const totalDiscount = order.order_items?.reduce((sum, item) => {
        if (item.total_before_discount && item.total_before_additions) {
            return sum + (item.total_before_discount - item.total_before_additions);
        }
        return sum;
    }, 0) || 0;

    const seatLabelsByItem = new Map<number, string[]>();
    (order.seats ?? []).forEach(seat => {
        seatLabelsByItem.set(seat.order_item_id, [...(seatLabelsByItem.get(seat.order_item_id) ?? []), seat.seat_label]);
    });

    return (
        <div className={classes.inlineOrderSummary} data-testid="inline-order-summary">
            <div
                className={classes.header}
                onClick={() => setExpanded(!expanded)}
                role="button"
                aria-expanded={expanded}
                tabIndex={0}
                onKeyDown={(e) => e.key === 'Enter' && setExpanded(!expanded)}
            >
                <span className={classes.headerTitle}>{t`Order Summary`}</span>
                <div className={classes.headerRight}>
                    <span className={classes.headerTotal}>
                        {formatCurrency(totalAmount, order.currency)} {order.currency}
                    </span>
                    <IconChevronDown
                        size={20}
                        className={classNames(classes.chevron, {
                            [classes.chevronRotated]: expanded
                        })}
                    />
                </div>
            </div>

            <Collapse expanded={expanded}>
                <div className={classes.content}>
                    <div className={classes.eventInfo}>
                        {coverImage && (
                            <div className={classes.eventImage}>
                                <img src={coverImage.url} alt={event.title}/>
                            </div>
                        )}
                        <div className={classes.eventDetails}>
                            <div className={classes.eventTitle}>{event.title}</div>
                            <div className={classes.eventMeta}>
                                {prettyDate(
                                    order.order_items?.[0]?.event_occurrence?.start_date || event.start_date,
                                    event.timezone,
                                    false
                                )}
                            </div>
                            {order.order_items?.[0]?.event_occurrence?.label && (
                                <div className={classes.eventMeta}>
                                    {order.order_items[0].event_occurrence.label}
                                </div>
                            )}
                            {location && (
                                <div className={classes.eventMeta}>{location}</div>
                            )}
                        </div>
                    </div>

                    <div className={classes.divider}/>

                    <div className={classes.lineItems}>
                        {order.order_items?.map((item) => {
                            const seatLabels = seatLabelsByItem.get(item.id) ?? [];

                            return (
                            <div key={item.id} className={classes.lineItem}>
                                <div className={classes.lineItemLeft}>
                                    <span title={item.item_name}
                                        className={classes.lineItemName}>{item.item_name}</span>
                                    {/* eslint-disable-next-line lingui/no-unlocalized-strings */}
                                    <span className={classes.lineItemQuantity}>× {item.quantity}</span>
                                    {seatLabels.length > 0 && (
                                        <span className={classes.lineItemSeats}>{seatLabels.join(', ')}</span>
                                    )}
                                </div>
                                <div className={classes.lineItemPriceWrapper}>
                                    {!!item.price_before_discount && (
                                        <span className={classes.lineItemPriceOriginal}>
                                            {formatCurrency(item.price_before_discount * item.quantity, order.currency)}
                                        </span>
                                    )}
                                    <span className={classes.lineItemPrice}>
                                        {formatCurrency(item.price * item.quantity, order.currency)}
                                    </span>
                                </div>
                            </div>
                            );
                        })}
                    </div>

                    {order.promo_code && totalDiscount > 0 && (
                        <div className={classes.promoCode}>
                            <div className={classes.promoCodeLeft}>
                                <IconTag size={16}/>
                                <span>{order.promo_code}</span>
                            </div>
                            <span className={classes.promoCodeDiscount}>
                                -{formatCurrency(totalDiscount, order.currency)}
                            </span>
                        </div>
                    )}

                    <div className={classes.divider}/>

                    <div className={classes.totals}>
                        <div className={classes.totalsRow}>
                            <span className={classes.totalsLabel}>{t`Subtotal`}</span>
                            <span className={classes.totalsValue}>
                                {formatCurrency(order.total_before_additions, order.currency)}
                            </span>
                        </div>

                        <div className={classes.totalsRow}>
                            <span className={classes.totalsLabelWithInfo}>
                                <span>{t`Fees`}</span>
                                {order.taxes_and_fees_rollup?.fees && order.taxes_and_fees_rollup.fees.length > 0 && (
                                    <Popover position="top" withArrow shadow="sm" width={220}>
                                        <Popover.Target>
                                            <span className={classes.infoIcon}>
                                                <IconInfoCircle size={14} />
                                            </span>
                                        </Popover.Target>
                                        <Popover.Dropdown>
                                            <div className={classes.breakdownList}>
                                                {order.taxes_and_fees_rollup.fees.map((fee, index) => (
                                                    <div key={index} className={classes.breakdownItem}>
                                                        <span className={classes.breakdownName}>{fee.name}</span>
                                                        <span className={classes.breakdownValue}>
                                                            {formatCurrency(fee.value, order.currency)}
                                                        </span>
                                                    </div>
                                                ))}
                                            </div>
                                        </Popover.Dropdown>
                                    </Popover>
                                )}
                            </span>
                            <span className={classes.totalsValue}>
                                {formatCurrency(totalFee, order.currency)}
                            </span>
                        </div>

                        {totalTax > 0 && (
                            <div className={classes.totalsRow}>
                                <span className={classes.totalsLabelWithInfo}>
                                    <span>{t`Taxes`}</span>
                                    {order.taxes_and_fees_rollup?.taxes && order.taxes_and_fees_rollup.taxes.length > 0 && (
                                        <Popover position="top" withArrow shadow="sm" width={220}>
                                            <Popover.Target>
                                                <span className={classes.infoIcon}>
                                                    <IconInfoCircle size={14} />
                                                </span>
                                            </Popover.Target>
                                            <Popover.Dropdown>
                                                <div className={classes.breakdownList}>
                                                    {order.taxes_and_fees_rollup.taxes.map((tax, index) => (
                                                        <div key={index} className={classes.breakdownItem}>
                                                            <span className={classes.breakdownName}>{tax.name}</span>
                                                            <span className={classes.breakdownValue}>
                                                                {formatCurrency(tax.value, order.currency)}
                                                            </span>
                                                        </div>
                                                    ))}
                                                </div>
                                            </Popover.Dropdown>
                                        </Popover>
                                    )}
                                </span>
                                <span className={classes.totalsValue}>
                                    {formatCurrency(totalTax, order.currency)}
                                </span>
                            </div>
                        )}

                        <div className={classNames(classes.totalsRow, classes.totalsRowFinal)}>
                            <span className={classes.totalsFinalLabel}>{t`Total`}</span>
                            <span className={classes.totalsFinalValue}>
                                {formatCurrency(totalAmount, order.currency)}
                                <span className={classes.totalsCurrency}>{order.currency}</span>
                            </span>
                        </div>
                    </div>

                    {showBuyerProtection && order.is_payment_required && (
                        <div className={classes.buyerProtection}>
                            <IconShieldCheck size={20} className={classes.buyerProtectionIcon}/>
                            <div className={classes.buyerProtectionText}>
                                <span className={classes.buyerProtectionTitle}>{t`Secure Checkout`}</span>
                                <span className={classes.buyerProtectionSubtitle}>
                                    {t`Your payment is protected with bank-level encryption`}
                                </span>
                            </div>
                        </div>
                    )}
                </div>
            </Collapse>
        </div>
    );
};