189 lines
6.1 KiB
JavaScript
189 lines
6.1 KiB
JavaScript
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
import { Container, Form } from 'react-bootstrap';
|
|
import { isSuccess, signedGet } from '@/api/client';
|
|
import { useAuth } from '@/auth/AuthProvider';
|
|
import PageBreadcrumb from '@/components/PageBreadcrumb';
|
|
import HierarchyFilter from './HierarchyFilter';
|
|
import ReportSection from './ReportSection';
|
|
import {
|
|
buildActivitySummary,
|
|
buildCenterSummary,
|
|
buildMonthlySummary,
|
|
buildTransplantPenetration,
|
|
buildTransplantSummary,
|
|
buildTrendSummary,
|
|
buildWeeklySummary,
|
|
currentMonth,
|
|
HIERARCHY_PARTITION,
|
|
REPORT_TIMEOUT,
|
|
reportData,
|
|
reportMonths,
|
|
} from './helpers';
|
|
|
|
const SECTIONS = [
|
|
{
|
|
id: 'monthly',
|
|
title: (month) => `Monthly Summary (${month})`,
|
|
path: 'gennovadc_reports/monthly_summary',
|
|
build: buildMonthlySummary,
|
|
filename: 'monthsummary-report.xlsx',
|
|
},
|
|
{
|
|
id: 'weekly',
|
|
title: (month) => `Weekly Summary (${month})`,
|
|
path: 'gennovadc_reports/weekly_summary',
|
|
build: buildWeeklySummary,
|
|
filename: 'weekly-summary.xlsx',
|
|
},
|
|
{
|
|
id: 'center',
|
|
title: () => 'Center Summary',
|
|
path: 'gennovadc_reports/center_summary',
|
|
build: buildCenterSummary,
|
|
filename: 'center-summary.xlsx',
|
|
},
|
|
{
|
|
id: 'centerTrend',
|
|
title: () => 'Center Trend Summary',
|
|
path: 'gennovadc_reports/center_trend_summary',
|
|
build: buildTrendSummary,
|
|
filename: 'center-trend-summary.xlsx',
|
|
monthless: true,
|
|
},
|
|
{
|
|
id: 'unitTrend',
|
|
title: () => 'Unit Trend Summary',
|
|
path: 'gennovadc_reports/unit_trend_summary',
|
|
build: buildTrendSummary,
|
|
filename: 'unit-trend-summary.xlsx',
|
|
monthless: true,
|
|
},
|
|
{
|
|
id: 'transplantPenetration',
|
|
title: () => 'Transplant Penetration',
|
|
path: 'gennovadc_reports/transplant_penetration',
|
|
build: buildTransplantPenetration,
|
|
filename: 'transplant-penetration.xlsx',
|
|
sticky: true,
|
|
},
|
|
{
|
|
id: 'transplantSummary',
|
|
title: () => 'Transplant Summary',
|
|
path: 'gennovadc_reports/transplant_summary',
|
|
build: buildTransplantSummary,
|
|
filename: 'transplant-summary.xlsx',
|
|
sticky: true,
|
|
},
|
|
{
|
|
id: 'activity',
|
|
title: () => 'Activity Summary',
|
|
path: 'gennovadc_reports/activity_summary',
|
|
build: buildActivitySummary,
|
|
filename: 'activity-summary.xlsx',
|
|
sticky: true,
|
|
},
|
|
];
|
|
|
|
const MONTH_SECTIONS = SECTIONS.filter((section) => !section.monthless);
|
|
const TREND_SECTIONS = SECTIONS.filter((section) => section.monthless);
|
|
const EMPTY_TABLE = { columns: [], rows: [], total: null };
|
|
|
|
export default function GennovadcDashboard() {
|
|
const { hasPermission } = useAuth();
|
|
const allDataAccess = hasPermission('all_data_access');
|
|
const months = useMemo(() => reportMonths(), []);
|
|
const [month, setMonth] = useState(currentMonth);
|
|
const [hierarchies, setHierarchies] = useState([]);
|
|
const [hierarchyError, setHierarchyError] = useState('');
|
|
const [selectedHier, setSelectedHier] = useState([]);
|
|
const [results, setResults] = useState({});
|
|
|
|
const hierarchyIds = useMemo(() => JSON.stringify(selectedHier), [selectedHier]);
|
|
|
|
useEffect(() => {
|
|
const loadHierarchies = async () => {
|
|
const params = { partition: HIERARCHY_PARTITION };
|
|
if (allDataAccess) params.hierarchy_id = 'root';
|
|
const response = await signedGet('ums', 'hierarchy/hierarchy_below_me', params, { timeout: REPORT_TIMEOUT });
|
|
if (!isSuccess(response)) {
|
|
setHierarchyError(response?.message || 'Unable to load hierarchy');
|
|
return;
|
|
}
|
|
const list = Array.isArray(response.data?.hierarchies) ? [...response.data.hierarchies] : [];
|
|
list.sort((a, b) => Number(b.level || 0) - Number(a.level || 0));
|
|
setHierarchies(list);
|
|
};
|
|
loadHierarchies();
|
|
}, [allDataAccess]);
|
|
|
|
const loadSections = useCallback((sections, params, isCancelled) => {
|
|
setResults((prev) => {
|
|
const next = { ...prev };
|
|
sections.forEach((section) => {
|
|
next[section.id] = { ...prev[section.id], loading: true, error: '' };
|
|
});
|
|
return next;
|
|
});
|
|
sections.forEach(async (section) => {
|
|
const response = await signedGet('reports', section.path, params, { timeout: REPORT_TIMEOUT });
|
|
if (isCancelled()) return;
|
|
const ok = isSuccess(response) || response?.body !== undefined;
|
|
setResults((prev) => ({
|
|
...prev,
|
|
[section.id]: ok
|
|
? { loading: false, error: '', table: section.build(reportData(response)) }
|
|
: { loading: false, error: response?.message || 'Unable to load report', table: EMPTY_TABLE },
|
|
}));
|
|
});
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
loadSections(MONTH_SECTIONS, { month, hierarchy_ids: hierarchyIds }, () => cancelled);
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [month, hierarchyIds, loadSections]);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
loadSections(TREND_SECTIONS, { hierarchy_ids: hierarchyIds }, () => cancelled);
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [hierarchyIds, loadSections]);
|
|
|
|
return (
|
|
<Container fluid>
|
|
<div className="d-flex flex-wrap align-items-center justify-content-between gap-2">
|
|
<PageBreadcrumb title="MYDTx Dashboard" />
|
|
<div className="d-flex align-items-center gap-2">
|
|
<HierarchyFilter options={hierarchies} selected={selectedHier} onApply={setSelectedHier} />
|
|
<Form.Select value={month} onChange={(event) => setMonth(event.target.value)} style={{ width: 170 }}>
|
|
{months.map((option) => (
|
|
<option key={option} value={option}>
|
|
{option}
|
|
</option>
|
|
))}
|
|
</Form.Select>
|
|
</div>
|
|
</div>
|
|
{hierarchyError ? <div className="alert alert-warning">{hierarchyError}</div> : null}
|
|
{SECTIONS.map((section, index) => {
|
|
const result = results[section.id] || {};
|
|
return (
|
|
<ReportSection
|
|
key={section.id}
|
|
title={`${index + 1}. ${section.title(month)}`}
|
|
table={result.table || EMPTY_TABLE}
|
|
loading={result.loading ?? true}
|
|
error={result.error}
|
|
filename={section.filename}
|
|
stickyFirstColumn={section.sticky}
|
|
/>
|
|
);
|
|
})}
|
|
</Container>
|
|
);
|
|
}
|