From acf29ce20edc20bdccad27cb989dc2f23421714b Mon Sep 17 00:00:00 2001 From: rizky Date: Wed, 24 Jul 2024 01:01:07 -0700 Subject: [PATCH] fix form logic --- comps/form/Form.tsx | 1 + comps/form/gen/gen-form.ts | 6 ++++- comps/form/gen/set-value.ts | 2 +- comps/form/typings.ts | 1 + comps/form/utils/init.tsx | 44 ++++++++++++++++++++++++++----------- 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/comps/form/Form.tsx b/comps/form/Form.tsx index 0cdeff5..4d8c2d7 100755 --- a/comps/form/Form.tsx +++ b/comps/form/Form.tsx @@ -57,6 +57,7 @@ export const Form: FC = (props) => { field: null, }, has_fields_container: null as any, + is_newly_created: false }); const form_inner_ref = useRef(null); diff --git a/comps/form/gen/gen-form.ts b/comps/form/gen/gen-form.ts index efb09be..3599708 100755 --- a/comps/form/gen/gen-form.ts +++ b/comps/form/gen/gen-form.ts @@ -157,7 +157,11 @@ ${ ...record, }, }); - if (res) form.id = res.id; + + if (res) { + form.id = res.id; + fm.is_newly_created = true; + } } if (has_many.length) { diff --git a/comps/form/gen/set-value.ts b/comps/form/gen/set-value.ts index 6d758c9..628d9ad 100755 --- a/comps/form/gen/set-value.ts +++ b/comps/form/gen/set-value.ts @@ -30,7 +30,7 @@ export const set_value = ({ ${pk}: selected[0], }, }; - } else { + } else if (fm.data["${pk}"]) { fm.data[name] = { disconnect: true, }; diff --git a/comps/form/typings.ts b/comps/form/typings.ts index da05b64..34db193 100755 --- a/comps/form/typings.ts +++ b/comps/form/typings.ts @@ -155,6 +155,7 @@ export type FMInternal = { field: any; }; has_fields_container: boolean; + is_newly_created: boolean; }; export type FMLocal = FMInternal & { render: () => void }; diff --git a/comps/form/utils/init.tsx b/comps/form/utils/init.tsx index 5d00868..a7dcd46 100755 --- a/comps/form/utils/init.tsx +++ b/comps/form/utils/init.tsx @@ -83,6 +83,22 @@ export const formInit = (fm: FMLocal, props: FMProps) => { fm.internal.reload.done.map((e) => e()); setTimeout(() => { toast.dismiss(); + + if (fm.is_newly_created) { + fm.is_newly_created = false; + toast.success( +
+ + Saved +
, + { + className: css` + background: #e4ffed; + border: 2px solid green; + `, + } + ); + } }, 100); fm.status = "ready"; @@ -109,7 +125,7 @@ export const formInit = (fm: FMLocal, props: FMProps) => { if (typeof fm.props.on_submit === "function") { fm.status = "saving"; fm.render(); - + if (fm.props.sonar === "on" && !isEditor) { toast.loading( <> @@ -179,18 +195,20 @@ export const formInit = (fm: FMLocal, props: FMProps) => { } ); } else { - toast.success( -
- - Saved -
, - { - className: css` - background: #e4ffed; - border: 2px solid green; - `, - } - ); + if (!fm.is_newly_created) { + toast.success( +
+ + Saved +
, + { + className: css` + background: #e4ffed; + border: 2px solid green; + `, + } + ); + } } }, 100); }