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:
parent
1f93dd1595
commit
6e4d8c5582
1 changed files with 36 additions and 13 deletions
|
|
@ -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 {
|
function idToString(v: any): string {
|
||||||
if (v == null || v === "") return "";
|
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 colors = useOptions("material_color", current?.mat_color || undefined);
|
||||||
const opacs = useOptions("material_opacity", current?.mat_opacity || undefined);
|
const opacs = useOptions("material_opacity", current?.mat_opacity || undefined);
|
||||||
const soft = useOptions("laser_software", current?.laser_soft || undefined); // required for ALL targets
|
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, {
|
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);
|
const lens = useOptions(`lens?target=${typeForOptions}`, current?.lens || undefined);
|
||||||
|
|
||||||
|
|
@ -490,8 +488,8 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
control,
|
control,
|
||||||
reset,
|
reset,
|
||||||
setValue, // ← added
|
setValue,
|
||||||
getValues, // ← added
|
getValues,
|
||||||
formState: { isSubmitting },
|
formState: { isSubmitting },
|
||||||
} = useForm<any>({
|
} = useForm<any>({
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
|
|
@ -506,7 +504,7 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
|
||||||
lens: "",
|
lens: "",
|
||||||
focus: "",
|
focus: "",
|
||||||
laser_soft: "",
|
laser_soft: "",
|
||||||
repeat_all: "", // on all targets
|
repeat_all: "",
|
||||||
fill_settings: [],
|
fill_settings: [],
|
||||||
line_settings: [],
|
line_settings: [],
|
||||||
raster_settings: [],
|
raster_settings: [],
|
||||||
|
|
@ -543,7 +541,7 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
|
||||||
}
|
}
|
||||||
}, [isEdit, edit?.initialValues, reset]);
|
}, [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(() => {
|
useEffect(() => {
|
||||||
if (!isEdit || !current) return;
|
if (!isEdit || !current) return;
|
||||||
|
|
||||||
|
|
@ -567,6 +565,34 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
|
||||||
});
|
});
|
||||||
}, [isEdit, current, getValues, setValue]);
|
}, [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) {
|
function num(v: any) {
|
||||||
return v === "" || v == null ? null : Number(v);
|
return v === "" || v == null ? null : Number(v);
|
||||||
}
|
}
|
||||||
|
|
@ -595,8 +621,8 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
|
||||||
source: values.source || null,
|
source: values.source || null,
|
||||||
lens: values.lens || null,
|
lens: values.lens || null,
|
||||||
focus: num(values.focus),
|
focus: num(values.focus),
|
||||||
laser_soft: values.laser_soft || null, // all targets
|
laser_soft: values.laser_soft || null,
|
||||||
repeat_all: num(values.repeat_all), // all targets
|
repeat_all: num(values.repeat_all),
|
||||||
fill_settings: (values.fill_settings || []).map((r: any) => ({
|
fill_settings: (values.fill_settings || []).map((r: any) => ({
|
||||||
name: r.name || "",
|
name: r.name || "",
|
||||||
power: num(r.power),
|
power: num(r.power),
|
||||||
|
|
@ -679,7 +705,6 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
|
||||||
|
|
||||||
// Success
|
// Success
|
||||||
if (!isEdit) {
|
if (!isEdit) {
|
||||||
// reset only on create
|
|
||||||
reset();
|
reset();
|
||||||
setPhotoFile(null);
|
setPhotoFile(null);
|
||||||
setScreenFile(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 ?? "");
|
const id = (data as any)?.id ? String((data as any).id) : String(edit?.submissionId ?? "");
|
||||||
// back to success (create) or view (edit)
|
|
||||||
if (isEdit) {
|
if (isEdit) {
|
||||||
// remove ?edit=1
|
|
||||||
const q = new URLSearchParams(sp.toString());
|
const q = new URLSearchParams(sp.toString());
|
||||||
q.delete("edit");
|
q.delete("edit");
|
||||||
router.replace(`/portal/laser-settings?${q.toString()}`);
|
router.replace(`/portal/laser-settings?${q.toString()}`);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue