mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-12 06:06:10 +00:00
The review dialog silently dropped any selected reading that could not be written (no date, no unit, no value) and saved the rest. When nothing was left it showed "Select at least one reading to save", which described a different situation and said nothing about what was missing. A save is now all-or-nothing over the selected readings. When one is incomplete, nothing is written; the dialog states how many readings need attention, marks each missing field (aria-invalid, inline message, red border, required marker on the label) and moves focus to the first one. Marks appear after a save attempt and follow the edits. The "nothing selected" message is kept for that case only. Validation is a pure function next to the payload mapping, so the rule that blocks a save and the rule that builds the payload cannot drift apart. Adds EN and DE strings; the other locales are left to the maintainers. Refs #1055
105 lines
3.8 KiB
TypeScript
105 lines
3.8 KiB
TypeScript
/**
|
|
* Per-row validation for the Lab-OCR review screen.
|
|
*
|
|
* A confirmed row can only be written when it carries everything the commit
|
|
* payload needs. `validateReviewRow` reports WHICH fields are missing so the
|
|
* review screen can mark them, and `toCommitRow` is the same rule applied to
|
|
* build the payload: it returns null exactly when `validateReviewRow` reports
|
|
* at least one field.
|
|
*/
|
|
import type { OcrReviewRow } from "./ocr-review-types";
|
|
import type { OcrCommitRowInput } from "./use-ocr-extract";
|
|
|
|
export type OcrRowField = "analyte" | "value" | "valueText" | "unit" | "date";
|
|
|
|
/** The fields of one row that block saving; empty when the row is writable. */
|
|
export type OcrRowErrors = Partial<Record<OcrRowField, true>>;
|
|
|
|
/** A calendar day → an ISO instant at noon UTC, avoiding a TZ day-shift. */
|
|
function dayAtNoonUtc(day: string | null): Date | null {
|
|
if (!day) return null;
|
|
const at = new Date(`${day}T12:00:00.000Z`);
|
|
return Number.isNaN(at.getTime()) ? null : at;
|
|
}
|
|
|
|
function isQualitativeRow(row: OcrReviewRow): boolean {
|
|
return row.valueText !== null && row.valueText !== undefined;
|
|
}
|
|
|
|
export function validateReviewRow(row: OcrReviewRow): OcrRowErrors {
|
|
const errors: OcrRowErrors = {};
|
|
if (!row.analyte.trim()) errors.analyte = true;
|
|
if (!dayAtNoonUtc(row.takenAt)) errors.date = true;
|
|
|
|
if (isQualitativeRow(row)) {
|
|
if (!(row.valueText ?? "").trim()) errors.valueText = true;
|
|
} else {
|
|
if (row.value === null || !Number.isFinite(row.value)) errors.value = true;
|
|
if (!(row.unit ?? "").trim()) errors.unit = true;
|
|
}
|
|
return errors;
|
|
}
|
|
|
|
export function hasRowErrors(errors: OcrRowErrors): boolean {
|
|
return Object.keys(errors).length > 0;
|
|
}
|
|
|
|
/** Map a confirmed review row to the commit payload, or null when invalid. */
|
|
export function toCommitRow(row: OcrReviewRow): OcrCommitRowInput | null {
|
|
const analyte = row.analyte.trim();
|
|
const takenAt = dayAtNoonUtc(row.takenAt);
|
|
if (!analyte || !takenAt) return null;
|
|
|
|
if (isQualitativeRow(row)) {
|
|
const valueText = (row.valueText ?? "").trim();
|
|
if (!valueText) return null;
|
|
return { analyte, valueText, takenAt: takenAt.toISOString() };
|
|
}
|
|
|
|
if (row.value === null || !Number.isFinite(row.value)) return null;
|
|
const unit = (row.unit ?? "").trim();
|
|
if (!unit) return null;
|
|
return {
|
|
analyte,
|
|
value: row.value,
|
|
unit,
|
|
takenAt: takenAt.toISOString(),
|
|
...(row.referenceLow !== null ? { referenceLow: row.referenceLow } : {}),
|
|
...(row.referenceHigh !== null ? { referenceHigh: row.referenceHigh } : {}),
|
|
...(row.referenceText ? { referenceText: row.referenceText } : {}),
|
|
};
|
|
}
|
|
|
|
/** Blocking fields per CONFIRMED row, keyed by the row's key; writable rows are absent. */
|
|
export function collectRowErrors(
|
|
rows: readonly OcrReviewRow[],
|
|
): Map<string, OcrRowErrors> {
|
|
const byKey = new Map<string, OcrRowErrors>();
|
|
for (const row of rows) {
|
|
if (!row.confirmed) continue;
|
|
const errors = validateReviewRow(row);
|
|
if (hasRowErrors(errors)) byKey.set(row.key, errors);
|
|
}
|
|
return byKey;
|
|
}
|
|
|
|
/**
|
|
* What pressing Save does. A save is all-or-nothing over the selected rows:
|
|
* one unwritable row blocks the whole save instead of being dropped, because a
|
|
* reading that is silently left out is a reading the person believes was kept.
|
|
*/
|
|
export type OcrSavePlan =
|
|
| { kind: "nothing-selected" }
|
|
| { kind: "blocked" }
|
|
| { kind: "ready"; payload: OcrCommitRowInput[] };
|
|
|
|
export function planSave(rows: readonly OcrReviewRow[]): OcrSavePlan {
|
|
const confirmed = rows.filter((row) => row.confirmed);
|
|
if (confirmed.length === 0) return { kind: "nothing-selected" };
|
|
if (collectRowErrors(confirmed).size > 0) return { kind: "blocked" };
|
|
const payload = confirmed
|
|
.map(toCommitRow)
|
|
.filter((row): row is OcrCommitRowInput => row !== null);
|
|
return { kind: "ready", payload };
|
|
}
|