fix: reset form

This commit is contained in:
hexastack 2025-02-04 16:21:31 +01:00
parent 8112b80f94
commit 9e029674e1

View File

@ -6,7 +6,6 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/ */
import AddIcon from "@mui/icons-material/Add"; import AddIcon from "@mui/icons-material/Add";
import { Button, Dialog, DialogActions, DialogContent } from "@mui/material"; import { Button, Dialog, DialogActions, DialogContent } from "@mui/material";
import { FC, useEffect } from "react"; import { FC, useEffect } from "react";
@ -53,30 +52,34 @@ export const ContentTypeDialog: FC<ContentTypeDialogProps> = ({
name: "fields", name: "fields",
control, control,
}); });
const CloseAndReset = () => {
closeDialog();
reset();
};
useEffect(() => {
if (open) reset();
}, [open, reset]);
useEffect(() => { useEffect(() => {
if (data) { if (data) {
reset({ reset({
name: data.name, name: data.name,
fields: data.fields,
}); });
} else { } else {
reset(); reset();
} }
}, [data, reset]); }, [data, reset]);
const { mutateAsync: createContentType } = useCreate( const { mutate: createContentType } = useCreate(EntityType.CONTENT_TYPE, {
EntityType.CONTENT_TYPE, onError: (error) => {
{ toast.error(error);
onError: (error) => {
toast.error(error);
},
onSuccess: () => {
closeDialog();
toast.success(t("message.success_save"));
},
}, },
); onSuccess: () => {
closeDialog();
toast.success(t("message.success_save"));
},
});
const { mutateAsync: updateContentType } = useUpdate( const { mutateAsync: updateContentType } = useUpdate(
EntityType.CONTENT_TYPE, EntityType.CONTENT_TYPE,
{ {
@ -98,9 +101,9 @@ export const ContentTypeDialog: FC<ContentTypeDialogProps> = ({
}; };
return ( return (
<Dialog open={open} fullWidth onClose={closeDialog}> <Dialog open={open} fullWidth onClose={CloseAndReset}>
<form onSubmit={handleSubmit(onSubmitForm)}> <form onSubmit={handleSubmit(onSubmitForm)}>
<DialogTitle onClose={closeDialog}> <DialogTitle onClose={CloseAndReset}>
{data ? t("title.edit_content_type") : t("title.new_content_type")} {data ? t("title.edit_content_type") : t("title.new_content_type")}
</DialogTitle> </DialogTitle>
<DialogContent> <DialogContent>