feat(coupons): add per-user limit and fully redeemed badge #70
19
src/App.tsx
19
src/App.tsx
|
|
@ -142,7 +142,7 @@ export default function App() {
|
||||||
|
|
||||||
// Coupons State
|
// Coupons State
|
||||||
const [adminCouponsList, setAdminCouponsList] = useState<any[]>([]);
|
const [adminCouponsList, setAdminCouponsList] = useState<any[]>([]);
|
||||||
const [couponFilterStatus, setCouponFilterStatus] = useState<'all' | 'active' | 'scheduled' | 'expired'>('all');
|
const [couponFilterStatus, setCouponFilterStatus] = useState<'all' | 'active' | 'scheduled' | 'fully_redeemed' | 'expired'>('all');
|
||||||
const [couponSearchQuery, setCouponSearchQuery] = useState<string>('');
|
const [couponSearchQuery, setCouponSearchQuery] = useState<string>('');
|
||||||
|
|
||||||
// Unified Coupon Modal State
|
// Unified Coupon Modal State
|
||||||
|
|
@ -168,6 +168,7 @@ export default function App() {
|
||||||
minSpendUsd: '',
|
minSpendUsd: '',
|
||||||
minSpendEur: '',
|
minSpendEur: '',
|
||||||
usageLimit: '0',
|
usageLimit: '0',
|
||||||
|
perUserLimit: '',
|
||||||
firstOnly: false,
|
firstOnly: false,
|
||||||
});
|
});
|
||||||
const [couponSaving, setCouponSaving] = useState<boolean>(false);
|
const [couponSaving, setCouponSaving] = useState<boolean>(false);
|
||||||
|
|
@ -766,6 +767,7 @@ export default function App() {
|
||||||
minSpendUsd: '',
|
minSpendUsd: '',
|
||||||
minSpendEur: '',
|
minSpendEur: '',
|
||||||
usageLimit: '0',
|
usageLimit: '0',
|
||||||
|
perUserLimit: '',
|
||||||
firstOnly: false,
|
firstOnly: false,
|
||||||
});
|
});
|
||||||
setCouponModalMode('create');
|
setCouponModalMode('create');
|
||||||
|
|
@ -797,6 +799,7 @@ export default function App() {
|
||||||
minSpendUsd: c.min_purchase_usd !== undefined && c.min_purchase_usd !== null ? String(c.min_purchase_usd) : '',
|
minSpendUsd: c.min_purchase_usd !== undefined && c.min_purchase_usd !== null ? String(c.min_purchase_usd) : '',
|
||||||
minSpendEur: c.min_purchase_eur !== undefined && c.min_purchase_eur !== null ? String(c.min_purchase_eur) : '',
|
minSpendEur: c.min_purchase_eur !== undefined && c.min_purchase_eur !== null ? String(c.min_purchase_eur) : '',
|
||||||
usageLimit: String(c.usage_limit ?? 0),
|
usageLimit: String(c.usage_limit ?? 0),
|
||||||
|
perUserLimit: c.per_user_limit !== undefined && c.per_user_limit !== null ? String(c.per_user_limit) : '',
|
||||||
firstOnly: Boolean(c.is_first_purchase_only),
|
firstOnly: Boolean(c.is_first_purchase_only),
|
||||||
});
|
});
|
||||||
setCouponModalMode('edit');
|
setCouponModalMode('edit');
|
||||||
|
|
@ -826,6 +829,19 @@ export default function App() {
|
||||||
const minUsd = couponFormData.minSpendUsd.trim() ? parseFloat(couponFormData.minSpendUsd) : undefined;
|
const minUsd = couponFormData.minSpendUsd.trim() ? parseFloat(couponFormData.minSpendUsd) : undefined;
|
||||||
const minEur = couponFormData.minSpendEur.trim() ? parseFloat(couponFormData.minSpendEur) : undefined;
|
const minEur = couponFormData.minSpendEur.trim() ? parseFloat(couponFormData.minSpendEur) : undefined;
|
||||||
|
|
||||||
|
const trimmedPerUser = couponFormData.perUserLimit.trim();
|
||||||
|
if (trimmedPerUser === '') {
|
||||||
|
alert('Per-user limit is required. Please enter 0 or greater.');
|
||||||
|
setCouponSaving(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const parsedPerUser = Number(trimmedPerUser);
|
||||||
|
if (!Number.isInteger(parsedPerUser) || parsedPerUser < 0) {
|
||||||
|
alert('Per-user limit must be a whole number (0 or greater).');
|
||||||
|
setCouponSaving(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const payload: any = {
|
const payload: any = {
|
||||||
code: couponFormData.code.trim().toUpperCase(),
|
code: couponFormData.code.trim().toUpperCase(),
|
||||||
discount_type: couponFormData.discountType,
|
discount_type: couponFormData.discountType,
|
||||||
|
|
@ -839,6 +855,7 @@ export default function App() {
|
||||||
max_discount_usd: capUsd,
|
max_discount_usd: capUsd,
|
||||||
max_discount_eur: capEur,
|
max_discount_eur: capEur,
|
||||||
usage_limit: parseInt(couponFormData.usageLimit) || 0,
|
usage_limit: parseInt(couponFormData.usageLimit) || 0,
|
||||||
|
per_user_limit: parsedPerUser,
|
||||||
min_purchase_amount: minInr,
|
min_purchase_amount: minInr,
|
||||||
min_purchase_inr: minInr,
|
min_purchase_inr: minInr,
|
||||||
min_purchase_usd: minUsd,
|
min_purchase_usd: minUsd,
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ export interface CouponFormData {
|
||||||
minSpendUsd: string;
|
minSpendUsd: string;
|
||||||
minSpendEur: string;
|
minSpendEur: string;
|
||||||
usageLimit: string;
|
usageLimit: string;
|
||||||
|
perUserLimit: string;
|
||||||
firstOnly: boolean;
|
firstOnly: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -421,9 +422,11 @@ export const CouponModal: React.FC<CouponModalProps> = ({
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3 pt-1">
|
<div className="grid grid-cols-1 sm:grid-cols-3 gap-3 pt-1 items-start">
|
||||||
<div>
|
<div>
|
||||||
<label className="block text-[11px] font-bold text-slate-600 mb-1">Global Usage Limit (0=None)</label>
|
<label className="block text-[11px] font-bold text-slate-600 mb-1">
|
||||||
|
Total Usage Limit
|
||||||
|
</label>
|
||||||
<input
|
<input
|
||||||
type="number"
|
type="number"
|
||||||
min="0"
|
min="0"
|
||||||
|
|
@ -432,8 +435,34 @@ export const CouponModal: React.FC<CouponModalProps> = ({
|
||||||
placeholder="0"
|
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"
|
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"
|
||||||
/>
|
/>
|
||||||
|
<p className="text-[10px] text-slate-400 font-medium mt-1">
|
||||||
|
0 = Unlimited total uses
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center pt-5">
|
<div>
|
||||||
|
<label className="block text-[11px] font-bold text-slate-600 mb-1">
|
||||||
|
Per-User Limit *
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
required
|
||||||
|
min="0"
|
||||||
|
value={data.perUserLimit}
|
||||||
|
onChange={(e) => onChange('perUserLimit', e.target.value)}
|
||||||
|
placeholder="e.g. 1"
|
||||||
|
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"
|
||||||
|
/>
|
||||||
|
<p
|
||||||
|
className={`text-[10px] font-medium mt-1 ${
|
||||||
|
data.perUserLimit.trim() === '0' ? 'text-rose-600 font-semibold' : 'text-slate-400'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{data.perUserLimit.trim() === '0'
|
||||||
|
? '0 = Blocked for all users'
|
||||||
|
: '0 = Blocked · 1+ = Uses per user'}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center sm:pt-7">
|
||||||
<label className="flex items-center gap-2 cursor-pointer">
|
<label className="flex items-center gap-2 cursor-pointer">
|
||||||
<input
|
<input
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,41 @@ import React, { useState } from 'react';
|
||||||
import { Ticket, Plus, Check, Copy, Edit, Trash2 } from 'lucide-react';
|
import { Ticket, Plus, Check, Copy, Edit, Trash2 } from 'lucide-react';
|
||||||
import { Pagination } from '../common/Pagination';
|
import { Pagination } from '../common/Pagination';
|
||||||
|
|
||||||
|
type CouponStatusFilter = 'all' | 'active' | 'scheduled' | 'fully_redeemed' | 'expired';
|
||||||
|
|
||||||
|
const STATUS_LABELS: Record<Exclude<CouponStatusFilter, 'all'>, string> = {
|
||||||
|
active: 'Active',
|
||||||
|
scheduled: 'Scheduled',
|
||||||
|
fully_redeemed: 'Fully Redeemed',
|
||||||
|
expired: 'Expired',
|
||||||
|
};
|
||||||
|
|
||||||
|
interface CouponStatusFields {
|
||||||
|
usage_limit?: number;
|
||||||
|
usage_count?: number;
|
||||||
|
schedule_status?: string;
|
||||||
|
is_active?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const getEffectiveCouponStatus = (coupon: CouponStatusFields): string => {
|
||||||
|
const usageLimit = coupon.usage_limit || 0;
|
||||||
|
const usageCount = coupon.usage_count || 0;
|
||||||
|
if (coupon.schedule_status === 'fully_redeemed') {
|
||||||
|
return 'fully_redeemed';
|
||||||
|
}
|
||||||
|
if (coupon.is_active && usageLimit > 0 && usageCount >= usageLimit) {
|
||||||
|
return 'fully_redeemed';
|
||||||
|
}
|
||||||
|
return coupon.schedule_status ?? '';
|
||||||
|
};
|
||||||
|
|
||||||
interface CouponsTabProps {
|
interface CouponsTabProps {
|
||||||
coupons: any[];
|
coupons: any[];
|
||||||
filterStatus: 'all' | 'active' | 'scheduled' | 'expired';
|
filterStatus: CouponStatusFilter;
|
||||||
searchQuery: string;
|
searchQuery: string;
|
||||||
currentPage: number;
|
currentPage: number;
|
||||||
perPage: number;
|
perPage: number;
|
||||||
onFilterStatusChange: (status: 'all' | 'active' | 'scheduled' | 'expired') => void;
|
onFilterStatusChange: (status: CouponStatusFilter) => void;
|
||||||
onSearchChange: (query: string) => void;
|
onSearchChange: (query: string) => void;
|
||||||
onPageChange: (page: number) => void;
|
onPageChange: (page: number) => void;
|
||||||
onPerPageChange: (perPage: number) => void;
|
onPerPageChange: (perPage: number) => void;
|
||||||
|
|
@ -42,7 +70,7 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
||||||
};
|
};
|
||||||
|
|
||||||
const filteredCoupons = coupons.filter((c) => {
|
const filteredCoupons = coupons.filter((c) => {
|
||||||
if (filterStatus !== 'all' && c.schedule_status !== filterStatus) return false;
|
if (filterStatus !== 'all' && getEffectiveCouponStatus(c) !== filterStatus) return false;
|
||||||
if (searchQuery.trim()) {
|
if (searchQuery.trim()) {
|
||||||
const q = searchQuery.toLowerCase();
|
const q = searchQuery.toLowerCase();
|
||||||
return (
|
return (
|
||||||
|
|
@ -94,7 +122,7 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
||||||
<div className="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4 bg-slate-50/80 border border-slate-200/60 rounded-2xl p-4">
|
<div className="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4 bg-slate-50/80 border border-slate-200/60 rounded-2xl p-4">
|
||||||
{/* Filter Pills */}
|
{/* Filter Pills */}
|
||||||
<div className="flex flex-wrap items-center gap-1.5">
|
<div className="flex flex-wrap items-center gap-1.5">
|
||||||
{(['all', 'active', 'scheduled', 'expired'] as const).map((status) => (
|
{(['all', 'active', 'scheduled', 'fully_redeemed', 'expired'] as const).map((status) => (
|
||||||
<button
|
<button
|
||||||
key={status}
|
key={status}
|
||||||
type="button"
|
type="button"
|
||||||
|
|
@ -102,7 +130,7 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
||||||
onFilterStatusChange(status);
|
onFilterStatusChange(status);
|
||||||
onPageChange(1);
|
onPageChange(1);
|
||||||
}}
|
}}
|
||||||
className={`px-3 py-1.5 rounded-xl text-xs font-bold capitalize transition-all cursor-pointer ${
|
className={`px-3 py-1.5 rounded-xl text-xs font-bold transition-all cursor-pointer ${
|
||||||
filterStatus === status
|
filterStatus === status
|
||||||
? 'bg-indigo-600 text-white shadow-sm'
|
? 'bg-indigo-600 text-white shadow-sm'
|
||||||
: 'bg-white text-slate-600 border border-slate-200 hover:bg-slate-100 shadow-sm'
|
: 'bg-white text-slate-600 border border-slate-200 hover:bg-slate-100 shadow-sm'
|
||||||
|
|
@ -110,7 +138,7 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
||||||
>
|
>
|
||||||
{status === 'all'
|
{status === 'all'
|
||||||
? `All (${coupons.length})`
|
? `All (${coupons.length})`
|
||||||
: `${status} (${coupons.filter((c) => c.schedule_status === status).length})`}
|
: `${STATUS_LABELS[status]} (${coupons.filter((c) => getEffectiveCouponStatus(c) === status).length})`}
|
||||||
</button>
|
</button>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -164,6 +192,13 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
||||||
const hasCap = isPercent && c.max_discount_limit > 0;
|
const hasCap = isPercent && c.max_discount_limit > 0;
|
||||||
const usageLimit = c.usage_limit || 0;
|
const usageLimit = c.usage_limit || 0;
|
||||||
const usageCount = c.usage_count || 0;
|
const usageCount = c.usage_count || 0;
|
||||||
|
const effectiveStatus = getEffectiveCouponStatus(c);
|
||||||
|
const statusLabel =
|
||||||
|
effectiveStatus === 'fully_redeemed'
|
||||||
|
? 'Fully Redeemed'
|
||||||
|
: effectiveStatus;
|
||||||
|
const hasPerUserLimit =
|
||||||
|
c.per_user_limit !== undefined && c.per_user_limit !== null;
|
||||||
const hasSpecific =
|
const hasSpecific =
|
||||||
(c.applicable_plan_ids && c.applicable_plan_ids.length > 0) ||
|
(c.applicable_plan_ids && c.applicable_plan_ids.length > 0) ||
|
||||||
(c.applicable_module_ids && c.applicable_module_ids.length > 0);
|
(c.applicable_module_ids && c.applicable_module_ids.length > 0);
|
||||||
|
|
@ -230,19 +265,21 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
||||||
<td className="py-3.5 px-4 whitespace-nowrap">
|
<td className="py-3.5 px-4 whitespace-nowrap">
|
||||||
<span
|
<span
|
||||||
className={`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-[10px] font-extrabold tracking-wider uppercase border ${
|
className={`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-[10px] font-extrabold tracking-wider uppercase border ${
|
||||||
c.schedule_status === 'active'
|
effectiveStatus === 'active'
|
||||||
? 'bg-emerald-50 text-emerald-700 border-emerald-200'
|
? 'bg-emerald-50 text-emerald-700 border-emerald-200'
|
||||||
: c.schedule_status === 'scheduled'
|
: effectiveStatus === 'scheduled'
|
||||||
? 'bg-amber-50 text-amber-700 border-amber-200'
|
? 'bg-amber-50 text-amber-700 border-amber-200'
|
||||||
: c.schedule_status === 'expired'
|
: effectiveStatus === 'fully_redeemed'
|
||||||
|
? 'bg-indigo-50 text-indigo-700 border-indigo-200'
|
||||||
|
: effectiveStatus === 'expired'
|
||||||
? 'bg-rose-50 text-rose-700 border-rose-200'
|
? 'bg-rose-50 text-rose-700 border-rose-200'
|
||||||
: 'bg-slate-100 text-slate-500 border-slate-200'
|
: 'bg-slate-100 text-slate-500 border-slate-200'
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
{c.schedule_status === 'active' && (
|
{effectiveStatus === 'active' && (
|
||||||
<span className="h-1.5 w-1.5 rounded-full bg-emerald-500 animate-pulse" />
|
<span className="h-1.5 w-1.5 rounded-full bg-emerald-500 animate-pulse" />
|
||||||
)}
|
)}
|
||||||
{c.schedule_status}
|
{statusLabel}
|
||||||
</span>
|
</span>
|
||||||
<p className="text-[10px] text-slate-400 font-medium mt-1">
|
<p className="text-[10px] text-slate-400 font-medium mt-1">
|
||||||
{c.start_date?.split('T')[0] || '—'} → {c.end_date?.split('T')[0] || '—'}
|
{c.start_date?.split('T')[0] || '—'} → {c.end_date?.split('T')[0] || '—'}
|
||||||
|
|
@ -264,6 +301,17 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
||||||
) : (
|
) : (
|
||||||
<span className="text-[9px] text-slate-400 font-semibold">Unlimited</span>
|
<span className="text-[9px] text-slate-400 font-semibold">Unlimited</span>
|
||||||
)}
|
)}
|
||||||
|
{hasPerUserLimit && (
|
||||||
|
<span
|
||||||
|
className={`block text-[9px] font-bold mt-1 ${
|
||||||
|
c.per_user_limit === 0 ? 'text-rose-600' : 'text-slate-500'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{c.per_user_limit === 0
|
||||||
|
? 'Per user: 0 (Blocked)'
|
||||||
|
: `Max ${c.per_user_limit} / user`}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
{/* Scope & Conditions */}
|
{/* Scope & Conditions */}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue