submission form cleanup

This commit is contained in:
makearmy 2025-10-06 21:18:15 -04:00
parent b779d2c83a
commit 190cb60ac0

View file

@ -216,7 +216,6 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV
const lines = useFieldArray({ control, name: "line_settings" });
const rasters = useFieldArray({ control, name: "raster_settings" });
// ⬇⬇⬇ Moved ABOVE the hydrate effect so they exist before being referenced
// Option lists (include current IDs to guarantee a visible option)
const mats = useOptions("material", initialValues?.mat ?? null);
const coats = useOptions("material_coating", initialValues?.mat_coat ?? null);
@ -228,7 +227,6 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV
const conf = useOptions("laser_scan_lens_config", initialValues?.lens_conf ?? null);
const apt = useOptions("laser_scan_lens_apt", initialValues?.lens_apt ?? null);
const exp = useOptions("laser_scan_lens_exp", initialValues?.lens_exp ?? null);
// ⬆⬆⬆
// Prefill (edit)
useEffect(() => {
@ -258,32 +256,29 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV
});
}, [isEdit, initialValues, reset]);
// Re-apply select values when options hydrate (fixes stubborn placeholder issue)
// Re-apply select values when options hydrate (forces DOM to adopt prefilled values)
useEffect(() => {
if (!isEdit || !initialValues) return;
const ensure = (name: string, currentId: string | null | undefined, opts: Opt[]) => {
if (!currentId) return;
const cur = getValues(name as any);
const curStr = cur == null ? "" : String(cur);
if (!curStr || !opts.some((o) => String(o.id) === curStr)) {
setValue(name as any, String(currentId), { shouldDirty: false, shouldValidate: false });
}
const hydrate = (name: string, currentId: string | null | undefined, opts: Opt[]) => {
if (!currentId || !opts.length) return;
// Always nudge value back to the current id after options load
setValue(name as any, String(currentId), { shouldDirty: false, shouldValidate: false });
};
ensure("mat", initialValues.mat, mats.opts);
ensure("mat_coat", initialValues.mat_coat, coats.opts);
ensure("mat_color", initialValues.mat_color, colors.opts);
ensure("mat_opacity", initialValues.mat_opacity, opacs.opts);
hydrate("mat", initialValues.mat, mats.opts);
hydrate("mat_coat", initialValues.mat_coat, coats.opts);
hydrate("mat_color", initialValues.mat_color, colors.opts);
hydrate("mat_opacity", initialValues.mat_opacity, opacs.opts);
ensure("laser_soft", initialValues.laser_soft, soft.opts);
ensure("source", initialValues.source, srcs.opts);
hydrate("laser_soft", initialValues.laser_soft, soft.opts);
hydrate("source", initialValues.source, srcs.opts);
ensure("lens_conf", initialValues.lens_conf, conf.opts);
ensure("lens_apt", initialValues.lens_apt, apt.opts);
ensure("lens_exp", initialValues.lens_exp, exp.opts);
hydrate("lens_conf", initialValues.lens_conf, conf.opts);
hydrate("lens_apt", initialValues.lens_apt, apt.opts);
hydrate("lens_exp", initialValues.lens_exp, exp.opts);
ensure("lens", initialValues.lens, lens.opts);
hydrate("lens", initialValues.lens, lens.opts);
}, [
isEdit,
initialValues,
@ -291,7 +286,7 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV
soft.opts, srcs.opts,
conf.opts, apt.opts, exp.opts,
lens.opts,
getValues, setValue,
setValue,
]);
// Image files
@ -453,9 +448,7 @@ export default function SettingsSubmit({ mode = "create", submissionId, initialV
<Number label="Angle (°)" name={`fill_settings.${i}.angle`} register={register} step="1" />
<Check label="Auto Rotate" name={`fill_settings.${i}.auto`} register={register} />
{autoRotate && (
<div className="md:col-span-2">
<Number label="Auto Rotate Increment (°)" name={`fill_settings.${i}.increment`} register={register} step="0.001" />
</div>
)}
<Check label="Crosshatch" name={`fill_settings.${i}.cross`} register={register} />
<Check label="Flood Fill" name={`fill_settings.${i}.flood`} register={register} />
@ -593,17 +586,22 @@ function Check({ label, name, register }: any) {
}
function Repeater({ title, fields, onAdd, onRemove, render }: any) {
return (
<fieldset className="border rounded p-3 space-y-2">
<fieldset className="border rounded p-3 space-y-3">
<div className="flex items-center justify-between">
<legend className="font-semibold">{title}</legend>
<button type="button" className="px-2 py-1 border rounded" onClick={onAdd}>+ Add</button>
</div>
<div className="space-y-3">
{fields.map((_: any, i: number) => (
<div key={i} className="space-y-2">
<div key={i} className="rounded-lg border bg-muted/20 p-3">
{render(i)}
<div className="pt-2">
<button type="button" className="px-2 py-1 border rounded" onClick={() => onRemove(i)}>Remove</button>
</div>
</div>
))}
</div>
</fieldset>
);
}