# Resolve 2 Signatory image issue in the generated Agreement PDF.

# Step status cards are now in line and status color badges.
# Added save button in survey and a automatically dissapearing toast message for success and error.
# Check the numeric validations for survey fields and added field specific error messages instead of showing all in topside.
This commit is contained in:
Amey Bhagwat 2026-10-19 12:13:59 +05:30
parent bb3f8b37a0
commit 29f879c5d1
8 changed files with 571 additions and 266 deletions

View File

@ -2,20 +2,13 @@ import { readFileSync } from 'node:fs';
import path from 'node:path'; import path from 'node:path';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
const ROOT = path.dirname(fileURLToPath(import.meta.url)); const AGREEMENT_PATH = path.resolve(
const AGREEMENT_PATH = path.resolve(ROOT, '../../../files/docs/agreement.md'); path.dirname(fileURLToPath(import.meta.url)),
const LOGO_PATH = path.resolve(ROOT, '../../../frontend/src/images/CREST_logo.jpg'); '../../../files/docs/agreement.md',
);
const sourceText = readFileSync(AGREEMENT_PATH, 'utf8'); const sourceText = readFileSync(AGREEMENT_PATH, 'utf8');
/** @type {string} */
let crestLogoDataUrl = '';
try {
crestLogoDataUrl = `data:image/jpeg;base64,${readFileSync(LOGO_PATH).toString('base64')}`;
} catch {
crestLogoDataUrl = '';
}
const MONTHS = [ const MONTHS = [
'January', 'January',
'February', 'February',
@ -87,18 +80,6 @@ function physicianSignatureHtml(signature) {
return '___________________'; return '___________________';
} }
/**
* @param {string} signatureMarkup
* @param {{ withLogo?: boolean }} [options]
*/
function signatureRow(signatureMarkup, options = {}) {
const logo =
options.withLogo && crestLogoDataUrl
? `<img class="crest-logo" alt="CREST" src="${crestLogoDataUrl}" />`
: '';
return `<span class="signature-row"><span class="signature-field">Signature: ${signatureMarkup}</span>${logo}</span>`;
}
/** /**
* @param {{ name: string, designation: string, signatureDataUrl?: string } | null} signatory * @param {{ name: string, designation: string, signatureDataUrl?: string } | null} signatory
* @param {string} dateLabel * @param {string} dateLabel
@ -115,7 +96,7 @@ function crestBlock(signatory, dateLabel) {
return [ return [
`Name: ${filled(signatory.name)}`, `Name: ${filled(signatory.name)}`,
`Designation: ${filled(signatory.designation)}`, `Designation: ${filled(signatory.designation)}`,
signatureRow(image, { withLogo: true }), `Signature: ${image}`,
`Date: ${filled(dateLabel)}`, `Date: ${filled(dateLabel)}`,
].join('\n\n'); ].join('\n\n');
} }
@ -306,10 +287,7 @@ export function agreementHtml(options) {
.filled { font-weight: 700; text-decoration: underline; text-underline-offset: 0.15em; } .filled { font-weight: 700; text-decoration: underline; text-underline-offset: 0.15em; }
.watermark { position: fixed; top: 45%; left: -10%; width: 120%; text-align: center; font-size: 42pt; font-weight: 700; letter-spacing: 0.04em; color: rgba(185, 28, 28, 0.18); transform: rotate(-32deg); z-index: 10; } .watermark { position: fixed; top: 45%; left: -10%; width: 120%; text-align: center; font-size: 42pt; font-weight: 700; letter-spacing: 0.04em; color: rgba(185, 28, 28, 0.18); transform: rotate(-32deg); z-index: 10; }
.typed-signature { font-family: "Segoe Script", "Apple Chancery", cursive; font-size: 20pt; } .typed-signature { font-family: "Segoe Script", "Apple Chancery", cursive; font-size: 20pt; }
.signature-row { display: flex; justify-content: space-between; align-items: center; gap: 1rem; width: 100%; }
.signature-field { display: inline-flex; align-items: center; gap: 0.5rem; min-height: 64px; }
img.signature { height: 64px; max-width: 240px; width: auto; vertical-align: middle; background: #fff; } img.signature { height: 64px; max-width: 240px; width: auto; vertical-align: middle; background: #fff; }
img.crest-logo { height: 52px; width: auto; flex: none; }
</style> </style>
</head> </head>
<body> <body>

View File

@ -70,8 +70,8 @@ describe('agreement HTML', () => {
expect(html).toContain('<span class="filled">Dr Sudeep Salvi</span>'); expect(html).toContain('<span class="filled">Dr Sudeep Salvi</span>');
expect(html).toContain('<span class="filled">Medical Director</span>'); expect(html).toContain('<span class="filled">Medical Director</span>');
expect(html).toContain('data:image/png;base64,AAAA'); expect(html).toContain('data:image/png;base64,AAAA');
expect(html).toContain('class="signature-row"'); expect(html).toContain('alt="CREST signature"');
expect(html).toContain('alt="CREST"'); expect(html).not.toContain('alt="CREST"');
expect(html).not.toContain('Pending approval'); expect(html).not.toContain('Pending approval');
expect(html).not.toContain('DRAFT - NOT SIGNED'); expect(html).not.toContain('DRAFT - NOT SIGNED');
}); });

View File

@ -91,6 +91,20 @@
box-shadow: 0 1px 2px rgb(15 118 110 / 0.06), 0 18px 40px rgb(19 78 74 / 0.08); box-shadow: 0 1px 2px rgb(15 118 110 / 0.06), 0 18px 40px rgb(19 78 74 / 0.08);
} }
.admin-card-status {
margin: 0.75rem 0 0;
font-size: 0.9rem;
font-weight: 600;
}
.admin-card-status-success {
color: #14532d;
}
.admin-card-status-error {
color: #b91c1c;
}
.admin-stat-card { .admin-stat-card {
display: flex; display: flex;
flex-direction: column; flex-direction: column;

View File

@ -1,25 +1,62 @@
import { useEffect, useState } from 'react'; import { useEffect, useRef, useState } from 'react';
import { adminApi } from './adminApi.js'; import { adminApi } from './adminApi.js';
/**
* @typedef {'signatory' | 'signature' | 'reminders'} SettingsCard
* @typedef {{ message: string, tone: 'success' | 'error' }} CardMessage
*/
export default function SettingsPage() { export default function SettingsPage() {
const [settings, setSettings] = useState(null); const [settings, setSettings] = useState(null);
const [reminders, setReminders] = useState(null); const [reminders, setReminders] = useState(null);
const [error, setError] = useState(''); const [loadError, setLoadError] = useState('');
const [status, setStatus] = useState(''); /** @type {[Partial<Record<SettingsCard, CardMessage>>, Function]} */
const [cardMessages, setCardMessages] = useState({});
const timersRef = useRef(
/** @type {Partial<Record<SettingsCard, ReturnType<typeof setTimeout>>>} */ ({}),
);
useEffect(() => { useEffect(() => {
adminApi('/api/admin/settings') adminApi('/api/admin/settings')
.then(setSettings) .then(setSettings)
.catch((loadError) => setError(loadError.message)); .catch((error) => setLoadError(error.message));
adminApi('/api/admin/settings/reminders') adminApi('/api/admin/settings/reminders')
.then(setReminders) .then(setReminders)
.catch((loadError) => setError(loadError.message)); .catch((error) => setLoadError(error.message));
return () => {
for (const timer of Object.values(timersRef.current)) {
if (timer) {
clearTimeout(timer);
}
}
};
}, []); }, []);
/**
* @param {SettingsCard} card
* @param {string} message
* @param {'success' | 'error'} [tone]
*/
function showCardMessage(card, message, tone = 'success') {
const existing = timersRef.current[card];
if (existing) {
clearTimeout(existing);
}
setCardMessages((current) => ({ ...current, [card]: { message, tone } }));
timersRef.current[card] = setTimeout(() => {
setCardMessages((current) => {
const next = { ...current };
delete next[card];
return next;
});
delete timersRef.current[card];
}, 5000);
}
async function save(event) { async function save(event) {
event.preventDefault(); event.preventDefault();
setError('');
setStatus('');
try { try {
setSettings( setSettings(
@ -28,9 +65,9 @@ export default function SettingsPage() {
body: { name: settings.name, designation: settings.designation }, body: { name: settings.name, designation: settings.designation },
}), }),
); );
setStatus('Settings saved'); showCardMessage('signatory', 'Settings saved');
} catch (saveError) { } catch (saveError) {
setError(saveError.message); showCardMessage('signatory', saveError.message, 'error');
} }
} }
@ -43,9 +80,6 @@ export default function SettingsPage() {
const reader = new FileReader(); const reader = new FileReader();
reader.onload = async () => { reader.onload = async () => {
setError('');
setStatus('');
try { try {
setSettings( setSettings(
await adminApi('/api/admin/settings/signature', { await adminApi('/api/admin/settings/signature', {
@ -53,9 +87,9 @@ export default function SettingsPage() {
body: { image: String(reader.result) }, body: { image: String(reader.result) },
}), }),
); );
setStatus('Signature saved'); showCardMessage('signature', 'Signature saved');
} catch (uploadError) { } catch (uploadError) {
setError(uploadError.message); showCardMessage('signature', uploadError.message, 'error');
} }
}; };
reader.readAsDataURL(file); reader.readAsDataURL(file);
@ -63,8 +97,6 @@ export default function SettingsPage() {
async function saveReminders(event) { async function saveReminders(event) {
event.preventDefault(); event.preventDefault();
setError('');
setStatus('');
try { try {
setReminders( setReminders(
@ -77,14 +109,14 @@ export default function SettingsPage() {
}, },
}), }),
); );
setStatus('Reminder settings saved'); showCardMessage('reminders', 'Reminder settings saved');
} catch (saveError) { } catch (saveError) {
setError(saveError.message); showCardMessage('reminders', saveError.message, 'error');
} }
} }
if (!settings || !reminders) { if (!settings || !reminders) {
return error ? <p className="text-red-700">{error}</p> : <p>Loading settings…</p>; return loadError ? <p className="text-red-700">{loadError}</p> : <p>Loading settings…</p>;
} }
return ( return (
@ -115,6 +147,7 @@ export default function SettingsPage() {
<button type="submit" className="admin-btn admin-btn-primary"> <button type="submit" className="admin-btn admin-btn-primary">
Save Save
</button> </button>
<CardStatus message={cardMessages.signatory} />
</form> </form>
<div className="admin-card mt-4"> <div className="admin-card mt-4">
<h2 className="mb-2 font-semibold text-[#134e4a]">Signature image</h2> <h2 className="mb-2 font-semibold text-[#134e4a]">Signature image</h2>
@ -137,6 +170,7 @@ export default function SettingsPage() {
className="admin-file" className="admin-file"
onChange={upload} onChange={upload}
/> />
<CardStatus message={cardMessages.signature} />
</div> </div>
<form className="admin-card mt-4 grid gap-3" onSubmit={saveReminders}> <form className="admin-card mt-4 grid gap-3" onSubmit={saveReminders}>
<h2 className="font-semibold text-[#134e4a]">Reminders</h2> <h2 className="font-semibold text-[#134e4a]">Reminders</h2>
@ -181,9 +215,23 @@ export default function SettingsPage() {
<button type="submit" className="admin-btn admin-btn-primary"> <button type="submit" className="admin-btn admin-btn-primary">
Save reminders Save reminders
</button> </button>
<CardStatus message={cardMessages.reminders} />
</form> </form>
{error ? <p className="mt-3 text-red-700">{error}</p> : null}
{status ? <p className="mt-3">{status}</p> : null}
</div> </div>
); );
} }
/**
* @param {{ message?: CardMessage }} props
*/
function CardStatus({ message }) {
if (!message) {
return null;
}
return (
<p className={`admin-card-status admin-card-status-${message.tone}`} role="status">
{message.message}
</p>
);
}

View File

@ -773,6 +773,56 @@ a:focus-visible {
color: #1c1917; color: #1c1917;
} }
.survey-field {
display: grid;
gap: 0;
}
.survey-field-errors {
margin: 0.15rem 0 0.35rem;
}
.survey-field-errors .field-error {
margin: 0.15rem 0 0;
}
.survey-toast {
position: fixed;
right: 1.25rem;
/* Sit above the site footer */
bottom: 5.5rem;
z-index: 1000;
max-width: min(22rem, calc(100vw - 2rem));
padding: 0.65rem 0.9rem;
border-radius: 0.55rem;
box-shadow: var(--shadow);
font-size: 0.9rem;
font-weight: 500;
animation: survey-toast-in 0.2s ease-out;
}
.survey-toast-success {
background: rgba(15, 118, 110, 0.9);
color: #f0fdfa;
}
.survey-toast-error {
background: rgb(185, 28, 28, 0.9);
color: #fff;
}
@keyframes survey-toast-in {
from {
opacity: 0;
transform: translateY(0.4rem);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.survey-dialog { .survey-dialog {
position: fixed; position: fixed;
inset: 0; inset: 0;
@ -1018,19 +1068,33 @@ a:focus-visible {
.user-step-list { .user-step-list {
display: grid; display: grid;
grid-template-columns: 1fr;
gap: 0.75rem; gap: 0.75rem;
margin: 1.25rem 0; margin: 1.25rem 0;
} }
@media (min-width: 48rem) {
.user-step-list {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
}
.user-step { .user-step {
display: flex; display: flex;
flex-wrap: wrap; flex-direction: column;
justify-content: space-between; align-items: flex-start;
gap: 0.5rem; gap: 0.55rem;
padding: 0.85rem 1rem; padding: 0.95rem 1rem;
border: 1px solid var(--border); border: 1px solid var(--border);
border-radius: var(--radius-sm); border-radius: var(--radius-sm);
background: var(--surface-muted); background: var(--surface-muted);
min-width: 0;
}
.user-step strong {
font-size: 0.98rem;
line-height: 1.3;
color: var(--text);
} }
.user-step.done { .user-step.done {
@ -1038,9 +1102,35 @@ a:focus-visible {
background: #f0fdfa; background: #f0fdfa;
} }
.user-step span { .user-step-status {
color: var(--text-muted); display: inline-flex;
font-size: 0.9rem; align-items: center;
padding: 0.2rem 0.55rem;
border-radius: 999px;
font-size: 0.72rem;
font-weight: 700;
letter-spacing: 0.03em;
text-transform: uppercase;
}
.user-step-status-completed {
background: #dcfce7;
color: #14532d;
}
.user-step-status-in-process {
background: #fef3c7;
color: #92400e;
}
.user-step-status-not-started {
background: #fecaca;
color: #9f1233;
}
.user-step-status-rejected {
background: #fee2e2;
color: #991b1b;
} }
.user-next-hint { .user-next-hint {

View File

@ -1,4 +1,5 @@
import { useEffect, useRef, useState } from 'react'; import { useEffect, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import { Link, useParams } from 'react-router-dom'; import { Link, useParams } from 'react-router-dom';
import { validateAnswers } from '../../../backend/src/survey/validate.js'; import { validateAnswers } from '../../../backend/src/survey/validate.js';
import { surveyApi } from '../surveyApi.js'; import { surveyApi } from '../surveyApi.js';
@ -70,10 +71,44 @@ function formatAnswer(question, value) {
} }
/** /**
* @param {{ question: Record<string, unknown>, value: unknown, invalid?: boolean, hideLabel?: boolean, onChange: (value: unknown) => void }} props * @param {{ messages: string[] }} props
*/ */
function QuestionField({ question, value, invalid = false, hideLabel = false, onChange }) { function FieldErrors({ messages }) {
if (!messages.length) {
return null;
}
return (
<div className="survey-field-errors" role="alert">
{messages.map((message) => (
<p key={message} className="field-error">
* {message}
</p>
))}
</div>
);
}
/**
* @param {{
* question: Record<string, unknown>,
* value: unknown,
* invalid?: boolean,
* errorMessages?: string[],
* hideLabel?: boolean,
* onChange: (value: unknown) => void,
* }} props
*/
function QuestionField({
question,
value,
invalid = false,
errorMessages = [],
hideLabel = false,
onChange,
}) {
const legendClass = hideLabel ? 'visually-hidden' : undefined; const legendClass = hideLabel ? 'visually-hidden' : undefined;
const errors = <FieldErrors messages={errorMessages} />;
if (question.type === 'help') { if (question.type === 'help') {
return <p className="survey-help">{question.text}</p>; return <p className="survey-help">{question.text}</p>;
@ -86,204 +121,220 @@ function QuestionField({ question, value, invalid = false, hideLabel = false, on
if (question.type === 'short_text' || question.type === 'long_text') { if (question.type === 'short_text' || question.type === 'long_text') {
const Input = question.type === 'long_text' ? 'textarea' : 'input'; const Input = question.type === 'long_text' ? 'textarea' : 'input';
return ( return (
<label> <div className="survey-field">
{question.label} <label>
<Input {question.label}
value={typeof value === 'string' ? value : ''} <Input
onChange={(event) => onChange(event.target.value)} aria-invalid={invalid || undefined}
/> value={typeof value === 'string' ? value : ''}
</label> onChange={(event) => onChange(event.target.value)}
/>
</label>
{errors}
</div>
); );
} }
if (question.type === 'number' || question.type === 'percentage') { if (question.type === 'number' || question.type === 'percentage') {
return ( return (
<label> <div className="survey-field">
{question.label} <label>
<input {question.label}
type="number" <input
inputMode={question.integer ? 'numeric' : 'decimal'} type="number"
min={question.min} inputMode={question.integer ? 'numeric' : 'decimal'}
max={question.max} min={question.min}
aria-invalid={invalid || undefined} max={question.max}
value={value === undefined || value === null ? '' : value} aria-invalid={invalid || undefined}
onChange={(event) => { value={value === undefined || value === null ? '' : value}
const raw = event.target.value; onChange={(event) => {
onChange(raw === '' ? '' : Number(raw)); const raw = event.target.value;
}} onChange(raw === '' ? '' : Number(raw));
/> }}
</label> />
</label>
{errors}
</div>
); );
} }
if (question.type === 'single_choice' || question.type === 'multi_choice') { if (question.type === 'single_choice' || question.type === 'multi_choice') {
const current = value && typeof value === 'object' ? value : {}; const current = value && typeof value === 'object' ? value : {};
return ( return (
<fieldset> <div className="survey-field">
<legend className={legendClass}>{question.label}</legend> <fieldset aria-invalid={invalid || undefined}>
{question.options.map((option) => ( <legend className={legendClass}>{question.label}</legend>
<label key={option.id} className="survey-choice"> {question.options.map((option) => (
<label key={option.id} className="survey-choice">
<input
type={question.type === 'single_choice' ? 'radio' : 'checkbox'}
name={question.id}
checked={
question.type === 'single_choice'
? current.choice === option.id
: (current.choices ?? []).includes(option.id)
}
onChange={() => {
if (question.type === 'single_choice') {
onChange({ choice: option.id, other: current.other ?? '' });
return;
}
const choices = new Set(current.choices ?? []);
if (choices.has(option.id)) {
choices.delete(option.id);
} else {
choices.add(option.id);
}
onChange({ choices: [...choices], other: current.other ?? '' });
}}
/>
{option.label}
</label>
))}
{question.other &&
(current.choice === 'other' || (current.choices ?? []).includes('other')) ? (
<input <input
type={question.type === 'single_choice' ? 'radio' : 'checkbox'} aria-label={`${question.label} other`}
name={question.id} value={current.other ?? ''}
checked={ onChange={(event) => onChange({ ...current, other: event.target.value })}
question.type === 'single_choice'
? current.choice === option.id
: (current.choices ?? []).includes(option.id)
}
onChange={() => {
if (question.type === 'single_choice') {
onChange({ choice: option.id, other: current.other ?? '' });
return;
}
const choices = new Set(current.choices ?? []);
if (choices.has(option.id)) {
choices.delete(option.id);
} else {
choices.add(option.id);
}
onChange({ choices: [...choices], other: current.other ?? '' });
}}
/> />
{option.label} ) : null}
</label> </fieldset>
))} {errors}
{question.other && </div>
(current.choice === 'other' || (current.choices ?? []).includes('other')) ? (
<input
aria-label={`${question.label} other`}
value={current.other ?? ''}
onChange={(event) => onChange({ ...current, other: event.target.value })}
/>
) : null}
</fieldset>
); );
} }
if (question.type === 'grid_percentage' || question.type === 'grid_number') { if (question.type === 'grid_percentage' || question.type === 'grid_number') {
const grid = value && typeof value === 'object' ? value : {}; const grid = value && typeof value === 'object' ? value : {};
return ( return (
<fieldset> <div className="survey-field">
<legend className={legendClass}>{question.label}</legend> <fieldset aria-invalid={invalid || undefined}>
<table className="survey-grid"> <legend className={legendClass}>{question.label}</legend>
<thead> <table className="survey-grid">
<tr> <thead>
<th scope="col">Pack type</th> <tr>
{question.columns.map((column) => ( <th scope="col">Pack type</th>
<th key={column.id} scope="col">
{column.label}
</th>
))}
</tr>
</thead>
<tbody>
{question.rows.map((row) => (
<tr key={row.id}>
<th scope="row">{row.label}</th>
{question.columns.map((column) => ( {question.columns.map((column) => (
<td key={column.id}> <th key={column.id} scope="col">
<span className="cell-label">{column.label}</span> {column.label}
<div className="survey-grid-input"> </th>
<input
type="number"
inputMode={question.integer ? 'numeric' : 'decimal'}
min={question.min}
max={question.max}
aria-invalid={invalid || undefined}
aria-label={`${row.label}, ${column.label}`}
value={grid[row.id]?.[column.id] ?? ''}
onChange={(event) => {
const raw = event.target.value;
onChange({
...grid,
[row.id]: {
...(grid[row.id] ?? {}),
[column.id]: raw === '' ? '' : Number(raw),
},
});
}}
/>
{question.suffix ? (
<span className="survey-grid-suffix">{question.suffix}</span>
) : null}
</div>
</td>
))} ))}
</tr> </tr>
))} </thead>
</tbody> <tbody>
{question.type === 'grid_percentage' ? ( {question.rows.map((row) => (
<tfoot> <tr key={row.id}>
<tr> <th scope="row">{row.label}</th>
<th scope="row">Total</th> {question.columns.map((column) => (
{question.columns.map((column) => {
const numbers = question.rows.map((row) => grid[row.id]?.[column.id]);
const filled = numbers.filter((cell) => typeof cell === 'number');
const total = filled.reduce((sum, cell) => sum + cell, 0);
const ready = filled.length > 0;
const exact = ready && total === question.sumTo;
return (
<td key={column.id}> <td key={column.id}>
<span className={exact || !ready ? 'survey-total' : 'survey-total warn'}> <span className="cell-label">{column.label}</span>
{ready <div className="survey-grid-input">
? exact <input
? `Total: ${total}%` type="number"
: `Total: ${total}% — must equal ${question.sumTo}%` inputMode={question.integer ? 'numeric' : 'decimal'}
: 'Total: —'} min={question.min}
</span> max={question.max}
aria-invalid={invalid || undefined}
aria-label={`${row.label}, ${column.label}`}
value={grid[row.id]?.[column.id] ?? ''}
onChange={(event) => {
const raw = event.target.value;
onChange({
...grid,
[row.id]: {
...(grid[row.id] ?? {}),
[column.id]: raw === '' ? '' : Number(raw),
},
});
}}
/>
{question.suffix ? (
<span className="survey-grid-suffix">{question.suffix}</span>
) : null}
</div>
</td> </td>
); ))}
})} </tr>
</tr> ))}
</tfoot> </tbody>
) : null} {question.type === 'grid_percentage' ? (
</table> <tfoot>
</fieldset> <tr>
<th scope="row">Total</th>
{question.columns.map((column) => {
const numbers = question.rows.map((row) => grid[row.id]?.[column.id]);
const filled = numbers.filter((cell) => typeof cell === 'number');
const total = filled.reduce((sum, cell) => sum + cell, 0);
const ready = filled.length > 0;
const exact = ready && total === question.sumTo;
return (
<td key={column.id}>
<span className={exact || !ready ? 'survey-total' : 'survey-total warn'}>
{ready
? exact
? `Total: ${total}%`
: `Total: ${total}% — must equal ${question.sumTo}%`
: 'Total: —'}
</span>
</td>
);
})}
</tr>
</tfoot>
) : null}
</table>
</fieldset>
{errors}
</div>
); );
} }
if (question.type === 'likert_matrix') { if (question.type === 'likert_matrix') {
const selected = value && typeof value === 'object' ? value : {}; const selected = value && typeof value === 'object' ? value : {};
return ( return (
<fieldset> <div className="survey-field">
<legend className={legendClass}>{question.label}</legend> <fieldset aria-invalid={invalid || undefined}>
<table className="survey-grid"> <legend className={legendClass}>{question.label}</legend>
<thead> <table className="survey-grid">
<tr> <thead>
<th scope="col">Attribute</th> <tr>
{question.scale.map((point) => ( <th scope="col">Attribute</th>
<th key={point.id} scope="col">
{point.label}
</th>
))}
</tr>
</thead>
<tbody>
{question.rows.map((row) => (
<tr key={row.id}>
<th scope="row">{row.label}</th>
{question.scale.map((point) => ( {question.scale.map((point) => (
<td key={point.id} className="survey-grid-choice"> <th key={point.id} scope="col">
<span className="cell-label">{point.label}</span> {point.label}
<label className="survey-choice"> </th>
<input
type="radio"
name={`${question.id}-${row.id}`}
checked={selected[row.id] === point.id}
onChange={() => onChange({ ...selected, [row.id]: point.id })}
/>
<span className="visually-hidden">{point.label}</span>
</label>
</td>
))} ))}
</tr> </tr>
))} </thead>
</tbody> <tbody>
</table> {question.rows.map((row) => (
</fieldset> <tr key={row.id}>
<th scope="row">{row.label}</th>
{question.scale.map((point) => (
<td key={point.id} className="survey-grid-choice">
<span className="cell-label">{point.label}</span>
<label className="survey-choice">
<input
type="radio"
name={`${question.id}-${row.id}`}
checked={selected[row.id] === point.id}
onChange={() => onChange({ ...selected, [row.id]: point.id })}
/>
<span className="visually-hidden">{point.label}</span>
</label>
</td>
))}
</tr>
))}
</tbody>
</table>
</fieldset>
{errors}
</div>
); );
} }
@ -301,11 +352,14 @@ export default function SurveyPage() {
const [stepErrors, setStepErrors] = useState([]); const [stepErrors, setStepErrors] = useState([]);
const [confirming, setConfirming] = useState(false); const [confirming, setConfirming] = useState(false);
const [busy, setBusy] = useState(false); const [busy, setBusy] = useState(false);
const [saving, setSaving] = useState(false);
/** @type {[{ message: string, tone: 'success' | 'error' } | null, Function]} */
const [toast, setToast] = useState(null);
const suppressOpen = useRef(false); const suppressOpen = useRef(false);
const answersRef = useRef(answers); const answersRef = useRef(answers);
const dirtyRef = useRef(false); const dirtyRef = useRef(false);
const persistRef = useRef(async () => {}); const persistRef = useRef(async () => {});
const stepAlertRef = useRef(null); const toastTimerRef = useRef(/** @type {ReturnType<typeof setTimeout> | null} */ (null));
answersRef.current = answers; answersRef.current = answers;
useEffect(() => { useEffect(() => {
@ -333,8 +387,33 @@ export default function SurveyPage() {
}; };
}, [token]); }, [token]);
persistRef.current = async () => { /**
if (!dirtyRef.current) { * @param {string} message
* @param {'success' | 'error'} [tone]
*/
function showToast(message, tone = 'success') {
if (toastTimerRef.current) {
clearTimeout(toastTimerRef.current);
}
setToast({ message, tone });
toastTimerRef.current = setTimeout(() => {
setToast(null);
toastTimerRef.current = null;
}, 5000);
}
/**
* @param {{ force?: boolean, announce?: boolean }} [options]
*/
persistRef.current = async (options = {}) => {
const force = Boolean(options.force);
const announce = Boolean(options.announce);
if (!force && !dirtyRef.current) {
if (announce) {
showToast('Progress saved');
}
return; return;
} }
@ -345,9 +424,16 @@ export default function SurveyPage() {
method: 'PUT', method: 'PUT',
body: { answers: answersRef.current }, body: { answers: answersRef.current },
}); });
if (announce) {
showToast('Progress saved');
}
} catch (saveError) { } catch (saveError) {
dirtyRef.current = true; dirtyRef.current = true;
setError(saveError.message); if (announce) {
showToast(saveError.message, 'error');
} else {
setError(saveError.message);
}
} }
}; };
@ -362,6 +448,15 @@ export default function SurveyPage() {
return () => clearInterval(timer); return () => clearInterval(timer);
}, [screen, token]); }, [screen, token]);
useEffect(
() => () => {
if (toastTimerRef.current) {
clearTimeout(toastTimerRef.current);
}
},
[],
);
const shownSection = useRef(sectionIndex); const shownSection = useRef(sectionIndex);
useEffect(() => { useEffect(() => {
@ -374,18 +469,22 @@ export default function SurveyPage() {
page.scrollTo?.({ top: 0, behavior: 'smooth' }); page.scrollTo?.({ top: 0, behavior: 'smooth' });
}, [sectionIndex]); }, [sectionIndex]);
useEffect(() => {
if (stepErrors.length > 0) {
stepAlertRef.current?.scrollIntoView?.({ block: 'nearest' });
}
}, [stepErrors]);
function updateAnswer(id, value) { function updateAnswer(id, value) {
dirtyRef.current = true; dirtyRef.current = true;
setStepErrors([]); setStepErrors((current) => current.filter((item) => item.id !== id));
setAnswers((current) => ({ ...current, [id]: value })); setAnswers((current) => ({ ...current, [id]: value }));
} }
async function saveProgress() {
setSaving(true);
setError('');
try {
await persistRef.current({ force: true, announce: true });
} finally {
setSaving(false);
}
}
/** /**
* @param {{ questions: Record<string, unknown>[] }} section * @param {{ questions: Record<string, unknown>[] }} section
*/ */
@ -547,29 +646,26 @@ export default function SurveyPage() {
) : ( ) : (
<section> <section>
<h2>{section.title}</h2> <h2>{section.title}</h2>
{stepErrors.length > 0 ? (
<div ref={stepAlertRef} role="alert">
{stepErrors.map((item) => (
<p key={`${item.id}:${item.message}`} className="error">
{item.message}
</p>
))}
</div>
) : null}
<div className="survey-fields"> <div className="survey-fields">
{section.questions.map((question, index) => ( {section.questions.map((question, index) => {
<QuestionField const fieldErrors = stepErrors
key={question.id} .filter((item) => item.id === question.id)
question={question} .map((item) => item.message);
hideLabel={ return (
section.questions[index - 1]?.type === 'section_header' && <QuestionField
section.questions[index - 1].label === question.label key={question.id}
} question={question}
invalid={stepErrors.some((item) => item.id === question.id)} hideLabel={
value={answers[question.id]} section.questions[index - 1]?.type === 'section_header' &&
onChange={(value) => updateAnswer(question.id, value)} section.questions[index - 1].label === question.label
/> }
))} invalid={fieldErrors.length > 0}
errorMessages={fieldErrors}
value={answers[question.id]}
onChange={(value) => updateAnswer(question.id, value)}
/>
);
})}
</div> </div>
</section> </section>
)} )}
@ -591,16 +687,31 @@ export default function SurveyPage() {
Back Back
</button> </button>
) : null} ) : null}
<button type="button" className="secondary" disabled={busy || saving} onClick={saveProgress}>
{saving ? 'Saving…' : 'Save'}
</button>
{review ? ( {review ? (
<button type="button" disabled={busy} onClick={openConfirm}> <button type="button" disabled={busy || saving} onClick={openConfirm}>
Submit Submit
</button> </button>
) : ( ) : (
<button type="button" disabled={busy} onClick={goNext}> <button type="button" disabled={busy || saving} onClick={goNext}>
Next Next
</button> </button>
)} )}
</div> </div>
{toast
? createPortal(
<div
className={`survey-toast survey-toast-${toast.tone}`}
role="status"
aria-live="polite"
>
{toast.message}
</div>,
document.body,
)
: null}
{confirming ? ( {confirming ? (
<div className="survey-dialog"> <div className="survey-dialog">
<div role="dialog" aria-modal="true" aria-labelledby="submit-title"> <div role="dialog" aria-modal="true" aria-labelledby="submit-title">

View File

@ -68,7 +68,7 @@ describe('survey steps', () => {
fireEvent.click(screen.getByRole('button', { name: 'Next' })); fireEvent.click(screen.getByRole('button', { name: 'Next' }));
expect( expect(
await screen.findByText(`${PROPORTION} must be at most 100`), await screen.findByText((text) => text.includes(`${PROPORTION} must be at most 100`)),
).toBeInTheDocument(); ).toBeInTheDocument();
expect(screen.getByRole('heading', { name: 'Foracort prescribing pattern' })).toBeInTheDocument(); expect(screen.getByRole('heading', { name: 'Foracort prescribing pattern' })).toBeInTheDocument();
expect(screen.queryByRole('heading', { name: 'Notes' })).not.toBeInTheDocument(); expect(screen.queryByRole('heading', { name: 'Notes' })).not.toBeInTheDocument();
@ -80,7 +80,9 @@ describe('survey steps', () => {
await screen.findByLabelText(PROPORTION); await screen.findByLabelText(PROPORTION);
fireEvent.click(screen.getByRole('button', { name: 'Next' })); fireEvent.click(screen.getByRole('button', { name: 'Next' }));
expect(await screen.findByText(`${PROPORTION} is required`)).toBeInTheDocument(); expect(
await screen.findByText((text) => text.includes(`${PROPORTION} is required`)),
).toBeInTheDocument();
expect(screen.getByRole('heading', { name: 'Foracort prescribing pattern' })).toBeInTheDocument(); expect(screen.getByRole('heading', { name: 'Foracort prescribing pattern' })).toBeInTheDocument();
expect(screen.queryByRole('heading', { name: 'Review' })).not.toBeInTheDocument(); expect(screen.queryByRole('heading', { name: 'Review' })).not.toBeInTheDocument();
}); });
@ -92,6 +94,29 @@ describe('survey steps', () => {
fireEvent.click(screen.getByRole('button', { name: 'Next' })); fireEvent.click(screen.getByRole('button', { name: 'Next' }));
expect(await screen.findByRole('heading', { name: 'Notes' })).toBeInTheDocument(); expect(await screen.findByRole('heading', { name: 'Notes' })).toBeInTheDocument();
expect(screen.queryByText(`${PROPORTION} must be at most 100`)).not.toBeInTheDocument(); expect(
screen.queryByText((text) => text.includes(`${PROPORTION} must be at most 100`)),
).not.toBeInTheDocument();
});
it('saves progress from the Save button and shows a toast', async () => {
const fetchMock = vi.fn(async (path, options = {}) => {
if (String(path).endsWith('/progress') && options.method === 'PUT') {
return { ok: true, json: async () => survey };
}
return { ok: true, json: async () => survey };
});
vi.stubGlobal('fetch', fetchMock);
renderSurvey();
await screen.findByLabelText(PROPORTION);
fireEvent.click(screen.getByRole('button', { name: 'Save' }));
expect(await screen.findByText('Progress saved')).toBeInTheDocument();
expect(fetchMock).toHaveBeenCalledWith(
'/api/survey/token/progress',
expect.objectContaining({ method: 'PUT' }),
);
}); });
}); });

