Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | "use client";
import { useState } from "react";
import { Button, Input } from "@/components/ui";
import { Icon } from "@/components/ui/icons";
import type { CartItem } from "@/lib/promotions/types";
interface PromoCodeInputProps {
cart: CartItem[];
onApply: (result: AppliedPromoResult) => void;
onRemove?: () => void;
appliedCode?: string;
className?: string;
}
export interface AppliedPromoResult {
code: string;
promotionId: number;
promotionName: string;
discountAmount: number;
freeShipping: boolean;
savingsMessage?: string;
}
export function PromoCodeInput({
cart,
onApply,
onRemove,
appliedCode,
className = ""}: PromoCodeInputProps) {
const [code, setCode] = useState("");
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [success, setSuccess] = useState(false);
const handleApply = async () => {
if (!code.trim()) return;
setLoading(true);
setError(null);
setSuccess(false);
try {
const response = await fetch("/api/promotions/validate-code", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
code: code.trim(),
cart})});
const data = await response.json();
if (!data.success) {
setError(data.error || "Invalid promo code");
return;
}
setSuccess(true);
setCode("");
onApply({
code: code.trim().toUpperCase(),
promotionId: data.data.promotion.id,
promotionName: data.data.promotion.displayName || data.data.promotion.name,
discountAmount: data.data.discount.totalDiscount,
freeShipping: data.data.discount.freeShipping,
savingsMessage: data.data.discount.savingsMessage});
} catch {
setError("Failed to apply promo code. Please try again.");
} finally {
setLoading(false);
}
};
const handleRemove = () => {
setCode("");
setError(null);
setSuccess(false);
onRemove?.();
};
if (appliedCode) {
return (
<div className={`flex items-center justify-between p-3 bg-green-50 border border-green-200 rounded-lg ${className}`}>
<div className="flex items-center gap-2">
<Icon name="check-circle" size={18} className="text-green-600" />
<span className="text-sm font-medium text-green-800">
Code <span className="font-mono">{appliedCode}</span> applied
</span>
</div>
<button
onClick={handleRemove}
className="text-green-600 hover:text-green-800 text-sm"
>
Remove
</button>
</div>
);
}
return (
<div className={className}>
<div className="flex gap-2">
<Input
value={code}
onChange={(e) => {
setCode(e.target.value.toUpperCase());
setError(null);
}}
placeholder="Enter promo code"
className={`flex-1 font-mono ${error ? "border-red-300" : ""}`}
disabled={loading}
onKeyPress={(e) => e.key === "Enter" && handleApply()}
/>
<Button
onClick={handleApply}
disabled={loading || !code.trim()}
variant="secondary"
>
{loading ? "Applying..." : "Apply"}
</Button>
</div>
{error && (
<p className="mt-2 text-sm text-red-600 flex items-center gap-1">
<Icon name="x-circle" size={14} />
{error}
</p>
)}
{success && !appliedCode && (
<p className="mt-2 text-sm text-green-600 flex items-center gap-1">
<Icon name="check-circle" size={14} />
Promo code applied successfully!
</p>
)}
</div>
);
}
export default PromoCodeInput;
|