import { useEffect, useState } from 'react'; import { Button, Col, FormCheck, FormControl, Modal, ModalBody, ModalFooter, ModalHeader, Row } from 'react-bootstrap'; // import { ASSESSMENT_FIELDS } from '@/components/SessionVitalsModal'; import { ASSESSMENT_FIELDS, BpInput, emptyAssessment, splitBp, vitalsPayload } from '@/components/SessionVitalsModal'; import { api } from '@/lib/api'; const blank = (value) => (value == null ? '' : value); // Every session of one patient (offline first), with vitals to view, fill, and save. export default function PatientVitalsModal({ patient, onClose }) { const [rows, setRows] = useState([]); const [loading, setLoading] = useState(false); const [saving, setSaving] = useState(false); const [error, setError] = useState(''); useEffect(() => { if (!patient?.id) return undefined; let active = true; setLoading(true); setError(''); api(`/api/onboarding/${patient.id}/vitals`).then((data) => { // if (active) setRows(data.items || []); if (active) { setRows((data.items || []).map((row) => ({ ...row, pre: { ...row.pre, ...splitBp(row.pre?.bp) }, post: { ...row.post, ...splitBp(row.post?.bp) }, }))); } }).catch((err) => { if (active) setError(err.message); }).finally(() => { if (active) setLoading(false); }); return () => { active = false; }; }, [patient?.id]); const setRow = (index, patch) => setRows((current) => current.map((row, i) => (i === index ? { ...row, ...patch } : row))); const save = async () => { setSaving(true); setError(''); try { // await api(`/api/onboarding/${patient.id}/vitals`, { method: 'PUT', body: { rows } }); const payload = rows.map((row) => ({ kind: row.kind, number: row.number, sessionId: row.sessionId, attended: Boolean(row.attended), pre: row.attended ? vitalsPayload(row.pre) : vitalsPayload({}), post: row.attended ? vitalsPayload(row.post) : vitalsPayload({}), assessment: row.attended ? row.assessment || {} : emptyAssessment(), })); await api(`/api/onboarding/${patient.id}/vitals`, { method: 'PUT', body: { rows: payload } }); onClose(); } catch (err) { setError(err.message); } finally { setSaving(false); } }; return (
Vitals · {patient?.name}
{error ?
{error}
: null} {loading ?

Loading…

: null} {!loading && !rows.length ?

No completed sessions yet.

: null} {rows.map((row, index) => (
Session {row.number || '—'} {[row.date, row.cohort].filter(Boolean).join(' · ')} setRow(index, { attended: e.target.checked })} />
{['pre', 'post'].map((when) => (
{when}
{/* setRow(index, { [when]: { ...row[when], bp: e.target.value } })} /> */} {/* setRow(index, { [when]: { ...row[when], sbp: e.target.value } })} /> */} {/* setRow(index, { [when]: { ...row[when], dbp: e.target.value } })} /> */} setRow(index, { [when]: next })} /> setRow(index, { [when]: { ...row[when], spo2: e.target.value } })} /> setRow(index, { [when]: { ...row[when], pulse: e.target.value } })} /> ))}
{[15, 30].includes(Number(row.number)) ? ( {ASSESSMENT_FIELDS.map(([key, label]) => ( setRow(index, { assessment: { ...row.assessment, [key]: e.target.value } })} /> ))} ) : null}
))}
); }