attempt 58 at getting edit form fields to prefill with existing data because i don't know what else to write anymore

This commit is contained in:
makearmy 2025-10-04 18:09:38 -04:00
parent 1f93dd1595
commit 6e4d8c5582

View file

@ -55,7 +55,7 @@ function shortId(s?: string) {
}
// ─────────────────────────────────────────────────────────────
/** Normalizers for edit-mode prefill (IDs + enums) */
// Normalizers for edit-mode prefill (IDs + enums)
// ─────────────────────────────────────────────────────────────
function idToString(v: any): string {
if (v == null || v === "") return "";
@ -473,10 +473,8 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
const colors = useOptions("material_color", current?.mat_color || undefined);
const opacs = useOptions("material_opacity", current?.mat_opacity || undefined);
const soft = useOptions("laser_software", current?.laser_soft || undefined); // required for ALL targets
// these two need ?target=
const srcs = useOptions(`laser_source?target=${typeForOptions}`, current?.source || undefined, {
disableNmFilter: isEdit, // show the exact current source even if nm is out-of-range
disableNmFilter: isEdit,
});
const lens = useOptions(`lens?target=${typeForOptions}`, current?.lens || undefined);
@ -490,8 +488,8 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
handleSubmit,
control,
reset,
setValue, // ← added
getValues, // ← added
setValue,
getValues,
formState: { isSubmitting },
} = useForm<any>({
defaultValues: {
@ -506,7 +504,7 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
lens: "",
focus: "",
laser_soft: "",
repeat_all: "", // on all targets
repeat_all: "",
fill_settings: [],
line_settings: [],
raster_settings: [],
@ -543,7 +541,7 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
}
}, [isEdit, edit?.initialValues, reset]);
// After reset, force RHF values for selects so the browser matches options when they hydrate
// After reset, force RHF values once (covers early case)
useEffect(() => {
if (!isEdit || !current) return;
@ -567,6 +565,34 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
});
}, [isEdit, current, getValues, setValue]);
// NEW: whenever options hydrate/change, re-apply the current ids so the select shows them
useEffect(() => {
if (!isEdit || !current) return;
const apply = (name: keyof typeof current) => {
const cur = (current as any)[name];
if (cur) setValue(name as any, cur, { shouldDirty: false, shouldValidate: false });
};
apply("mat");
apply("mat_coat");
apply("mat_color");
apply("mat_opacity");
apply("laser_soft");
apply("source");
apply("lens");
// depend on the actual option arrays so this runs after they load/filter
}, [
isEdit,
current,
setValue,
mats.opts,
coats.opts,
colors.opts,
opacs.opts,
soft.opts,
srcs.opts,
lens.opts,
]);
function num(v: any) {
return v === "" || v == null ? null : Number(v);
}
@ -595,8 +621,8 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
source: values.source || null,
lens: values.lens || null,
focus: num(values.focus),
laser_soft: values.laser_soft || null, // all targets
repeat_all: num(values.repeat_all), // all targets
laser_soft: values.laser_soft || null,
repeat_all: num(values.repeat_all),
fill_settings: (values.fill_settings || []).map((r: any) => ({
name: r.name || "",
power: num(r.power),
@ -679,7 +705,6 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
// Success
if (!isEdit) {
// reset only on create
reset();
setPhotoFile(null);
setScreenFile(null);
@ -688,9 +713,7 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
}
const id = (data as any)?.id ? String((data as any).id) : String(edit?.submissionId ?? "");
// back to success (create) or view (edit)
if (isEdit) {
// remove ?edit=1
const q = new URLSearchParams(sp.toString());
q.delete("edit");
router.replace(`/portal/laser-settings?${q.toString()}`);