submission fix for data/payload

This commit is contained in:
makearmy 2025-10-04 19:54:31 -04:00
parent 6e4d8c5582
commit 874b1d7d32

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 "";
@ -488,8 +488,8 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
handleSubmit,
control,
reset,
setValue,
getValues,
setValue, // added
getValues, // added
formState: { isSubmitting },
} = useForm<any>({
defaultValues: {
@ -504,7 +504,7 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
lens: "",
focus: "",
laser_soft: "",
repeat_all: "",
repeat_all: "", // on all targets
fill_settings: [],
line_settings: [],
raster_settings: [],
@ -541,7 +541,7 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
}
}, [isEdit, edit?.initialValues, reset]);
// After reset, force RHF values once (covers early case)
// After reset, force RHF values once
useEffect(() => {
if (!isEdit || !current) return;
@ -565,7 +565,7 @@ 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
// When options hydrate/change, re-apply the current ids so the select shows them
useEffect(() => {
if (!isEdit || !current) return;
const apply = (name: keyof typeof current) => {
@ -579,7 +579,6 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
apply("laser_soft");
apply("source");
apply("lens");
// depend on the actual option arrays so this runs after they load/filter
}, [
isEdit,
current,
@ -621,8 +620,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,
repeat_all: num(values.repeat_all),
laser_soft: values.laser_soft || null, // all targets
repeat_all: num(values.repeat_all), // all targets
fill_settings: (values.fill_settings || []).map((r: any) => ({
name: r.name || "",
power: num(r.power),
@ -684,7 +683,8 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
if (photoFile || screenFile) {
const form = new FormData();
form.set("payload", JSON.stringify(payload));
// Directus multipart expects JSON in "data"
form.set("data", JSON.stringify(payload));
if (photoFile) form.set("photo", photoFile, photoFile.name || "photo");
if (screenFile) form.set("screen", screenFile, screenFile.name || "screen");
res = await fetch("/api/submit/settings", { method: "POST", body: form, credentials: "include" });
@ -692,7 +692,8 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
res = await fetch("/api/submit/settings", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
// Directus JSON expects { data: { ... } }
body: JSON.stringify({ data: payload }),
credentials: "include",
});
}
@ -705,6 +706,7 @@ export default function SettingsSubmit(props: CreateProps | EditProps) {
// Success
if (!isEdit) {
// reset only on create
reset();
setPhotoFile(null);
setScreenFile(null);
@ -713,7 +715,9 @@ 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()}`);