fix form logic

This commit is contained in:
rizky 2024-07-24 01:01:07 -07:00
parent 36ece9ec6d
commit acf29ce20e
5 changed files with 39 additions and 15 deletions

View File

@ -57,6 +57,7 @@ export const Form: FC<FMProps> = (props) => {
field: null, field: null,
}, },
has_fields_container: null as any, has_fields_container: null as any,
is_newly_created: false
}); });
const form_inner_ref = useRef<HTMLDivElement>(null); const form_inner_ref = useRef<HTMLDivElement>(null);

View File

@ -157,7 +157,11 @@ ${
...record, ...record,
}, },
}); });
if (res) form.id = res.id;
if (res) {
form.id = res.id;
fm.is_newly_created = true;
}
} }
if (has_many.length) { if (has_many.length) {

View File

@ -30,7 +30,7 @@ export const set_value = ({
${pk}: selected[0], ${pk}: selected[0],
}, },
}; };
} else { } else if (fm.data["${pk}"]) {
fm.data[name] = { fm.data[name] = {
disconnect: true, disconnect: true,
}; };

View File

@ -155,6 +155,7 @@ export type FMInternal = {
field: any; field: any;
}; };
has_fields_container: boolean; has_fields_container: boolean;
is_newly_created: boolean;
}; };
export type FMLocal = FMInternal & { render: () => void }; export type FMLocal = FMInternal & { render: () => void };

View File

@ -83,6 +83,22 @@ export const formInit = (fm: FMLocal, props: FMProps) => {
fm.internal.reload.done.map((e) => e()); fm.internal.reload.done.map((e) => e());
setTimeout(() => { setTimeout(() => {
toast.dismiss(); toast.dismiss();
if (fm.is_newly_created) {
fm.is_newly_created = false;
toast.success(
<div className="c-flex c-text-green-700 c-items-center">
<Check className="c-h-4 c-w-4 c-mr-1 " />
Saved
</div>,
{
className: css`
background: #e4ffed;
border: 2px solid green;
`,
}
);
}
}, 100); }, 100);
fm.status = "ready"; fm.status = "ready";
@ -109,7 +125,7 @@ export const formInit = (fm: FMLocal, props: FMProps) => {
if (typeof fm.props.on_submit === "function") { if (typeof fm.props.on_submit === "function") {
fm.status = "saving"; fm.status = "saving";
fm.render(); fm.render();
if (fm.props.sonar === "on" && !isEditor) { if (fm.props.sonar === "on" && !isEditor) {
toast.loading( toast.loading(
<> <>
@ -179,18 +195,20 @@ export const formInit = (fm: FMLocal, props: FMProps) => {
} }
); );
} else { } else {
toast.success( if (!fm.is_newly_created) {
<div className="c-flex c-text-green-700 c-items-center"> toast.success(
<Check className="c-h-4 c-w-4 c-mr-1 " /> <div className="c-flex c-text-green-700 c-items-center">
Saved <Check className="c-h-4 c-w-4 c-mr-1 " />
</div>, Saved
{ </div>,
className: css` {
background: #e4ffed; className: css`
border: 2px solid green; background: #e4ffed;
`, border: 2px solid green;
} `,
); }
);
}
} }
}, 100); }, 100);
} }