diff --git a/components/forms/SettingsSubmit.tsx b/components/forms/SettingsSubmit.tsx index f110abae..0ff3f16d 100644 --- a/components/forms/SettingsSubmit.tsx +++ b/components/forms/SettingsSubmit.tsx @@ -55,7 +55,7 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV const [me, setMe] = useState(null); const [submitErr, setSubmitErr] = useState(null); - // Robust current-user fetch + // Robust current-user fetch (unchanged) useEffect(() => { let alive = true; (async () => { @@ -84,7 +84,7 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV return () => { alive = false; }; }, []); - // Options loaders (Directus reads) + // Options loaders (unchanged) function useOptions(path: string, includeId?: string | null) { const [opts, setOpts] = useState([]); useEffect(() => { @@ -164,7 +164,7 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV return { opts }; } - // Enumerations + // Enumerations (unchanged) const FILL_TYPES: Opt[] = [ { id: "uni", label: "UniDirectional" }, { id: "bi", label: "BiDirectional" }, @@ -175,13 +175,12 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV "threshold", "ordered", "atkinson", "dither", "stucki", "jarvis", "newsprint", "halftone", "sketch", "grayscale", ].map((x) => ({ id: x, label: x[0].toUpperCase() + x.slice(1) })); - // react-hook-form + // react-hook-form (unchanged) const { register, handleSubmit, control, reset, - // ↓↓↓ added setValue & getValues for rehydrate-fix setValue, getValues, formState: { isSubmitting }, @@ -198,7 +197,6 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV // Rig & Optics laser_soft: "", source: "", - // keep these blank so Select shows "—" lens_conf: "", lens_apt: "", lens_exp: "", @@ -212,12 +210,12 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV }, }); - // Repeaters + // Repeaters (unchanged) const fills = useFieldArray({ control, name: "fill_settings" }); const lines = useFieldArray({ control, name: "line_settings" }); const rasters = useFieldArray({ control, name: "raster_settings" }); - // Prefill (edit) + // Prefill (unchanged) useEffect(() => { if (!isEdit || !initialValues) return; reset({ @@ -245,7 +243,7 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV }); }, [isEdit, initialValues, reset]); - // Option lists (include current IDs to guarantee a visible option) + // Options (unchanged) const mats = useOptions("material", initialValues?.mat ?? null); const coats = useOptions("material_coating", initialValues?.mat_coat ?? null); const colors = useOptions("material_color", initialValues?.mat_color ?? null); @@ -257,49 +255,34 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV const apt = useOptions("laser_scan_lens_apt", initialValues?.lens_apt ?? null); const exp = useOptions("laser_scan_lens_exp", initialValues?.lens_exp ?? null); - // 🔧 Re-apply RHF values when options hydrate (fixes stubborn placeholder) + // Re-apply select values when options hydrate (unchanged fix) useEffect(() => { - if (!isEdit) return; // only matters in edit-mode - const optionsByName: Record = { - mat: mats.opts, - mat_coat: coats.opts, - mat_color: colors.opts, - mat_opacity: opacs.opts, - laser_soft: soft.opts, - source: srcs.opts, - lens_conf: conf.opts, - lens_apt: apt.opts, - lens_exp: exp.opts, - lens: lens.opts, - }; - const names = Object.keys(optionsByName) as Array; - const current = getValues(); - - names.forEach((name) => { - const cur = current?.[name as string]; - if (cur == null || cur === "") return; - const opts = optionsByName[name] || []; - // if the option exists now (or even if it didn't before), nudge RHF to re-sync - if (!opts.length || opts.some((o) => String(o.id) === String(cur))) { - setValue(name as any, cur, { shouldDirty: false, shouldValidate: false }); + const pairs: Array<[keyof any, string | null | undefined, Opt[]]> = [ + ["mat", initialValues?.mat, mats.opts], + ["mat_coat", initialValues?.mat_coat, coats.opts], + ["mat_color", initialValues?.mat_color, colors.opts], + ["mat_opacity", initialValues?.mat_opacity, opacs.opts], + ["laser_soft", initialValues?.laser_soft, soft.opts], + ["source", initialValues?.source, srcs.opts], + ["lens_conf", initialValues?.lens_conf, conf.opts], + ["lens_apt", initialValues?.lens_apt, apt.opts], + ["lens_exp", initialValues?.lens_exp, exp.opts], + ["lens", initialValues?.lens, lens.opts], + ]; + pairs.forEach(([name, id, opts]) => { + if (!id || !opts.length) return; + const current = getValues(String(name)); + if (!current) { + setValue(String(name), String(id), { shouldDirty: false, shouldValidate: false }); } }); - // eslint-disable-next-line react-hooks/exhaustive-deps }, [ - isEdit, - mats.opts.length, - coats.opts.length, - colors.opts.length, - opacs.opts.length, - soft.opts.length, - srcs.opts.length, - conf.opts.length, - apt.opts.length, - exp.opts.length, - lens.opts.length, + mats.opts, coats.opts, colors.opts, opacs.opts, soft.opts, + srcs.opts, conf.opts, apt.opts, exp.opts, lens.opts, + initialValues, getValues, setValue ]); - // Image files + // Files (unchanged) const [photoFile, setPhotoFile] = useState(null); const [screenFile, setScreenFile] = useState(null); const onPick = (setter: (f: File | null) => void) => (e: React.ChangeEvent) => setter(e.target.files?.[0] ?? null); @@ -326,11 +309,10 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV lens: values.lens || null, focus: values.focus === "" ? null : globalThis.Number(values.focus), repeat_all: values.repeat_all === "" ? null : globalThis.Number(values.repeat_all), - // Repeaters (raw pass-through; api will normalize nums/bools) + // Repeaters fill_settings: values.fill_settings || [], line_settings: values.line_settings || [], raster_settings: values.raster_settings || [], - // If editing with existing asset IDs, the API will accept them ...(initialValues?.photo ? { photo: initialValues.photo } : {}), ...(initialValues?.screen ? { screen: initialValues.screen } : {}), }; @@ -362,7 +344,17 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV return (
-

{isEdit ? "Edit CO₂ Galvo Setting" : "Submit CO₂ Galvo Setting"}

+ {/* Hide duplicate header in edit mode; keep visible in create mode */} + {!isEdit && ( +

+ Submit CO₂ Galvo Setting +

+ )} + {isEdit && ( +

+ Edit CO₂ Galvo Setting +

+ )} {meLabel ?

Submitting as {meLabel}

: null} {submitErr ?
{submitErr}
: null}
@@ -406,11 +398,11 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV