pagination added for establishment dashboard table
This commit is contained in:
parent
22472cac43
commit
dc75fd84f3
@ -22,48 +22,50 @@ const formatDateTime = (value) => {
|
|||||||
return formatted.replace(/\s?(am|pm)$/i, (match) => match.toUpperCase());
|
return formatted.replace(/\s?(am|pm)$/i, (match) => match.toUpperCase());
|
||||||
};
|
};
|
||||||
|
|
||||||
const getStatusClass = (status) => {
|
const getStatusBadge = (status) => {
|
||||||
const normalized = String(status || '').toLowerCase();
|
const normalized = String(status || '').toLowerCase();
|
||||||
|
|
||||||
if (normalized === 'approved') {
|
if (normalized === 'approved') {
|
||||||
return 'inline-flex items-center rounded-md bg-[#F5FAF6] px-2 py-1 text-xs font-medium text-[#2F663C]';
|
return (
|
||||||
|
<span className="px-3 py-1 rounded-md text-green-700 bg-green-50 font-medium text-sm whitespace-nowrap">
|
||||||
|
Approved
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
} else if (['pending', 'under review', 'in-progress'].includes(normalized)) {
|
||||||
|
return (
|
||||||
|
<span className="px-3 py-1 rounded-md text-blue-600 bg-blue-50 font-medium text-sm whitespace-nowrap">
|
||||||
|
Under Review
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
} else if (['rejected', 'returned'].includes(normalized)) {
|
||||||
|
return (
|
||||||
|
<span className="px-3 py-1 rounded-md text-red-600 bg-red-50 font-medium text-sm whitespace-nowrap">
|
||||||
|
Returned
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return (
|
||||||
|
<span className="px-3 py-1 rounded-md text-gray-600 bg-gray-50 font-medium text-sm whitespace-nowrap">
|
||||||
|
{status || '—'}
|
||||||
|
</span>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (['pending', 'under review', 'in-progress'].includes(normalized)) {
|
|
||||||
return 'inline-flex items-center rounded-md bg-[#FFFDF3] px-2 py-1 text-xs font-medium text-[#7C5E24]';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (['rejected', 'returned'].includes(normalized)) {
|
|
||||||
return 'inline-flex items-center rounded-md bg-[#FEF6F6] px-2 py-1 text-xs font-medium text-[#B52520]';
|
|
||||||
}
|
|
||||||
|
|
||||||
return 'inline-flex items-center rounded-md bg-[#F8FAFC] px-2 py-1 text-xs font-medium text-[#374151]';
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const downloadCsv = (data) => {
|
const downloadCsv = (data) => {
|
||||||
if (!data.length) return;
|
if (!data.length) return;
|
||||||
|
|
||||||
const headers = ['Survey Name', 'Year', 'Quarter', 'Status', 'Submission On', 'Products'];
|
const headers = ['Survey Name', 'Year', 'Quarter', 'Status', 'Submission On', 'Products'];
|
||||||
const csvRows = [headers.join(',')];
|
const csvRows = [headers.join(',')];
|
||||||
|
|
||||||
data.forEach((item) => {
|
data.forEach((item) => {
|
||||||
const surveyName = item.survey_name || '—';
|
const row = [
|
||||||
const year = item.year || '—';
|
item.survey_name || '—',
|
||||||
const quarter = item.quarter || '—';
|
item.year || '—',
|
||||||
|
item.quarter || '—',
|
||||||
const normalized = String(item.status || '').toLowerCase();
|
item.status || '—',
|
||||||
let status = '—';
|
item.submission_on || '—',
|
||||||
if (normalized === 'pending') status = 'Under Review';
|
item.products || '—',
|
||||||
else if (normalized === 'rejected') status = 'Returned';
|
];
|
||||||
else if (['submitted', 'approved'].includes(normalized)) status = 'Approved';
|
csvRows.push(row.map((cell) => `"${String(cell).replace(/"/g, '""')}"`).join(','));
|
||||||
|
|
||||||
const submissionOn = item.submission_on || '—';
|
|
||||||
|
|
||||||
const productCount = Number(item.products) || 0;
|
|
||||||
const products = `${productCount} ${productCount === 1 ? 'product' : 'products'}`;
|
|
||||||
|
|
||||||
const row = [surveyName, year, quarter, status, submissionOn, products];
|
|
||||||
csvRows.push(row.map(cell => `"${String(cell).replace(/"/g, '""')}"`).join(','));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const blob = new Blob([csvRows.join('\n')], { type: 'text/csv;charset=utf-8;' });
|
const blob = new Blob([csvRows.join('\n')], { type: 'text/csv;charset=utf-8;' });
|
||||||
@ -80,6 +82,8 @@ const downloadCsv = (data) => {
|
|||||||
export const SubmissionHistory = ({ history = [], loading = false, error = '' }) => {
|
export const SubmissionHistory = ({ history = [], loading = false, error = '' }) => {
|
||||||
const [searchTerm, setSearchTerm] = React.useState('');
|
const [searchTerm, setSearchTerm] = React.useState('');
|
||||||
const [selectedSubmission, setSelectedSubmission] = React.useState(null);
|
const [selectedSubmission, setSelectedSubmission] = React.useState(null);
|
||||||
|
const [currentPage, setCurrentPage] = React.useState(1);
|
||||||
|
const pageSize = 10;
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const normalizedHistory = React.useMemo(() => {
|
const normalizedHistory = React.useMemo(() => {
|
||||||
@ -95,48 +99,38 @@ export const SubmissionHistory = ({ history = [], loading = false, error = '' })
|
|||||||
}));
|
}));
|
||||||
}, [history]);
|
}, [history]);
|
||||||
|
|
||||||
const filteredRows = normalizedHistory.filter((entry) => {
|
const filtered = normalizedHistory.filter((entry) => {
|
||||||
const term = searchTerm.trim().toLowerCase();
|
const term = searchTerm.trim().toLowerCase();
|
||||||
return (
|
return (
|
||||||
!term ||
|
!term ||
|
||||||
|
entry.survey_name.toLowerCase().includes(term) ||
|
||||||
entry.year.toString().includes(term) ||
|
entry.year.toString().includes(term) ||
|
||||||
entry.quarter.toLowerCase().includes(term) ||
|
entry.quarter.toLowerCase().includes(term) ||
|
||||||
entry.status.toLowerCase().includes(term)
|
entry.status.toLowerCase().includes(term)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleViewDetails = (rowData) => {
|
const tableHeaders = [
|
||||||
setSelectedSubmission(rowData);
|
'Survey Name',
|
||||||
};
|
'Year',
|
||||||
|
'Quarter',
|
||||||
const rowsData = filteredRows.map((item) => {
|
'Status',
|
||||||
const productCount = Number(item.products) || 0;
|
'Submission On',
|
||||||
const productLabel = `${productCount} ${productCount === 1 ? 'product' : 'products'}`;
|
'Products',
|
||||||
|
'Actions',
|
||||||
|
];
|
||||||
|
|
||||||
|
const tableRows = filtered.map((item) => {
|
||||||
|
const productsLabel = `${item.products} ${item.products === 1 ? 'product' : 'products'}`;
|
||||||
return [
|
return [
|
||||||
item.survey_name,
|
item.survey_name,
|
||||||
item.year,
|
item.year,
|
||||||
item.quarter,
|
item.quarter,
|
||||||
|
getStatusBadge(item.status),
|
||||||
(() => {
|
|
||||||
const normalized = String(item.status || '').toLowerCase();
|
|
||||||
let displayStatus = '—';
|
|
||||||
|
|
||||||
if (normalized === 'pending') displayStatus = 'Under Review';
|
|
||||||
else if (normalized === 'rejected') displayStatus = 'Returned';
|
|
||||||
else if (['submitted', 'approved'].includes(normalized)) displayStatus = 'Approved';
|
|
||||||
|
|
||||||
return (
|
|
||||||
<span className={getStatusClass(displayStatus)}>
|
|
||||||
{displayStatus}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
})(),
|
|
||||||
|
|
||||||
<span className="whitespace-nowrap">{item.submission_on}</span>,
|
<span className="whitespace-nowrap">{item.submission_on}</span>,
|
||||||
productLabel,
|
productsLabel,
|
||||||
<button
|
<button
|
||||||
onClick={() => handleViewDetails(item)}
|
onClick={() => setSelectedSubmission(item)}
|
||||||
className="text-[#9E792B] hover:text-[#7b5f22] font-medium"
|
className="text-[#9E792B] hover:text-[#7b5f22] font-medium"
|
||||||
>
|
>
|
||||||
View Details
|
View Details
|
||||||
@ -144,11 +138,27 @@ export const SubmissionHistory = ({ history = [], loading = false, error = '' })
|
|||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
const toolbar = (
|
if (selectedSubmission) {
|
||||||
<div className="px-6 py-4 flex items-center justify-between border-b border-[#D9D9DC] bg-white">
|
return (
|
||||||
<h3 className="text-[16px] font-medium text-[#232528]">
|
<div className="min-h-screen bg-[#F8FAFC]">
|
||||||
|
<HeaderBar />
|
||||||
|
<main className="max-w-[1280px] mx-auto px-4 py-6">
|
||||||
|
<DetailedOverview
|
||||||
|
submission={selectedSubmission}
|
||||||
|
onBack={() => setSelectedSubmission(null)}
|
||||||
|
/>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div className="max-w-[1280px] mx-auto bg-white border border-[#E5E7EB] shadow-[0_16px_32px_rgba(15,23,42,0.06)] rounded-[8px] mt-8 w-full overflow-hidden">
|
||||||
|
<div className="flex items-center justify-between px-6 pt-6 pb-4">
|
||||||
|
<h2 className="text-[18px] leading-[28px] font-medium text-[#232528]">
|
||||||
Submission History
|
Submission History
|
||||||
</h3>
|
</h2>
|
||||||
|
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
<div className="relative w-56">
|
<div className="relative w-56">
|
||||||
@ -168,10 +178,10 @@ export const SubmissionHistory = ({ history = [], loading = false, error = '' })
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => downloadCsv(filteredRows)}
|
onClick={() => downloadCsv(filtered)}
|
||||||
disabled={!rowsData.length}
|
disabled={!filtered.length}
|
||||||
className={`inline-flex h-10 items-center gap-2 rounded-md border px-4 text-sm font-medium ${
|
className={`inline-flex h-10 items-center gap-2 rounded-md border px-4 text-sm font-medium ${
|
||||||
rowsData.length
|
filtered.length
|
||||||
? 'border-[#92722A] text-[#92722A] hover:bg-[#F2ECCF]'
|
? 'border-[#92722A] text-[#92722A] hover:bg-[#F2ECCF]'
|
||||||
: 'border-gray-200 text-gray-400 cursor-not-allowed'
|
: 'border-gray-200 text-gray-400 cursor-not-allowed'
|
||||||
}`}
|
}`}
|
||||||
@ -194,77 +204,34 @@ export const SubmissionHistory = ({ history = [], loading = false, error = '' })
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
|
||||||
|
|
||||||
if (selectedSubmission) {
|
<div className="overflow-x-auto">
|
||||||
return (
|
{loading ? (
|
||||||
<div className="min-h-screen bg-[#F8FAFC]">
|
<div className="flex items-center justify-center h-[500px] text-gray-500">
|
||||||
<HeaderBar />
|
Loading submissions...
|
||||||
<main className="max-w-[1280px] mx-auto px-4 py-6">
|
|
||||||
<DetailedOverview
|
|
||||||
submission={selectedSubmission}
|
|
||||||
onBack={() => setSelectedSubmission(null)}
|
|
||||||
/>
|
|
||||||
</main>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<section className="max-w-[1280px] mx-auto px-0">
|
|
||||||
<div className="overflow-x-auto border border-[#D9D9DC] shadow-sm bg-white">
|
|
||||||
{toolbar}
|
|
||||||
<table className="min-w-full border-collapse text-sm text-[#232528]">
|
|
||||||
<thead>
|
|
||||||
<tr className="bg-[#F2ECCF] text-left text-[13px] font-semibold text-[#232528]">
|
|
||||||
{[
|
|
||||||
'Survey Name',
|
|
||||||
'Year',
|
|
||||||
'Quarter',
|
|
||||||
'Status',
|
|
||||||
'Submission on',
|
|
||||||
'Products',
|
|
||||||
'Actions',
|
|
||||||
].map((header, index) => (
|
|
||||||
<th
|
|
||||||
key={index}
|
|
||||||
className="px-4 py-3 border-b border-[#D9D9DC] whitespace-nowrap"
|
|
||||||
>
|
|
||||||
{header}
|
|
||||||
</th>
|
|
||||||
))}
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
|
|
||||||
<tbody className="divide-y divide-[#D9D9DC] text-[13px] leading-[20px]">
|
|
||||||
{rowsData.length === 0 ? (
|
|
||||||
<tr>
|
|
||||||
<td colSpan="7" className="px-4 py-8 text-center text-gray-500">
|
|
||||||
{loading ? 'Loading...' : error ? error : 'No submissions found'}
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
) : (
|
) : (
|
||||||
rowsData.map((row, rowIndex) => (
|
<div className="overflow-x-auto">
|
||||||
<tr
|
<div className="min-w-[1200px]">
|
||||||
key={rowIndex}
|
<Table
|
||||||
className="hover:bg-[#FAFAFA] transition-colors duration-150"
|
headers={tableHeaders}
|
||||||
>
|
rows={tableRows}
|
||||||
{row.map((cell, cellIndex) => (
|
separated
|
||||||
<td
|
rowGapClass="border-spacing-y-1"
|
||||||
key={cellIndex}
|
pagination={{
|
||||||
className="px-4 py-3 align-middle whitespace-nowrap border-b border-[#D9D9DC]"
|
currentPage,
|
||||||
>
|
onPageChange: setCurrentPage,
|
||||||
{cell}
|
pageSize,
|
||||||
</td>
|
totalItems: filtered.length,
|
||||||
))}
|
}}
|
||||||
</tr>
|
columnWidths={[250, 100, 100, 160, 180, 150, 120]}
|
||||||
))
|
/>
|
||||||
)}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<Footer />
|
<Footer />
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user