474 lines
22 KiB
TypeScript
474 lines
22 KiB
TypeScript
import React from 'react';
|
||
import { Ticket, Edit, Percent, Calendar, Tag, DollarSign, RefreshCw } from 'lucide-react';
|
||
|
||
export interface CouponFormData {
|
||
code: string;
|
||
category: string;
|
||
discountType: 'percentage' | 'flat';
|
||
discountValue: string;
|
||
discountValueUsd: string;
|
||
discountValueEur: string;
|
||
maxDiscountCap: string;
|
||
maxDiscountCapUsd: string;
|
||
maxDiscountCapEur: string;
|
||
description: string;
|
||
startDate: string;
|
||
endDate: string;
|
||
scope: 'storewide' | 'specific';
|
||
applicablePlanIds: string[];
|
||
applicableModuleIds: string[];
|
||
minSpend: string;
|
||
minSpendUsd: string;
|
||
minSpendEur: string;
|
||
usageLimit: string;
|
||
firstOnly: boolean;
|
||
}
|
||
|
||
interface CouponModalProps {
|
||
mode: 'create' | 'edit' | null;
|
||
data: CouponFormData;
|
||
plans: any[];
|
||
modules: any[];
|
||
saving: boolean;
|
||
onChange: (field: keyof CouponFormData, val: any) => void;
|
||
onSave: (e: React.FormEvent) => void;
|
||
onClose: () => void;
|
||
}
|
||
|
||
export const CouponModal: React.FC<CouponModalProps> = ({
|
||
mode,
|
||
data,
|
||
plans,
|
||
modules,
|
||
saving,
|
||
onChange,
|
||
onSave,
|
||
onClose,
|
||
}) => {
|
||
if (!mode) return null;
|
||
|
||
const isEdit = mode === 'edit';
|
||
const isDateRangeInvalid = Boolean(data.startDate && data.endDate && data.endDate <= data.startDate);
|
||
|
||
return (
|
||
<div className="fixed inset-0 z-50 bg-slate-900/60 backdrop-blur-xs flex items-center justify-center p-4">
|
||
<div className="bg-white rounded-3xl border border-slate-200 shadow-2xl max-w-xl w-full p-6 space-y-5 max-h-[90vh] overflow-y-auto animate-in fade-in zoom-in duration-150">
|
||
{/* Header */}
|
||
<div className="flex items-center justify-between border-b border-slate-100 pb-3">
|
||
<div>
|
||
<h3 className="text-base font-bold text-slate-800 flex items-center gap-2">
|
||
{isEdit ? <Edit className="h-4 w-4 text-indigo-600" /> : <Ticket className="h-4 w-4 text-indigo-600" />}
|
||
{isEdit ? `Edit Coupon: ${data.code}` : 'Create Promotional Coupon'}
|
||
</h3>
|
||
<p className="text-xs text-slate-400">
|
||
Configure discount rules, date schedule, scope, and cart thresholds.
|
||
</p>
|
||
</div>
|
||
<button
|
||
type="button"
|
||
onClick={onClose}
|
||
className="text-slate-400 hover:text-slate-600 p-1 cursor-pointer"
|
||
>
|
||
✕
|
||
</button>
|
||
</div>
|
||
|
||
<form onSubmit={onSave} className="space-y-4">
|
||
{/* BLOCK 1: Code & Discount Value */}
|
||
<div className="bg-slate-50/80 border border-slate-200/80 rounded-2xl p-4 space-y-3">
|
||
<div className="flex items-center gap-1.5 border-b border-slate-200/60 pb-2">
|
||
<Percent className="h-3.5 w-3.5 text-indigo-600" />
|
||
<span className="text-xs font-bold text-slate-700">Block 1: Promo Code & Discount Value</span>
|
||
</div>
|
||
|
||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
||
<div>
|
||
<label className="block text-[11px] font-bold text-slate-600 mb-1">Coupon Code *</label>
|
||
<input
|
||
type="text"
|
||
required
|
||
placeholder="e.g. FLASH50"
|
||
disabled={isEdit}
|
||
value={data.code}
|
||
onChange={(e) => onChange('code', e.target.value.toUpperCase())}
|
||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-mono font-bold text-slate-800 uppercase focus:outline-none focus:border-indigo-500 disabled:bg-slate-100"
|
||
/>
|
||
</div>
|
||
<div>
|
||
<label className="block text-[11px] font-bold text-slate-600 mb-1">Category</label>
|
||
<select
|
||
value={data.category}
|
||
onChange={(e) => onChange('category', e.target.value)}
|
||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500 cursor-pointer"
|
||
>
|
||
<option value="general">General Promo</option>
|
||
<option value="introductory">Introductory (New User)</option>
|
||
<option value="seasonal">Seasonal / Flash Sale</option>
|
||
<option value="loyalty">Loyalty / Staggered</option>
|
||
<option value="referral">Referral Partner</option>
|
||
<option value="institutional">Institutional / Student</option>
|
||
</select>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="space-y-3">
|
||
<div>
|
||
<label className="block text-[11px] font-bold text-slate-600 mb-1">Discount Type</label>
|
||
<select
|
||
value={data.discountType}
|
||
onChange={(e) => onChange('discountType', e.target.value)}
|
||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500 cursor-pointer"
|
||
>
|
||
<option value="percentage">Percentage (%)</option>
|
||
<option value="flat">Flat Amount (₹ / $ / €)</option>
|
||
</select>
|
||
</div>
|
||
|
||
{data.discountType === 'percentage' ? (
|
||
<div className="grid grid-cols-1 sm:grid-cols-4 gap-2.5">
|
||
<div>
|
||
<label className="block text-[11px] font-bold text-slate-600 mb-1">Discount % *</label>
|
||
<div className="relative">
|
||
<input
|
||
type="number"
|
||
step="0.01"
|
||
min="0.01"
|
||
max="100"
|
||
required
|
||
value={data.discountValue}
|
||
onChange={(e) => onChange('discountValue', e.target.value)}
|
||
placeholder="e.g. 20"
|
||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-bold text-slate-800 focus:outline-none focus:border-indigo-500"
|
||
/>
|
||
<span className="absolute right-3 top-2 text-xs font-bold text-slate-400">%</span>
|
||
</div>
|
||
</div>
|
||
<div>
|
||
<label className="block text-[11px] font-bold text-slate-600 mb-1">Max Cap ₹ (INR)</label>
|
||
<input
|
||
type="number"
|
||
step="0.01"
|
||
min="0"
|
||
value={data.maxDiscountCap}
|
||
onChange={(e) => onChange('maxDiscountCap', e.target.value)}
|
||
placeholder="0 = None"
|
||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs text-slate-800 focus:outline-none focus:border-indigo-500"
|
||
/>
|
||
</div>
|
||
<div>
|
||
<label className="block text-[11px] font-bold text-slate-600 mb-1">Max Cap $ (USD)</label>
|
||
<input
|
||
type="number"
|
||
step="0.01"
|
||
min="0"
|
||
value={data.maxDiscountCapUsd}
|
||
onChange={(e) => onChange('maxDiscountCapUsd', e.target.value)}
|
||
placeholder="0 = None"
|
||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs text-slate-800 focus:outline-none focus:border-indigo-500"
|
||
/>
|
||
</div>
|
||
<div>
|
||
<label className="block text-[11px] font-bold text-slate-600 mb-1">Max Cap € (EUR)</label>
|
||
<input
|
||
type="number"
|
||
step="0.01"
|
||
min="0"
|
||
value={data.maxDiscountCapEur}
|
||
onChange={(e) => onChange('maxDiscountCapEur', e.target.value)}
|
||
placeholder="0 = None"
|
||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs text-slate-800 focus:outline-none focus:border-indigo-500"
|
||
/>
|
||
</div>
|
||
</div>
|
||
) : (
|
||
<div>
|
||
<label className="block text-[11px] font-bold text-slate-600 mb-1.5">
|
||
Flat Discount Amount (by Currency Tier) *
|
||
</label>
|
||
<div className="grid grid-cols-1 sm:grid-cols-3 gap-2.5">
|
||
<div className="relative">
|
||
<span className="absolute left-3 top-2 text-xs font-bold text-slate-400">₹</span>
|
||
<input
|
||
type="number"
|
||
step="0.01"
|
||
min="0.01"
|
||
required
|
||
value={data.discountValue}
|
||
onChange={(e) => onChange('discountValue', e.target.value)}
|
||
placeholder="INR (₹)"
|
||
className="w-full bg-white border border-slate-200 rounded-xl pl-7 pr-3 py-2 text-xs font-bold text-slate-800 focus:outline-none focus:border-indigo-500"
|
||
/>
|
||
</div>
|
||
<div className="relative">
|
||
<span className="absolute left-3 top-2 text-xs font-bold text-slate-400">$</span>
|
||
<input
|
||
type="number"
|
||
step="0.01"
|
||
min="0"
|
||
value={data.discountValueUsd}
|
||
onChange={(e) => onChange('discountValueUsd', e.target.value)}
|
||
placeholder="USD ($)"
|
||
className="w-full bg-white border border-slate-200 rounded-xl pl-7 pr-3 py-2 text-xs font-bold text-slate-800 focus:outline-none focus:border-indigo-500"
|
||
/>
|
||
</div>
|
||
<div className="relative">
|
||
<span className="absolute left-3 top-2 text-xs font-bold text-slate-400">€</span>
|
||
<input
|
||
type="number"
|
||
step="0.01"
|
||
min="0"
|
||
value={data.discountValueEur}
|
||
onChange={(e) => onChange('discountValueEur', e.target.value)}
|
||
placeholder="EUR (€)"
|
||
className="w-full bg-white border border-slate-200 rounded-xl pl-7 pr-3 py-2 text-xs font-bold text-slate-800 focus:outline-none focus:border-indigo-500"
|
||
/>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
<div>
|
||
<label className="block text-[11px] font-bold text-slate-600 mb-1">Description / Campaign Note</label>
|
||
<input
|
||
type="text"
|
||
placeholder="e.g. Festival 50% discount up to ₹1,000"
|
||
value={data.description}
|
||
onChange={(e) => onChange('description', e.target.value)}
|
||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs text-slate-800 focus:outline-none focus:border-indigo-500"
|
||
/>
|
||
</div>
|
||
</div>
|
||
|
||
{/* BLOCK 2: Schedule & Validity */}
|
||
<div className="bg-slate-50/80 border border-slate-200/80 rounded-2xl p-4 space-y-3">
|
||
<div className="flex items-center gap-1.5 border-b border-slate-200/60 pb-2">
|
||
<Calendar className="h-3.5 w-3.5 text-indigo-600" />
|
||
<span className="text-xs font-bold text-slate-700">Block 2: Schedule & Date Window</span>
|
||
</div>
|
||
|
||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
||
<div>
|
||
<label className="block text-[11px] font-bold text-slate-600 mb-1">Start Date</label>
|
||
<input
|
||
type="date"
|
||
value={data.startDate}
|
||
onChange={(e) => onChange('startDate', e.target.value)}
|
||
className={`w-full bg-white border rounded-xl px-3 py-2 text-xs text-slate-800 focus:outline-none cursor-pointer ${
|
||
isDateRangeInvalid ? 'border-rose-400 focus:border-rose-500' : 'border-slate-200 focus:border-indigo-500'
|
||
}`}
|
||
/>
|
||
</div>
|
||
<div>
|
||
<label className="block text-[11px] font-bold text-slate-600 mb-1">Expiration Date</label>
|
||
<input
|
||
type="date"
|
||
value={data.endDate}
|
||
onChange={(e) => onChange('endDate', e.target.value)}
|
||
className={`w-full bg-white border rounded-xl px-3 py-2 text-xs text-slate-800 focus:outline-none cursor-pointer ${
|
||
isDateRangeInvalid ? 'border-rose-400 focus:border-rose-500' : 'border-slate-200 focus:border-indigo-500'
|
||
}`}
|
||
/>
|
||
</div>
|
||
</div>
|
||
|
||
{isDateRangeInvalid && (
|
||
<div className="text-xs font-semibold text-rose-600 bg-rose-50 border border-rose-200 rounded-xl px-3 py-2 flex items-center gap-2">
|
||
<span>⚠️</span>
|
||
<span>Invalid date range: Expiration date must be later than start date.</span>
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
{/* BLOCK 3: Scope & Item Restrictions */}
|
||
<div className="bg-slate-50/80 border border-slate-200/80 rounded-2xl p-4 space-y-3">
|
||
<div className="flex items-center gap-1.5 border-b border-slate-200/60 pb-2">
|
||
<Tag className="h-3.5 w-3.5 text-indigo-600" />
|
||
<span className="text-xs font-bold text-slate-700">Block 3: Scope & Applicability</span>
|
||
</div>
|
||
|
||
<div className="flex gap-4">
|
||
<label className="flex items-center gap-2 cursor-pointer text-xs font-bold text-slate-700">
|
||
<input
|
||
type="radio"
|
||
name="couponScope"
|
||
checked={data.scope === 'storewide'}
|
||
onChange={() => onChange('scope', 'storewide')}
|
||
className="accent-indigo-600"
|
||
/>
|
||
Entire Storefront (All Items)
|
||
</label>
|
||
<label className="flex items-center gap-2 cursor-pointer text-xs font-bold text-slate-700">
|
||
<input
|
||
type="radio"
|
||
name="couponScope"
|
||
checked={data.scope === 'specific'}
|
||
onChange={() => onChange('scope', 'specific')}
|
||
className="accent-indigo-600"
|
||
/>
|
||
Specific Plans & Modules Only
|
||
</label>
|
||
</div>
|
||
|
||
{data.scope === 'specific' && (
|
||
<div className="space-y-2 pt-2 border-t border-slate-200/60">
|
||
<div>
|
||
<label className="block text-[10px] font-bold text-slate-500 uppercase tracking-wider mb-1">
|
||
Qualifying Plans
|
||
</label>
|
||
<div className="max-h-28 overflow-y-auto bg-white border border-slate-200 rounded-xl p-2 space-y-1.5">
|
||
{plans.map((p) => {
|
||
const pids: string[] = [p.inr_plan_id, p.usd_plan_id, p.eur_plan_id, p.id].filter(Boolean);
|
||
const checked = pids.some((id) => data.applicablePlanIds.includes(id));
|
||
return (
|
||
<label key={p.base_code || p.code} className="flex items-center gap-2 text-xs text-slate-700 cursor-pointer">
|
||
<input
|
||
type="checkbox"
|
||
checked={checked}
|
||
onChange={(e) => {
|
||
if (e.target.checked) {
|
||
const toAdd = pids.filter((id) => !data.applicablePlanIds.includes(id));
|
||
onChange('applicablePlanIds', [...data.applicablePlanIds, ...toAdd]);
|
||
} else {
|
||
onChange('applicablePlanIds', data.applicablePlanIds.filter((x) => !pids.includes(x)));
|
||
}
|
||
}}
|
||
className="accent-indigo-600 rounded"
|
||
/>
|
||
<span className="font-semibold">{p.name} ({p.base_code || p.code})</span>
|
||
</label>
|
||
);
|
||
})}
|
||
</div>
|
||
</div>
|
||
|
||
<div>
|
||
<label className="block text-[10px] font-bold text-slate-500 uppercase tracking-wider mb-1">
|
||
Qualifying Modules
|
||
</label>
|
||
<div className="max-h-28 overflow-y-auto bg-white border border-slate-200 rounded-xl p-2 space-y-1.5">
|
||
{modules.map((m) => {
|
||
const checked = data.applicableModuleIds.includes(m.id);
|
||
return (
|
||
<label key={m.id} className="flex items-center gap-2 text-xs text-slate-700 cursor-pointer">
|
||
<input
|
||
type="checkbox"
|
||
checked={checked}
|
||
onChange={(e) => {
|
||
if (e.target.checked) onChange('applicableModuleIds', [...data.applicableModuleIds, m.id]);
|
||
else onChange('applicableModuleIds', data.applicableModuleIds.filter((x) => x !== m.id));
|
||
}}
|
||
className="accent-indigo-600 rounded"
|
||
/>
|
||
<span className="font-semibold">{m.name}</span>
|
||
</label>
|
||
);
|
||
})}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
{/* BLOCK 4: Conditions & Thresholds */}
|
||
<div className="bg-slate-50/80 border border-slate-200/80 rounded-2xl p-4 space-y-3">
|
||
<div className="flex items-center gap-1.5 border-b border-slate-200/60 pb-2">
|
||
<DollarSign className="h-3.5 w-3.5 text-indigo-600" />
|
||
<span className="text-xs font-bold text-slate-700">Block 4: Conditions & Usage Limits</span>
|
||
</div>
|
||
|
||
<div>
|
||
<label className="block text-[11px] font-bold text-slate-600 mb-1.5">
|
||
Minimum Cart Spend (0 = No Minimum)
|
||
</label>
|
||
<div className="grid grid-cols-1 sm:grid-cols-3 gap-2.5">
|
||
<div className="relative">
|
||
<span className="absolute left-3 top-2 text-xs font-bold text-slate-400">₹</span>
|
||
<input
|
||
type="number"
|
||
step="0.01"
|
||
min="0"
|
||
value={data.minSpend}
|
||
onChange={(e) => onChange('minSpend', e.target.value)}
|
||
placeholder="Min ₹ (INR)"
|
||
className="w-full bg-white border border-slate-200 rounded-xl pl-7 pr-3 py-2 text-xs text-slate-800 focus:outline-none focus:border-indigo-500"
|
||
/>
|
||
</div>
|
||
<div className="relative">
|
||
<span className="absolute left-3 top-2 text-xs font-bold text-slate-400">$</span>
|
||
<input
|
||
type="number"
|
||
step="0.01"
|
||
min="0"
|
||
value={data.minSpendUsd}
|
||
onChange={(e) => onChange('minSpendUsd', e.target.value)}
|
||
placeholder="Min $ (USD)"
|
||
className="w-full bg-white border border-slate-200 rounded-xl pl-7 pr-3 py-2 text-xs text-slate-800 focus:outline-none focus:border-indigo-500"
|
||
/>
|
||
</div>
|
||
<div className="relative">
|
||
<span className="absolute left-3 top-2 text-xs font-bold text-slate-400">€</span>
|
||
<input
|
||
type="number"
|
||
step="0.01"
|
||
min="0"
|
||
value={data.minSpendEur}
|
||
onChange={(e) => onChange('minSpendEur', e.target.value)}
|
||
placeholder="Min € (EUR)"
|
||
className="w-full bg-white border border-slate-200 rounded-xl pl-7 pr-3 py-2 text-xs text-slate-800 focus:outline-none focus:border-indigo-500"
|
||
/>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3 pt-1">
|
||
<div>
|
||
<label className="block text-[11px] font-bold text-slate-600 mb-1">Global Usage Limit (0=None)</label>
|
||
<input
|
||
type="number"
|
||
min="0"
|
||
value={data.usageLimit}
|
||
onChange={(e) => onChange('usageLimit', e.target.value)}
|
||
placeholder="0"
|
||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs text-slate-800 focus:outline-none focus:border-indigo-500"
|
||
/>
|
||
</div>
|
||
<div className="flex items-center pt-5">
|
||
<label className="flex items-center gap-2 cursor-pointer">
|
||
<input
|
||
type="checkbox"
|
||
checked={data.firstOnly}
|
||
onChange={(e) => onChange('firstOnly', e.target.checked)}
|
||
className="accent-indigo-600 rounded"
|
||
/>
|
||
<span className="text-xs font-bold text-slate-700">
|
||
First Purchase Only
|
||
</span>
|
||
</label>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
{/* Actions */}
|
||
<div className="flex gap-3 pt-2">
|
||
<button
|
||
type="button"
|
||
onClick={onClose}
|
||
className="flex-1 py-2.5 rounded-xl text-xs font-bold bg-slate-100 hover:bg-slate-200 text-slate-600 transition-colors cursor-pointer"
|
||
>
|
||
Cancel
|
||
</button>
|
||
<button
|
||
type="submit"
|
||
disabled={saving || isDateRangeInvalid}
|
||
className="flex-1 py-2.5 rounded-xl text-xs font-bold bg-indigo-600 hover:bg-indigo-500 text-white transition-colors flex items-center justify-center gap-1.5 shadow-md shadow-indigo-600/20 cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed"
|
||
>
|
||
{saving ? <RefreshCw className="h-3.5 w-3.5 animate-spin" /> : isEdit ? 'Save Changes' : 'Save & Publish Coupon'}
|
||
</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
);
|
||
};
|