feat(payments): add month and generic year filters in payments tab (issue #41)
- Add dual Year and Month dropdown filters in PaymentsTab - Support generic custom year input (e.g. 2002) without hardcoded year limits - Enable filtering by specific month across any year or full-year view - Provide clean clear-filter controls and comprehensive empty states
This commit is contained in:
parent
e801ec1aee
commit
def70d2033
|
|
@ -1,5 +1,5 @@
|
|||
import React, { useState, useMemo } from 'react';
|
||||
import { Receipt, Download, Mail, RefreshCw, Search, ArrowUpDown, ArrowDown, ArrowUp, X } from 'lucide-react';
|
||||
import { Receipt, Download, Mail, RefreshCw, Search, ArrowUpDown, ArrowDown, ArrowUp, X, Calendar, ChevronDown } from 'lucide-react';
|
||||
import { Pagination } from '../common/Pagination';
|
||||
|
||||
interface PaymentsTabProps {
|
||||
|
|
@ -14,6 +14,21 @@ interface PaymentsTabProps {
|
|||
onShareInvoice: (id: string) => void;
|
||||
}
|
||||
|
||||
const MONTH_OPTIONS = [
|
||||
{ value: '01', label: 'January' },
|
||||
{ value: '02', label: 'February' },
|
||||
{ value: '03', label: 'March' },
|
||||
{ value: '04', label: 'April' },
|
||||
{ value: '05', label: 'May' },
|
||||
{ value: '06', label: 'June' },
|
||||
{ value: '07', label: 'July' },
|
||||
{ value: '08', label: 'August' },
|
||||
{ value: '09', label: 'September' },
|
||||
{ value: '10', label: 'October' },
|
||||
{ value: '11', label: 'November' },
|
||||
{ value: '12', label: 'December' },
|
||||
];
|
||||
|
||||
export const PaymentsTab: React.FC<PaymentsTabProps> = ({
|
||||
payments,
|
||||
currentPage,
|
||||
|
|
@ -27,12 +42,59 @@ export const PaymentsTab: React.FC<PaymentsTabProps> = ({
|
|||
}) => {
|
||||
const [searchQuery, setSearchQuery] = useState('');
|
||||
const [sortOrder, setSortOrder] = useState<'desc' | 'asc'>('desc');
|
||||
const [selectedYear, setSelectedYear] = useState<string>('all');
|
||||
const [selectedMonth, setSelectedMonth] = useState<string>('all');
|
||||
const [isCustomYear, setIsCustomYear] = useState<boolean>(false);
|
||||
const [customYearInput, setCustomYearInput] = useState<string>('');
|
||||
|
||||
// Dynamically extract all available years from payments (with fallback to current year)
|
||||
const availableYears = useMemo(() => {
|
||||
const currentYear = new Date().getFullYear();
|
||||
const yearsSet = new Set<number>([currentYear]);
|
||||
for (const p of payments) {
|
||||
if (!p?.created_at) continue;
|
||||
const d = typeof p.created_at === 'number'
|
||||
? new Date(p.created_at * 1000)
|
||||
: new Date(p.created_at);
|
||||
|
||||
if (!isNaN(d.getTime())) {
|
||||
yearsSet.add(d.getFullYear());
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(yearsSet).sort((a, b) => b - a);
|
||||
}, [payments]);
|
||||
|
||||
// Filter and Sort payments
|
||||
const processedPayments = useMemo(() => {
|
||||
let result = [...payments];
|
||||
|
||||
// Filter
|
||||
// Filter by Year
|
||||
if (selectedYear !== 'all') {
|
||||
result = result.filter((p: any) => {
|
||||
if (!p?.created_at) return false;
|
||||
const d = typeof p.created_at === 'number'
|
||||
? new Date(p.created_at * 1000)
|
||||
: new Date(p.created_at);
|
||||
if (isNaN(d.getTime())) return false;
|
||||
return d.getFullYear().toString() === selectedYear;
|
||||
});
|
||||
}
|
||||
|
||||
// Filter by Month
|
||||
if (selectedMonth !== 'all') {
|
||||
result = result.filter((p: any) => {
|
||||
if (!p?.created_at) return false;
|
||||
const d = typeof p.created_at === 'number'
|
||||
? new Date(p.created_at * 1000)
|
||||
: new Date(p.created_at);
|
||||
if (isNaN(d.getTime())) return false;
|
||||
const m = String(d.getMonth() + 1).padStart(2, '0');
|
||||
return m === selectedMonth;
|
||||
});
|
||||
}
|
||||
|
||||
// Filter by Search Query
|
||||
if (searchQuery.trim()) {
|
||||
const q = searchQuery.toLowerCase().trim();
|
||||
result = result.filter((p: any) => {
|
||||
|
|
@ -64,7 +126,17 @@ export const PaymentsTab: React.FC<PaymentsTabProps> = ({
|
|||
});
|
||||
|
||||
return result;
|
||||
}, [payments, searchQuery, sortOrder]);
|
||||
}, [payments, searchQuery, sortOrder, selectedYear, selectedMonth]);
|
||||
|
||||
const hasDateFilter = selectedYear !== 'all' || selectedMonth !== 'all';
|
||||
|
||||
const handleClearDateFilters = () => {
|
||||
setSelectedYear('all');
|
||||
setSelectedMonth('all');
|
||||
setIsCustomYear(false);
|
||||
setCustomYearInput('');
|
||||
onPageChange(1);
|
||||
};
|
||||
|
||||
const totalFiltered = processedPayments.length;
|
||||
const totalPages = Math.max(1, Math.ceil(totalFiltered / perPage));
|
||||
|
|
@ -75,35 +147,141 @@ export const PaymentsTab: React.FC<PaymentsTabProps> = ({
|
|||
return (
|
||||
<div className="space-y-4">
|
||||
{/* Search & Sort Controls Header */}
|
||||
<div className="bg-slate-50 border border-slate-200/80 rounded-2xl p-4 flex flex-col sm:flex-row justify-between items-stretch sm:items-center gap-3">
|
||||
{/* Search Input */}
|
||||
<div className="flex-1 max-w-md relative">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Search payment ID, order ID, email, method, invoice..."
|
||||
value={searchQuery}
|
||||
onChange={(e) => {
|
||||
setSearchQuery(e.target.value);
|
||||
onPageChange(1);
|
||||
}}
|
||||
className="w-full bg-white border border-slate-200 rounded-xl pl-9 pr-8 py-2 text-xs text-slate-800 font-semibold focus:outline-none focus:border-indigo-500 placeholder:text-slate-400 shadow-sm"
|
||||
/>
|
||||
<Search className="h-3.5 w-3.5 absolute left-3 top-1/2 -translate-y-1/2 text-slate-400" />
|
||||
{searchQuery && (
|
||||
<button
|
||||
onClick={() => {
|
||||
setSearchQuery('');
|
||||
<div className="bg-slate-50 border border-slate-200/80 rounded-2xl p-4 flex flex-col xl:flex-row justify-between items-stretch xl:items-center gap-3">
|
||||
{/* Left Side: Search & Date Filters */}
|
||||
<div className="flex flex-col sm:flex-row items-stretch sm:items-center gap-3 flex-1">
|
||||
{/* Search Input */}
|
||||
<div className="flex-1 max-w-md relative">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Search payment ID, order ID, email, method, invoice..."
|
||||
value={searchQuery}
|
||||
onChange={(e) => {
|
||||
setSearchQuery(e.target.value);
|
||||
onPageChange(1);
|
||||
}}
|
||||
className="absolute right-2.5 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-600 p-0.5"
|
||||
>
|
||||
<X className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
)}
|
||||
className="w-full bg-white border border-slate-200 rounded-xl pl-9 pr-8 py-2 text-xs text-slate-800 font-semibold focus:outline-none focus:border-indigo-500 placeholder:text-slate-400 shadow-sm"
|
||||
/>
|
||||
<Search className="h-3.5 w-3.5 absolute left-3 top-1/2 -translate-y-1/2 text-slate-400" />
|
||||
{searchQuery && (
|
||||
<button
|
||||
onClick={() => {
|
||||
setSearchQuery('');
|
||||
onPageChange(1);
|
||||
}}
|
||||
className="absolute right-2.5 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-600 p-0.5 cursor-pointer"
|
||||
>
|
||||
<X className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Date Filter Controls: Year & Month */}
|
||||
<div className="flex items-center gap-2 shrink-0">
|
||||
{/* Year Selector (Dynamic with Custom Year support) */}
|
||||
{!isCustomYear ? (
|
||||
<div className="relative">
|
||||
<Calendar className="h-3.5 w-3.5 absolute left-3 top-1/2 -translate-y-1/2 text-slate-400 pointer-events-none" />
|
||||
<select
|
||||
value={selectedYear}
|
||||
onChange={(e) => {
|
||||
const val = e.target.value;
|
||||
if (val === 'custom') {
|
||||
setIsCustomYear(true);
|
||||
setCustomYearInput('');
|
||||
} else {
|
||||
setSelectedYear(val);
|
||||
onPageChange(1);
|
||||
}
|
||||
}}
|
||||
className="appearance-none bg-white border border-slate-200 rounded-xl pl-8.5 pr-8 py-2 text-xs font-bold text-slate-700 hover:border-slate-300 focus:outline-none focus:border-indigo-500 shadow-2xs cursor-pointer transition-colors"
|
||||
title="Filter payments by year"
|
||||
>
|
||||
<option value="all">All Years</option>
|
||||
{availableYears.map((yr) => (
|
||||
<option key={yr} value={String(yr)}>
|
||||
{yr}
|
||||
</option>
|
||||
))}
|
||||
<option value="custom">Custom Year...</option>
|
||||
</select>
|
||||
<ChevronDown className="h-3.5 w-3.5 absolute right-2.5 top-1/2 -translate-y-1/2 text-slate-400 pointer-events-none" />
|
||||
</div>
|
||||
) : (
|
||||
<div className="relative flex items-center">
|
||||
<Calendar className="h-3.5 w-3.5 absolute left-3 top-1/2 -translate-y-1/2 text-indigo-500 pointer-events-none" />
|
||||
<input
|
||||
type="text"
|
||||
autoFocus
|
||||
maxLength={4}
|
||||
placeholder="Year (e.g. 2002)"
|
||||
value={customYearInput}
|
||||
onChange={(e) => {
|
||||
const val = e.target.value.replace(/\D/g, '').slice(0, 4);
|
||||
setCustomYearInput(val);
|
||||
if (val.length === 4) {
|
||||
setSelectedYear(val);
|
||||
onPageChange(1);
|
||||
} else {
|
||||
setSelectedYear('all');
|
||||
onPageChange(1);
|
||||
}
|
||||
}}
|
||||
className="w-34 bg-white border border-indigo-400 rounded-xl pl-8.5 pr-7 py-2 text-xs font-bold text-slate-800 placeholder:text-slate-400 focus:outline-none focus:ring-1 focus:ring-indigo-500 shadow-2xs transition-colors"
|
||||
title="Enter any custom year"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setIsCustomYear(false);
|
||||
setCustomYearInput('');
|
||||
setSelectedYear('all');
|
||||
onPageChange(1);
|
||||
}}
|
||||
className="absolute right-2 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-600 p-0.5 cursor-pointer"
|
||||
title="Back to year dropdown"
|
||||
>
|
||||
<X className="h-3 w-3" />
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Month Selector */}
|
||||
<div className="relative">
|
||||
<select
|
||||
value={selectedMonth}
|
||||
onChange={(e) => {
|
||||
setSelectedMonth(e.target.value);
|
||||
onPageChange(1);
|
||||
}}
|
||||
className="appearance-none bg-white border border-slate-200 rounded-xl pl-3.5 pr-8 py-2 text-xs font-bold text-slate-700 hover:border-slate-300 focus:outline-none focus:border-indigo-500 shadow-2xs cursor-pointer transition-colors"
|
||||
title="Filter payments by month"
|
||||
>
|
||||
<option value="all">All Months</option>
|
||||
{MONTH_OPTIONS.map((m) => (
|
||||
<option key={m.value} value={m.value}>
|
||||
{m.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<ChevronDown className="h-3.5 w-3.5 absolute right-2.5 top-1/2 -translate-y-1/2 text-slate-400 pointer-events-none" />
|
||||
</div>
|
||||
|
||||
{hasDateFilter && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleClearDateFilters}
|
||||
className="p-1.5 text-slate-400 hover:text-slate-600 hover:bg-slate-200/60 rounded-lg transition-colors cursor-pointer"
|
||||
title="Reset year and month filters"
|
||||
>
|
||||
<X className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Sort & Count Controls */}
|
||||
<div className="flex items-center gap-3 justify-between sm:justify-end">
|
||||
{/* Right Side: Sort & Count Controls */}
|
||||
<div className="flex items-center gap-3 justify-between sm:justify-end shrink-0">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setSortOrder(prev => prev === 'desc' ? 'asc' : 'desc')}
|
||||
|
|
@ -134,13 +312,28 @@ export const PaymentsTab: React.FC<PaymentsTabProps> = ({
|
|||
<div className="border border-slate-200 rounded-2xl p-10 bg-white text-center space-y-2">
|
||||
<p className="text-sm font-bold text-slate-700">No matching payments found</p>
|
||||
<p className="text-xs text-slate-400">
|
||||
No payments match your search query "{searchQuery}".
|
||||
No payments match your
|
||||
{hasDateFilter && (
|
||||
<span>
|
||||
{' '}selected period (
|
||||
<strong className="text-slate-600">
|
||||
{selectedMonth !== 'all' ? MONTH_OPTIONS.find((m) => m.value === selectedMonth)?.label : ''}
|
||||
{selectedMonth !== 'all' && selectedYear !== 'all' ? ' ' : ''}
|
||||
{selectedYear !== 'all' ? selectedYear : ''}
|
||||
</strong>
|
||||
)
|
||||
</span>
|
||||
)}
|
||||
{searchQuery ? ` and search query "${searchQuery}"` : ''}.
|
||||
</p>
|
||||
<button
|
||||
onClick={() => setSearchQuery('')}
|
||||
className="text-xs text-indigo-600 font-bold hover:underline mt-2 inline-block"
|
||||
onClick={() => {
|
||||
setSearchQuery('');
|
||||
handleClearDateFilters();
|
||||
}}
|
||||
className="text-xs text-indigo-600 font-bold hover:underline mt-2 inline-block cursor-pointer"
|
||||
>
|
||||
Clear search query
|
||||
Reset all filters
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
|
|
|
|||
Loading…
Reference in New Issue