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) { export function useDictionaryForm(formState, getActiveFormStates) {
let hasErrors = $derived.by(() => { let hasErrors = $derived.by(() => {
const mainHasError = Object.values(formState.errors).some(v => v !== null); 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) Object.values(fs.errors).some(v => v !== null)
); );
return mainHasError || activeHasError; return mainHasError || activeHasError;

View File

@ -995,22 +995,39 @@ export function getTestFormActions(handlers) {
]; ];
} }
export function buildTestPayload({ mainForm, calForm, refForm, testType }) { // export function buildTestPayload({ mainForm, calForm, refForm, testType }) {
let payload = { // let payload = {
...mainForm // ...mainForm
}; // };
if (testType === 'CALC') { // if (testType === 'CALC') {
payload = { // payload.calculation = {
...payload, // ...calForm
...calForm // }
}; // } else if (testType === 'TEST' || testType === 'PARAM') {
} else if (testType === 'TEST' || 'PARAM') { // payload.reference = {
payload = { // ...refForm
...payload, // };
...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' 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(() => { 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 "TEST":
case "PARAM": case "PARAM":
return [refNumState, refTxtState, mapFormState]; return {
...refState,
map: mapFormState
};
case "CALC": case "CALC":
return [calFormState, refNumState, refTxtState, mapFormState]; return {
cal: calFormState,
...refState,
map: mapFormState
};
case "GROUP": case "GROUP":
return [];
case "TITLE": case "TITLE":
return [];
default: default:
return []; return {};
} }
}); });
@ -92,14 +124,12 @@
async function handleSave() { async function handleSave() {
const mainForm = masterDetail.formState.form; const mainForm = masterDetail.formState.form;
const calForm = calFormState.form; const testType = mainForm.TestType;
const refForm = refNumState.form;
const payload = buildTestPayload({ const payload = buildTestPayload({
mainForm, mainForm,
calForm, activeFormStates,
refForm, testType: testType
testType: mainForm.TestType
}); });
console.log(payload); console.log(payload);
// const result = await formState.save(masterDetail.mode); // const result = await formState.save(masterDetail.mode);
@ -401,6 +431,11 @@
// definition: formState.errors, // definition: formState.errors,
// active: activeFormStates.map(fs => fs.errors) // active: activeFormStates.map(fs => fs.errors)
// }); // });
// $inspect({
// definition: formState.errors,
// active: Object.values(activeFormStates).map(fs => fs.errors)
// });
</script> </script>
<FormPageContainer title="Create Test" {primaryAction} {secondaryActions} {actions}> <FormPageContainer title="Create Test" {primaryAction} {secondaryActions} {actions}>