From cd31b873fde568bf97c523d32f29af6fe2702f9b Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Thu, 6 Feb 2025 17:06:10 +0100 Subject: [PATCH] fix(frontend): refactor NLP value logic to use synchronous methods --- .../components/nlp/components/NlpValue.tsx | 25 +++++++++---------- .../nlp/components/NlpValueForm.tsx | 23 ++++++++++------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/frontend/src/components/nlp/components/NlpValue.tsx b/frontend/src/components/nlp/components/NlpValue.tsx index ccdcfcf4..57dd0462 100644 --- a/frontend/src/components/nlp/components/NlpValue.tsx +++ b/frontend/src/components/nlp/components/NlpValue.tsx @@ -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). */ - import { faGraduationCap } from "@fortawesome/free-solid-svg-icons"; import AddIcon from "@mui/icons-material/Add"; import ArrowBackIcon from "@mui/icons-material/ArrowBack"; @@ -64,22 +63,23 @@ export const NlpValues = ({ entityId }: { entityId: string }) => { params: searchPayload, }, ); - const options = { + const { mutate: deleteNlpValue } = useDelete(EntityType.NLP_VALUE, { + onError: (error: Error) => { + toast.error(error.message || t("message.internal_server_error")); + }, + onSuccess() { + refetchEntity(); + toast.success(t("message.item_delete_success")); + }, + }); + const { mutate: deleteNlpValues } = useDeleteMany(EntityType.NLP_VALUE, { onError: (error: Error) => { toast.error(error.message || t("message.internal_server_error")); }, onSuccess() { toast.success(t("message.item_delete_success")); }, - }; - const { mutateAsync: deleteNlpValue } = useDelete( - EntityType.NLP_VALUE, - options, - ); - const { mutate: deleteNlpValues } = useDeleteMany( - EntityType.NLP_VALUE, - options, - ); + }); const [selectedNlpValues, setSelectedNlpValues] = useState([]); const actionColumns = useActionColumns( EntityType.NLP_VALUE, @@ -95,8 +95,7 @@ export const NlpValues = ({ entityId }: { entityId: string }) => { const isConfirmed = await dialogs.confirm(ConfirmDialogBody); if (isConfirmed) { - await deleteNlpValue(id); - refetchEntity(); + deleteNlpValue(id); } }, }, diff --git a/frontend/src/components/nlp/components/NlpValueForm.tsx b/frontend/src/components/nlp/components/NlpValueForm.tsx index eb16bc96..68bcbe98 100644 --- a/frontend/src/components/nlp/components/NlpValueForm.tsx +++ b/frontend/src/components/nlp/components/NlpValueForm.tsx @@ -33,7 +33,18 @@ export const NlpValueForm: FC< entity: EntityType.NLP_ENTITY, format: Format.FULL, }); - const options = { + const { mutate: createNlpValue } = useCreate(EntityType.NLP_VALUE, { + onError: () => { + rest.onError?.(); + toast.error(t("message.internal_server_error")); + }, + onSuccess() { + rest.onSuccess?.(); + refetchEntity(); + toast.success(t("message.success_save")); + }, + }); + const { mutate: updateNlpValue } = useUpdate(EntityType.NLP_VALUE, { onError: () => { rest.onError?.(); toast.error(t("message.internal_server_error")); @@ -42,12 +53,7 @@ export const NlpValueForm: FC< rest.onSuccess?.(); toast.success(t("message.success_save")); }, - }; - const { mutateAsync: createNlpValue } = useCreate( - EntityType.NLP_VALUE, - options, - ); - const { mutate: updateNlpValue } = useUpdate(EntityType.NLP_VALUE, options); + }); const { reset, register, handleSubmit, control } = useForm< INlpValueAttributes & { expressions: string[]; @@ -69,8 +75,7 @@ export const NlpValueForm: FC< if (data) { updateNlpValue({ id: data.id, params }); } else { - await createNlpValue({ ...params, entity: String(query.id) }); - refetchEntity(); + createNlpValue({ ...params, entity: String(query.id) }); } };