166 lines
5.4 KiB
JavaScript
166 lines
5.4 KiB
JavaScript
import { useState } from 'react';
|
|
|
|
/**
|
|
* @param {{
|
|
* id: string,
|
|
* title: string,
|
|
* description: string,
|
|
* endpoint: string,
|
|
* filename: string,
|
|
* baseParams?: Record<string, string>,
|
|
* fullReport?: { description: string, filename: string, help: string },
|
|
* }} props
|
|
*/
|
|
function ExportCard({ id, title, description, endpoint, filename, baseParams = {}, fullReport }) {
|
|
const [from, setFrom] = useState('');
|
|
const [to, setTo] = useState('');
|
|
const [full, setFull] = useState(false);
|
|
const [error, setError] = useState('');
|
|
const [busy, setBusy] = useState(false);
|
|
const useFull = Boolean(fullReport && full);
|
|
|
|
async function download(event) {
|
|
event.preventDefault();
|
|
setError('');
|
|
setBusy(true);
|
|
const params = new URLSearchParams(baseParams);
|
|
|
|
if (useFull) {
|
|
params.set('full', 'true');
|
|
}
|
|
|
|
if (from) {
|
|
params.set('from', from);
|
|
}
|
|
|
|
if (to) {
|
|
params.set('to', to);
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`${endpoint}?${params}`, {
|
|
credentials: 'include',
|
|
});
|
|
|
|
if (response.status === 401) {
|
|
window.location.assign('/admin/login');
|
|
return;
|
|
}
|
|
|
|
if (!response.ok) {
|
|
const data = await response.json().catch(() => ({}));
|
|
throw new Error(data?.error?.message || 'Export failed');
|
|
}
|
|
|
|
const blob = await response.blob();
|
|
const url = URL.createObjectURL(blob);
|
|
const link = document.createElement('a');
|
|
link.href = url;
|
|
link.download = useFull && fullReport ? fullReport.filename : filename;
|
|
link.click();
|
|
URL.revokeObjectURL(url);
|
|
} catch (downloadError) {
|
|
setError(downloadError.message);
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<form
|
|
className="admin-card grid gap-3"
|
|
onSubmit={download}
|
|
aria-labelledby={`${id}-heading`}
|
|
>
|
|
<div>
|
|
<h2 id={`${id}-heading`} className="font-semibold text-[#134e4a]">
|
|
{title}
|
|
</h2>
|
|
<p className="mt-1 text-sm text-stone-600">
|
|
{useFull && fullReport ? fullReport.description : description}
|
|
</p>
|
|
</div>
|
|
{fullReport ? (
|
|
<div className="grid grid-cols-[auto_1fr] items-start gap-x-2 gap-y-1 rounded-lg border border-stone-200 bg-[#fffaec] px-3 py-2.5">
|
|
<input
|
|
id={`${id}-full`}
|
|
type="checkbox"
|
|
className="mt-0.5 h-4 w-4 accent-[#0f766e]"
|
|
checked={full}
|
|
aria-describedby={`${id}-full-help`}
|
|
onChange={(event) => setFull(event.target.checked)}
|
|
/>
|
|
<label className="text-sm font-medium text-stone-700" htmlFor={`${id}-full`}>
|
|
Full report
|
|
</label>
|
|
<p id={`${id}-full-help`} className="col-start-2 m-0 text-xs text-stone-500">
|
|
{fullReport.help}
|
|
</p>
|
|
</div>
|
|
) : null}
|
|
<label className="grid gap-1 text-sm font-medium text-stone-600" htmlFor={`${id}-from`}>
|
|
From
|
|
<input
|
|
id={`${id}-from`}
|
|
type="date"
|
|
className="admin-input"
|
|
value={from}
|
|
onChange={(event) => setFrom(event.target.value)}
|
|
/>
|
|
</label>
|
|
<label className="grid gap-1 text-sm font-medium text-stone-600" htmlFor={`${id}-to`}>
|
|
To
|
|
<input
|
|
id={`${id}-to`}
|
|
type="date"
|
|
className="admin-input"
|
|
value={to}
|
|
onChange={(event) => setTo(event.target.value)}
|
|
/>
|
|
</label>
|
|
<button type="submit" className="admin-btn admin-btn-primary" disabled={busy}>
|
|
{busy ? 'Exporting…' : 'Export to Excel'}
|
|
</button>
|
|
{error ? (
|
|
<p className="admin-card-status admin-card-status-error" role="alert">
|
|
{error}
|
|
</p>
|
|
) : null}
|
|
</form>
|
|
);
|
|
}
|
|
|
|
export default function ReportsPage() {
|
|
return (
|
|
<div className="max-w-xl">
|
|
<h1 className="admin-title">Reports</h1>
|
|
<p className="mb-4 rounded-lg bg-amber-50 px-3 py-2 text-sm text-amber-900" role="status">
|
|
These files contain personal data.
|
|
</p>
|
|
<div className="grid gap-4">
|
|
<ExportCard
|
|
id="survey-export"
|
|
title="Export Survey Data"
|
|
description="Completed survey responses, filtered by completion date."
|
|
endpoint="/api/admin/survey/export"
|
|
filename="survey-responses.xlsx"
|
|
baseParams={{ status: 'COMPLETED' }}
|
|
/>
|
|
<ExportCard
|
|
id="applications-export"
|
|
title="Export Applications with consent"
|
|
description="Basic application and doctor profile details with consent and survey status, filtered by application created date. Excludes registration and bank details."
|
|
endpoint="/api/admin/applications/export"
|
|
filename="applications-consent.xlsx"
|
|
fullReport={{
|
|
description:
|
|
'Every application, consent and survey column, filtered by application created date. Includes territory/TM fields from the doctor list, registration details, full PAN and bank account numbers.',
|
|
filename: 'applications-consent-full.xlsx',
|
|
help: 'Tick this only when you need registration, bank, payment or territory details, for example to process honorarium payments. It adds TASKFORCE through TM Mobile from the doctor list, registration number, medical council, PAN, GSTIN, bank account details, cheque status, approval dates and each consent statement.',
|
|
}}
|
|
/>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|