View File

@ -49,6 +49,43 @@ function surveyStatusLabel(surveyStep, survey) {
return 'Not started'; return 'Not started';
} }
/**
* @param {string} label
* @returns {'completed' | 'in-process' | 'not-started' | 'rejected'}
*/
function stepStatusTone(label) {
const normalized = label.toLowerCase();
if (normalized === 'completed' || normalized === 'approved') {
return 'completed';
}
if (normalized === 'rejected') {
return 'rejected';
}
if (
normalized === 'not started' ||
normalized === 'not-started' ||
normalized === ''
) {
return 'not-started';
}
return 'in-process';
}
/**
* @param {{ label: string }} props
*/
function StepStatusBadge({ label }) {
return (
<span className={`user-step-status user-step-status-${stepStatusTone(label)}`}>
{label}
</span>
);
}
/** /**
* @param {{ status: string }} props * @param {{ status: string }} props
*/ */
@ -147,7 +184,9 @@ function SurveyDetails({ survey, surveyStep }) {
<dl className="user-detail-grid"> <dl className="user-detail-grid">
<div> <div>
<dt>Status</dt> <dt>Status</dt>
<dd>{status}</dd> <dd>
<StepStatusBadge label={status} />
</dd>
</div> </div>
{started ? ( {started ? (
<div> <div>
@ -240,15 +279,15 @@ export default function UserDashboardPage() {
<div className="user-step-list"> <div className="user-step-list">
<div className={`user-step ${me.steps.consent.done ? 'done' : 'pending'}`}> <div className={`user-step ${me.steps.consent.done ? 'done' : 'pending'}`}>
<strong>1. Consent (PIS / ICF)</strong> <strong>1. Consent (PIS / ICF)</strong>
<span>{me.steps.consent.done ? 'Completed' : 'Not started'}</span> <StepStatusBadge label={me.steps.consent.done ? 'Completed' : 'Not started'} />
</div> </div>
<div className={`user-step ${me.steps.agreement.done ? 'done' : 'pending'}`}> <div className={`user-step ${me.steps.agreement.done ? 'done' : 'pending'}`}>
<strong>2. Physician agreement</strong> <strong>2. Physician agreement</strong>
<span>{agreementLabel}</span> <StepStatusBadge label={agreementLabel} />
</div> </div>
<div className={`user-step ${me.steps.survey.done ? 'done' : 'pending'}`}> <div className={`user-step ${me.steps.survey.done ? 'done' : 'pending'}`}>
<strong>3. Study questionnaire</strong> <strong>3. Study questionnaire</strong>
<span>{surveyLabel}</span> <StepStatusBadge label={surveyLabel} />
</div> </div>
</div> </div>