feat(coupons): add per-user limit and fully redeemed badge
This commit is contained in:
parent
e05e1e4f0f
commit
5ff3215aa5
19
src/App.tsx
19
src/App.tsx
|
|
@ -142,7 +142,7 @@ export default function App() {
|
|||
|
||||
// Coupons State
|
||||
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>('');
|
||||
|
||||
// Unified Coupon Modal State
|
||||
|
|
@ -168,6 +168,7 @@ export default function App() {
|
|||
minSpendUsd: '',
|
||||
minSpendEur: '',
|
||||
usageLimit: '0',
|
||||
perUserLimit: '',
|
||||
firstOnly: false,
|
||||
});
|
||||
const [couponSaving, setCouponSaving] = useState<boolean>(false);
|
||||
|
|
@ -766,6 +767,7 @@ export default function App() {
|
|||
minSpendUsd: '',
|
||||
minSpendEur: '',
|
||||
usageLimit: '0',
|
||||
perUserLimit: '',
|
||||
firstOnly: false,
|
||||
});
|
||||
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) : '',
|
||||
minSpendEur: c.min_purchase_eur !== undefined && c.min_purchase_eur !== null ? String(c.min_purchase_eur) : '',
|
||||
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),
|
||||
});
|
||||
setCouponModalMode('edit');
|
||||
|
|
@ -826,6 +829,19 @@ export default function App() {
|
|||
const minUsd = couponFormData.minSpendUsd.trim() ? parseFloat(couponFormData.minSpendUsd) : 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 = {
|
||||
code: couponFormData.code.trim().toUpperCase(),
|
||||
discount_type: couponFormData.discountType,
|
||||
|
|
@ -839,6 +855,7 @@ export default function App() {
|
|||
max_discount_usd: capUsd,
|
||||
max_discount_eur: capEur,
|
||||
usage_limit: parseInt(couponFormData.usageLimit) || 0,
|
||||
per_user_limit: parsedPerUser,
|
||||
min_purchase_amount: minInr,
|
||||
min_purchase_inr: minInr,
|
||||
min_purchase_usd: minUsd,
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ export interface CouponFormData {
|
|||
minSpendUsd: string;
|
||||
minSpendEur: string;
|
||||
usageLimit: string;
|
||||
perUserLimit: string;
|
||||
firstOnly: boolean;
|
||||
}
|
||||
|
||||
|
|
@ -421,9 +422,11 @@ export const CouponModal: React.FC<CouponModalProps> = ({
|
|||
</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>
|
||||
<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
|
||||
type="number"
|
||||
min="0"
|
||||
|
|
@ -432,8 +435,34 @@ export const CouponModal: React.FC<CouponModalProps> = ({
|
|||
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"
|
||||
/>
|
||||
<p className="text-[10px] text-slate-400 font-medium mt-1">
|
||||
0 = Unlimited total uses
|
||||
</p>
|
||||
</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">
|
||||
<input
|
||||
type="checkbox"
|
||||
|
|
|
|||
|
|
@ -2,13 +2,41 @@ import React, { useState } from 'react';
|
|||
import { Ticket, Plus, Check, Copy, Edit, Trash2 } from 'lucide-react';
|
||||
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 {
|
||||
coupons: any[];
|
||||
filterStatus: 'all' | 'active' | 'scheduled' | 'expired';
|
||||
filterStatus: CouponStatusFilter;
|
||||
searchQuery: string;
|
||||
currentPage: number;
|
||||
perPage: number;
|
||||
onFilterStatusChange: (status: 'all' | 'active' | 'scheduled' | 'expired') => void;
|
||||
onFilterStatusChange: (status: CouponStatusFilter) => void;
|
||||
onSearchChange: (query: string) => void;
|
||||
onPageChange: (page: number) => void;
|
||||
onPerPageChange: (perPage: number) => void;
|
||||
|
|
@ -42,7 +70,7 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
|||
};
|
||||
|
||||
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()) {
|
||||
const q = searchQuery.toLowerCase();
|
||||
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">
|
||||
{/* Filter Pills */}
|
||||
<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
|
||||
key={status}
|
||||
type="button"
|
||||
|
|
@ -102,7 +130,7 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
|||
onFilterStatusChange(status);
|
||||
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
|
||||
? 'bg-indigo-600 text-white 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'
|
||||
? `All (${coupons.length})`
|
||||
: `${status} (${coupons.filter((c) => c.schedule_status === status).length})`}
|
||||
: `${STATUS_LABELS[status]} (${coupons.filter((c) => getEffectiveCouponStatus(c) === status).length})`}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
|
@ -164,6 +192,13 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
|||
const hasCap = isPercent && c.max_discount_limit > 0;
|
||||
const usageLimit = c.usage_limit || 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 =
|
||||
(c.applicable_plan_ids && c.applicable_plan_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">
|
||||
<span
|
||||
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'
|
||||
: c.schedule_status === 'scheduled'
|
||||
: effectiveStatus === 'scheduled'
|
||||
? '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-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" />
|
||||
)}
|
||||
{c.schedule_status}
|
||||
{statusLabel}
|
||||
</span>
|
||||
<p className="text-[10px] text-slate-400 font-medium mt-1">
|
||||
{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>
|
||||
)}
|
||||
{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>
|
||||
|
||||
{/* Scope & Conditions */}
|
||||
|
|
|
|||
Loading…
Reference in New Issue