Fixed Form Vlidation
This commit is contained in:
@@ -10,7 +10,7 @@ import { EmptyState, ErrorState, Skeleton } from "../../components/ui/States";
|
||||
import { useToast } from "../../components/ui/Toast";
|
||||
import { LIVE, useAsync } from "../../hooks/useAsync";
|
||||
import { api } from "../../api";
|
||||
import { errorMessage, formatDate, humanize } from "../../lib/format";
|
||||
import { columnLabel, errorMessage, formatDate } from "../../lib/format";
|
||||
import { Link } from "../../lib/router";
|
||||
import { useUser } from "../../lib/session";
|
||||
import type { CsvRow, CsvTable, FormApproval } from "../../types";
|
||||
@@ -22,8 +22,10 @@ const SERIAL = new Set(["serial_no", "doctor_sr_no", "chemist_sr_no"]);
|
||||
const DATES = new Set(["report_date", "date_of_visit"]);
|
||||
const MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
|
||||
const sameMonth = (a: unknown, b: string) => String(a ?? "").trim().toLowerCase() === b.trim().toLowerCase();
|
||||
/** The number shown for a row: its place in that MR's own form. */
|
||||
const numberOf = (r: CsvRow) => String(r.serial_no ?? r.source_row_number);
|
||||
/** Tables whose stored Sl. No. restarts at 1 for each month (see MONTH_NUMBERED_TABLES in
|
||||
* app/csv_data/router.py) — so it only means something within one chosen month. Viewed across
|
||||
* "All months" it would repeat 1, 1, 1…, so that view counts fresh over what's actually shown. */
|
||||
const MONTH_NUMBERED_TABLES = new Set(["sales_projection"]);
|
||||
|
||||
export function FormsPage({ table: routeTable }: { table?: string }) {
|
||||
const user = useUser();
|
||||
@@ -61,6 +63,8 @@ export function FormsPage({ table: routeTable }: { table?: string }) {
|
||||
const [deleting, setDeleting] = useState<CsvRow | null>(null);
|
||||
|
||||
const canEdit = user.role === "MR" || user.role === "ADMIN";
|
||||
// Only an admin may delete a row — an MR can edit their own forms but not remove rows from them.
|
||||
const canDelete = user.role === "ADMIN";
|
||||
// An admin adds rows on behalf of one chosen MR.
|
||||
const canAdd = canEdit && (!needsApproval || user.role === "MR" || !!viewOwner);
|
||||
const subjectId = user.role === "MR" ? user.id : viewOwner?.id;
|
||||
@@ -72,9 +76,11 @@ export function FormsPage({ table: routeTable }: { table?: string }) {
|
||||
const canApprove = needsApproval && user.role === "AM" && status === "pending" && !!viewOwner;
|
||||
const showStatus = needsApproval && (status === "pending" || status === "approved");
|
||||
|
||||
// Forms with a month can be downloaded one month at a time.
|
||||
// Forms with a month can be downloaded one month at a time. Every month is always offered —
|
||||
// not just ones with rows already in them — so choosing next month before anyone has filled
|
||||
// it in is a normal, working choice, not something hidden until data happens to exist.
|
||||
const hasMonth = !!table?.columns.includes("month");
|
||||
const months = useMemo(() => {
|
||||
const monthsWithRows = useMemo(() => {
|
||||
if (!hasMonth) return [] as string[];
|
||||
const seen = new Map<string, string>();
|
||||
for (const r of rows.data ?? []) { const m = String(r.month ?? "").trim(); if (m && !seen.has(m.toLowerCase())) seen.set(m.toLowerCase(), m); }
|
||||
@@ -83,10 +89,18 @@ export function FormsPage({ table: routeTable }: { table?: string }) {
|
||||
}, [hasMonth, rows.data]);
|
||||
const thisMonth = MONTHS[new Date().getMonth()]!;
|
||||
const chosenMonth = table ? monthChoice[table.name] : undefined;
|
||||
// Starts on the current month when the form has rows for it, otherwise on everything.
|
||||
const month = chosenMonth !== undefined ? (chosenMonth === "" || months.some((m) => sameMonth(m, chosenMonth)) ? chosenMonth : "") : months.find((m) => sameMonth(m, thisMonth)) ?? "";
|
||||
// Starts on the current month when the form already has rows for it, otherwise on everything.
|
||||
// Any of the twelve real months is a valid choice, whether or not it has rows yet.
|
||||
const month = chosenMonth !== undefined ? (chosenMonth === "" || MONTHS.some((m) => sameMonth(m, chosenMonth)) ? chosenMonth : "") : monthsWithRows.find((m) => sameMonth(m, thisMonth)) ?? "";
|
||||
const shownRows = month ? (rows.data ?? []).filter((r) => sameMonth(r.month, month)) : rows.data ?? [];
|
||||
const showToolbar = hasMonth ? months.length > 0 : isManager && needsApproval;
|
||||
const showToolbar = hasMonth || (isManager && needsApproval);
|
||||
|
||||
// Across "All months" a month-numbered table's own Sl. No. would repeat per month, so number
|
||||
// by position in the list actually on screen instead; a single chosen month already has its
|
||||
// own correct running numbers from the server, so those are kept as stored.
|
||||
const continuousNumbering = !!table && MONTH_NUMBERED_TABLES.has(table.name) && !month;
|
||||
const positionOf = useMemo(() => new Map(shownRows.map((r, i) => [r.id, i + 1] as const)), [shownRows]);
|
||||
const numberOf = (r: CsvRow) => String(continuousNumbering ? positionOf.get(r.id) ?? "" : r.serial_no ?? r.source_row_number);
|
||||
|
||||
/** Send (MR) or approve (AM). The screen changes at once; the server's answer confirms it, or undoes it. */
|
||||
async function act(kind: "request" | "approve") {
|
||||
@@ -134,7 +148,7 @@ export function FormsPage({ table: routeTable }: { table?: string }) {
|
||||
.filter((c) => !SERIAL.has(c) && !(user.role !== "ADMIN" && IDENTITY.has(c)))
|
||||
.map<Column<CsvRow>>((c) => ({
|
||||
key: c,
|
||||
header: humanize(c),
|
||||
header: columnLabel(c),
|
||||
nowrap: true,
|
||||
render: (r) => {
|
||||
const value = String(r[c] ?? "");
|
||||
@@ -147,7 +161,7 @@ export function FormsPage({ table: routeTable }: { table?: string }) {
|
||||
<span className="row-actions">
|
||||
{canEdit && <Button size="sm" variant="ghost" icon="edit" onClick={() => setEditing({ row: r })}>Edit</Button>}
|
||||
<Button size="sm" variant="ghost" icon="download" onClick={() => void run(api.downloadCsvPdf(table.name, { rowId: r.id }))}>PDF</Button>
|
||||
{canEdit && <Button size="sm" variant="ghost" icon="trash" onClick={() => setDeleting(r)} aria-label="Delete row" />}
|
||||
{canDelete && <Button size="sm" variant="ghost" icon="trash" onClick={() => setDeleting(r)} aria-label="Delete row" />}
|
||||
</span>
|
||||
),
|
||||
},
|
||||
@@ -178,7 +192,7 @@ export function FormsPage({ table: routeTable }: { table?: string }) {
|
||||
<>
|
||||
{canSend && <Button icon="send" loading={acting} onClick={() => void act("request")}>Send for approval</Button>}
|
||||
{canApprove && <Button variant="primary" icon="check" loading={acting} onClick={() => void act("approve")}>Approve as AM</Button>}
|
||||
<Button icon="download" disabled={!hasRows} onClick={() => void run(api.downloadCsvPdf(table.name, { ownerId: viewOwner?.id, month: month || undefined }))}>{month ? `Download ${month} PDF` : "Download PDF"}</Button>
|
||||
<Button icon="download" disabled={shownRows.length === 0} onClick={() => void run(api.downloadCsvPdf(table.name, { ownerId: viewOwner?.id, month: month || undefined }))}>{month ? `Download ${month} PDF` : "Download PDF"}</Button>
|
||||
{canAdd && <Button variant="primary" icon="plus" onClick={() => setEditing({ row: null })}>New row</Button>}
|
||||
</>
|
||||
}
|
||||
@@ -194,12 +208,12 @@ export function FormsPage({ table: routeTable }: { table?: string }) {
|
||||
</Select>
|
||||
</label>
|
||||
)}
|
||||
{hasMonth && months.length > 0 && (
|
||||
{hasMonth && (
|
||||
<label className="filter">
|
||||
<span>Month</span>
|
||||
<Select value={month} onChange={(e) => setMonthChoice((m) => ({ ...m, [table.name]: e.target.value }))} aria-label="Month">
|
||||
<option value="">All months</option>
|
||||
{months.map((m) => <option key={m} value={m}>{m}</option>)}
|
||||
{MONTHS.map((m) => <option key={m} value={m}>{m}{monthsWithRows.some((x) => sameMonth(x, m)) ? "" : " (no rows yet)"}</option>)}
|
||||
</Select>
|
||||
</label>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user