Fixed Form Vlidation
This commit is contained in:
@@ -4,7 +4,8 @@ import { Button } from "../../components/ui/Button";
|
||||
import { Field, Input, Select } from "../../components/ui/Field";
|
||||
import { Modal } from "../../components/ui/Modal";
|
||||
import { api } from "../../api";
|
||||
import { errorMessage, humanize } from "../../lib/format";
|
||||
import { useAsync } from "../../hooks/useAsync";
|
||||
import { columnLabel, errorMessage } from "../../lib/format";
|
||||
import { isoDate, maxLength, numeric, required, useValidation, type Rule } from "../../lib/validation";
|
||||
import type { CsvRow, CsvTable, FormOwner, Profile, User } from "../../types";
|
||||
|
||||
@@ -40,10 +41,18 @@ export function RowEditor({ open, table, row, rows, user, profile, owner, onClos
|
||||
() => (table.name === "sales_projection" ? rows.map((r) => ({ name: String(r.product_name || ""), pts: String(r.pts || "") })).filter((p) => p.name) : []),
|
||||
[table.name, rows],
|
||||
);
|
||||
// "Core Dr. Call & Conversion" tracks core doctors only — the field is only ever one of
|
||||
// the doctors assigned to whoever the row is for, never free text.
|
||||
const needsDoctors = table.name === "core_dr_call_conversion";
|
||||
const doctors = useAsync(() => (needsDoctors ? api.doctors() : Promise.resolve([])), [needsDoctors]);
|
||||
const coreDoctorNames = useMemo(
|
||||
() => (doctors.data ?? []).filter((d) => d.is_core_doctor).map((d) => d.name),
|
||||
[doctors.data],
|
||||
);
|
||||
|
||||
const [draft, setDraft] = useState<Record<string, string>>({});
|
||||
const rulesFor = (c: string): Rule[] => {
|
||||
const label = humanize(c);
|
||||
const label = columnLabel(c);
|
||||
if (table.name === "sales_projection" && c === "product_name") return [required("Product name"), maxLength(200, label)];
|
||||
if (DATES.has(c)) return [isoDate(label)];
|
||||
if (NUMERIC.has(c)) return [maxLength(30, label), numeric(label)];
|
||||
@@ -108,6 +117,16 @@ export function RowEditor({ open, table, row, rows, user, profile, owner, onClos
|
||||
</Select>
|
||||
);
|
||||
}
|
||||
if (needsDoctors && column === "doctor_name") {
|
||||
return (
|
||||
<Select value={value} onChange={(e) => set(column, e.target.value)} onBlur={blur(column)} disabled={doctors.loading}>
|
||||
<option value="">{doctors.loading ? "Loading doctors…" : "Choose doctor"}</option>
|
||||
{/* A row imported before this doctor's assignment changed keeps showing its own value. */}
|
||||
{value && !coreDoctorNames.includes(value) && <option value={value}>{value}</option>}
|
||||
{coreDoctorNames.map((n) => <option key={n} value={n}>{n}</option>)}
|
||||
</Select>
|
||||
);
|
||||
}
|
||||
return <Input type={DATES.has(column) ? "date" : "text"} inputMode={NUMERIC.has(column) ? "decimal" : undefined} value={value} readOnly={generated(column)} onChange={(e) => set(column, e.target.value)} onBlur={blur(column)} />;
|
||||
};
|
||||
|
||||
@@ -123,7 +142,18 @@ export function RowEditor({ open, table, row, rows, user, profile, owner, onClos
|
||||
<form id="row-editor-form" className="form-grid form-single" onSubmit={submit} noValidate>
|
||||
<Alert>{error}</Alert>
|
||||
{columns.map((c) => (
|
||||
<Field key={c} label={humanize(c)} error={v.errors[c]} required={table.name === "sales_projection" && c === "product_name"} hint={generated(c) ? "Filled in automatically" : NUMERIC.has(c) ? "Numbers only" : undefined}>{control(c)}</Field>
|
||||
<Field
|
||||
key={c}
|
||||
label={columnLabel(c)}
|
||||
error={v.errors[c]}
|
||||
required={table.name === "sales_projection" && c === "product_name"}
|
||||
hint={
|
||||
generated(c) ? "Filled in automatically"
|
||||
: needsDoctors && c === "doctor_name" && !doctors.loading && coreDoctorNames.length === 0 ? "No core doctors are assigned yet — ask your administrator to assign some."
|
||||
: NUMERIC.has(c) ? "Numbers only"
|
||||
: undefined
|
||||
}
|
||||
>{control(c)}</Field>
|
||||
))}
|
||||
</form>
|
||||
</Modal>
|
||||
|
||||
Reference in New Issue
Block a user