'use client'; import { useState } from 'react'; import { Tag } from 'lucide-react'; import type { Cart } from 'brainerce'; import { getClient } from '@/core/lib/brainerce'; import { useCart } from '@/core/providers/store-provider'; import { couponErrorKey } from '@/core/lib/coupon-error'; import { useTranslations } from '@/core/lib/translations'; import { LoadingSpinner } from '@/ui/shared/loading-spinner'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { cn } from '@/core/lib/utils'; interface CouponInputProps { cart: Cart; /** When provided, uses applyCheckoutCoupon/removeCheckoutCoupon instead of * applyCoupon/removeCoupon. Always pass this when a checkout session exists. */ checkoutId?: string; /** Checkout mode only: the checkout page re-reads its own cart. The cart page needs nothing here. */ onUpdate?: () => void; className?: string; } export function CouponInput({ cart, checkoutId, onUpdate, className }: CouponInputProps) { const t = useTranslations('coupon'); const tc = useTranslations('common'); const { applyCoupon, removeCoupon, dismissMutationError } = useCart(); const [code, setCode] = useState(''); const [applying, setApplying] = useState(false); const [removing, setRemoving] = useState(false); const [error, setError] = useState(null); const appliedCoupon = cart.couponCode || null; async function handleApply() { const trimmed = code.trim(); if (!trimmed || applying) return; setApplying(true); setError(null); try { if (checkoutId) { // A checkout-scoped coupon is not a cart write: the checkout page owns // that view of the cart and re-reads it through `onUpdate`. await getClient().applyCheckoutCoupon(checkoutId, trimmed); setCode(''); onUpdate?.(); } else { // The cart store queues the write and shows the result at once. It // never throws: a refusal comes back in the result. const result = await applyCoupon(trimmed); if (!result.ok) { showApplyError(result.error); // This field says it; clear the shared slot so a cart-level alert // does not say it a second time. dismissMutationError(); return; } setCode(''); } } catch (err) { showApplyError(err); } finally { setApplying(false); } } function showApplyError(err: unknown) { // Known codes are shown from our own (translated) messages; the API's // `message` is English. An uncoded refusal falls back to that message. const key = couponErrorKey(err); const message = key ? t(key) : err instanceof Error ? err.message : t('invalidCode'); setError(message); } async function handleRemove() { if (removing) return; setRemoving(true); setError(null); try { if (checkoutId) { await getClient().removeCheckoutCoupon(checkoutId); onUpdate?.(); } else { const result = await removeCoupon(); // A refusal is rolled back (the coupon is still applied) and reported by // the cart-level alert, which is the only owner this control has. if (!result.ok) console.error('Failed to remove coupon:', result.error); } } catch (err) { console.error('Failed to remove coupon:', err); } finally { setRemoving(false); } } // Show applied coupon if (appliedCoupon) { return (
); } return (
{ setCode(e.target.value); if (error) setError(null); }} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); handleApply(); } }} placeholder={t('placeholder')} className={cn('h-9 flex-1 rounded', error && 'border-destructive')} />
{error &&

{error}

}
); }