add feature dynamic payload builder

This commit is contained in:
faiztyanirh 2026-03-06 11:06:35 +07:00
parent 0d49cc57eb
commit 104b6eff88
3 changed files with 93 additions and 27 deletions

View File

@ -11,10 +11,24 @@
// }
// }
// export function useDictionaryForm(formState, getActiveFormStates) {
// let hasErrors = $derived.by(() => {
// const mainHasError = Object.values(formState.errors).some(v => v !== null);
// const activeHasError = getActiveFormStates().some(fs =>
// Object.values(fs.errors).some(v => v !== null)
// );
// return mainHasError || activeHasError;
// });
// return {
// get hasErrors() { return hasErrors },
// }
// }
export function useDictionaryForm(formState, getActiveFormStates) {
let hasErrors = $derived.by(() => {
const mainHasError = Object.values(formState.errors).some(v => v !== null);
const activeHasError = getActiveFormStates().some(fs =>
const activeHasError = Object.values(getActiveFormStates()).some(fs =>
Object.values(fs.errors).some(v => v !== null)
);
return mainHasError || activeHasError;

View File

@ -995,22 +995,39 @@ export function getTestFormActions(handlers) {
];
}
export function buildTestPayload({ mainForm, calForm, refForm, testType }) {
let payload = {
...mainForm
};
// export function buildTestPayload({ mainForm, calForm, refForm, testType }) {
// let payload = {
// ...mainForm
// };
if (testType === 'CALC') {
payload = {
...payload,
...calForm
};
} else if (testType === 'TEST' || 'PARAM') {
payload = {
...payload,
...refForm
};
// if (testType === 'CALC') {
// payload.calculation = {
// ...calForm
// }
// } else if (testType === 'TEST' || testType === 'PARAM') {
// payload.reference = {
// ...refForm
// };
// }
// return cleanEmptyStrings(payload);
// }
export function buildTestPayload({ mainForm, activeFormStates, testType }) {
let payload = {
...mainForm
};
for (const key in activeFormStates) {
const state = activeFormStates[key];
if (state?.form) {
payload[key] = {
...state.form
};
}
}
return cleanEmptyStrings(payload);
return cleanEmptyStrings(payload);
}

View File

@ -55,19 +55,51 @@
modeOpt: 'cascade'
})
// const activeFormStates = $derived.by(() => {
// switch (formState.form.TestType) {
// case "TEST":
// case "PARAM":
// return [refNumState, refTxtState, mapFormState];
// case "CALC":
// return [calFormState, refNumState, refTxtState, mapFormState];
// case "GROUP":
// return [];
// case "TITLE":
// return [];
// default:
// return [];
// }
// });
const activeFormStates = $derived.by(() => {
switch (formState.form.TestType) {
const testType = formState.form.TestType ?? "";
const refType = formState.form.RefType ?? "";
let refState = {};
if (refType === "RANGE" || refType === "THOLD") {
refState = { refNum: refNumState };
} else if (refType === "TEXT" || refType === "VSET") {
refState = { refTxt: refTxtState };
}
switch (testType) {
case "TEST":
case "PARAM":
return [refNumState, refTxtState, mapFormState];
return {
...refState,
map: mapFormState
};
case "CALC":
return [calFormState, refNumState, refTxtState, mapFormState];
return {
cal: calFormState,
...refState,
map: mapFormState
};
case "GROUP":
return [];
case "TITLE":
return [];
default:
return [];
return {};
}
});
@ -92,14 +124,12 @@
async function handleSave() {
const mainForm = masterDetail.formState.form;
const calForm = calFormState.form;
const refForm = refNumState.form;
const testType = mainForm.TestType;
const payload = buildTestPayload({
mainForm,
calForm,
refForm,
testType: mainForm.TestType
activeFormStates,
testType: testType
});
console.log(payload);
// const result = await formState.save(masterDetail.mode);
@ -401,6 +431,11 @@
// definition: formState.errors,
// active: activeFormStates.map(fs => fs.errors)
// });
// $inspect({
// definition: formState.errors,
// active: Object.values(activeFormStates).map(fs => fs.errors)
// });
</script>
<FormPageContainer title="Create Test" {primaryAction} {secondaryActions} {actions}>