"use client"; import { useState } from "react"; import { Check, X, Loader2 } from "lucide-react"; import { Button, Input } from "../../../../shadcnui"; import { PromotionCodeValidationResult } from "../data/stripe-promotion-code.interface"; type PromoCodeInputProps = { appliedCode: PromotionCodeValidationResult | null; isValidating: boolean; error: string | null; onApply: (code: string) => void; onRemove: () => void; disabled?: boolean; }; export function PromoCodeInput({ appliedCode, isValidating, error, onApply, onRemove, disabled = false, }: PromoCodeInputProps) { const [code, setCode] = useState(""); const handleApply = () => { if (code.trim()) { onApply(code.trim().toUpperCase()); } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter") { e.preventDefault(); handleApply(); } }; // Format discount for display const formatDiscount = (result: PromotionCodeValidationResult): string => { if (result.discountType === "percent_off") { return `${result.discountValue}% off`; } // amount_off is in cents, convert to dollars const amount = (result.discountValue || 0) / 100; const currency = result.currency?.toUpperCase() || "USD"; return `${currency} ${amount.toFixed(2)} off`; }; // Format duration for display const formatDuration = (result: PromotionCodeValidationResult): string => { switch (result.duration) { case "forever": return "Applied to all payments"; case "once": return "Applied to first payment only"; case "repeating": return `Applied for ${result.durationInMonths} months`; default: return ""; } }; // Show applied code state if (appliedCode?.valid) { return (
{appliedCode.code} {formatDiscount(appliedCode)}

{formatDuration(appliedCode)}

); } return (
setCode(e.target.value.toUpperCase())} onKeyDown={handleKeyDown} disabled={disabled || isValidating} className="flex-1" />
{error &&

{error}

}
); }