mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix(frontend): update categoryForm to use mutate
This commit is contained in:
parent
658bfbc924
commit
dfff8cab10
@ -13,10 +13,7 @@ import { Button, Grid } from "@mui/material";
|
|||||||
import { useTranslate } from "@/hooks/useTranslate";
|
import { useTranslate } from "@/hooks/useTranslate";
|
||||||
import { FormButtonsProps } from "@/types/common/dialogs.types";
|
import { FormButtonsProps } from "@/types/common/dialogs.types";
|
||||||
|
|
||||||
export const DialogFormButtons = <T,>({
|
export const DialogFormButtons = ({ onCancel, onSubmit }: FormButtonsProps) => {
|
||||||
onCancel,
|
|
||||||
onSubmit,
|
|
||||||
}: FormButtonsProps<T>) => {
|
|
||||||
const { t } = useTranslate();
|
const { t } = useTranslate();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -13,12 +13,12 @@ import { FormDialogProps } from "@/types/common/dialogs.types";
|
|||||||
|
|
||||||
import { DialogFormButtons } from "../buttons/FormButtons";
|
import { DialogFormButtons } from "../buttons/FormButtons";
|
||||||
|
|
||||||
export const FormDialog = <T,>({
|
export const FormDialog = ({
|
||||||
title,
|
title,
|
||||||
children,
|
children,
|
||||||
onSubmit,
|
onSubmit,
|
||||||
...rest
|
...rest
|
||||||
}: FormDialogProps<T>) => {
|
}: FormDialogProps) => {
|
||||||
const handleClose = () => rest.onClose?.({}, "backdropClick");
|
const handleClose = () => rest.onClose?.({}, "backdropClick");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -37,14 +37,8 @@ export const CategoryForm: FC<ComponentFormProps<ICategory>> = ({
|
|||||||
toast.success(t("message.success_save"));
|
toast.success(t("message.success_save"));
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const { mutateAsync: createCategory } = useCreate(
|
const { mutate: createCategory } = useCreate(EntityType.CATEGORY, options);
|
||||||
EntityType.CATEGORY,
|
const { mutate: updateCategory } = useUpdate(EntityType.CATEGORY, options);
|
||||||
options,
|
|
||||||
);
|
|
||||||
const { mutateAsync: updateCategory } = useUpdate(
|
|
||||||
EntityType.CATEGORY,
|
|
||||||
options,
|
|
||||||
);
|
|
||||||
const {
|
const {
|
||||||
reset,
|
reset,
|
||||||
register,
|
register,
|
||||||
@ -60,13 +54,13 @@ export const CategoryForm: FC<ComponentFormProps<ICategory>> = ({
|
|||||||
};
|
};
|
||||||
const onSubmitForm = async (params: ICategoryAttributes) => {
|
const onSubmitForm = async (params: ICategoryAttributes) => {
|
||||||
if (data) {
|
if (data) {
|
||||||
return await updateCategory({ id: data.id, params });
|
updateCategory({ id: data.id, params });
|
||||||
} else {
|
} else {
|
||||||
return await createCategory(params);
|
createCategory(params);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const submitAsync = async (e: BaseSyntheticEvent) => {
|
const submitAsync = async (e: BaseSyntheticEvent) => {
|
||||||
return await new Promise<ICategory>((resolve) => {
|
return await new Promise<void>((resolve) => {
|
||||||
handleSubmit((params) => {
|
handleSubmit((params) => {
|
||||||
resolve(onSubmitForm(params));
|
resolve(onSubmitForm(params));
|
||||||
})(e);
|
})(e);
|
||||||
|
|||||||
@ -137,10 +137,10 @@ export interface DialogProviderProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// form dialog
|
// form dialog
|
||||||
export interface FormDialogProps<T> extends MuiDialogProps {
|
export interface FormDialogProps extends MuiDialogProps {
|
||||||
title?: string;
|
title?: string;
|
||||||
children?: React.ReactNode;
|
children?: React.ReactNode;
|
||||||
onSubmit: (e: BaseSyntheticEvent) => Promise<T>;
|
onSubmit: (e: BaseSyntheticEvent) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
// form
|
// form
|
||||||
@ -148,13 +148,13 @@ export type ComponentFormProps<T> = {
|
|||||||
data: T | null;
|
data: T | null;
|
||||||
onError?: () => void;
|
onError?: () => void;
|
||||||
onSuccess?: () => void;
|
onSuccess?: () => void;
|
||||||
Wrapper?: React.FC<FormDialogProps<T>>;
|
Wrapper?: React.FC<FormDialogProps>;
|
||||||
WrapperProps?: Partial<FormDialogProps<T>>;
|
WrapperProps?: Partial<FormDialogProps>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface FormButtonsProps<T> {
|
export interface FormButtonsProps {
|
||||||
onCancel?: () => void;
|
onCancel?: () => void;
|
||||||
onSubmit: (e: BaseSyntheticEvent) => Promise<T>;
|
onSubmit: (e: BaseSyntheticEvent) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ComponentFormDialogProps<T> = DialogProps<T | null, boolean>;
|
export type ComponentFormDialogProps<T> = DialogProps<T | null, boolean>;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user