fix: nlp form update

This commit is contained in:
Mohamed Marrouchi 2024-09-25 09:06:56 +01:00
parent 7425f60284
commit d04f2f3ce4
3 changed files with 39 additions and 68 deletions

View File

@ -20,10 +20,11 @@ import { ContentItem } from "@/app-components/dialogs/layouts/ContentItem";
import { Input } from "@/app-components/inputs/Input"; import { Input } from "@/app-components/inputs/Input";
import MultipleInput from "@/app-components/inputs/MultipleInput"; import MultipleInput from "@/app-components/inputs/MultipleInput";
import { useCreate } from "@/hooks/crud/useCreate"; import { useCreate } from "@/hooks/crud/useCreate";
import { useGet } from "@/hooks/crud/useGet";
import { useUpdate } from "@/hooks/crud/useUpdate"; import { useUpdate } from "@/hooks/crud/useUpdate";
import { DialogControlProps } from "@/hooks/useDialog"; import { DialogControlProps } from "@/hooks/useDialog";
import { useToast } from "@/hooks/useToast"; import { useToast } from "@/hooks/useToast";
import { EntityType } from "@/services/types"; import { EntityType, Format } from "@/services/types";
import { INlpValue, INlpValueAttributes } from "@/types/nlp-value.types"; import { INlpValue, INlpValueAttributes } from "@/types/nlp-value.types";
export type TNlpValueAttributesWithRequiredExpressions = INlpValueAttributes & { export type TNlpValueAttributesWithRequiredExpressions = INlpValueAttributes & {
@ -44,11 +45,16 @@ export const NlpValueDialog: FC<NlpValueDialogProps> = ({
const { t } = useTranslation(); const { t } = useTranslation();
const { toast } = useToast(); const { toast } = useToast();
const { query } = useRouter(); const { query } = useRouter();
const { refetch: refetchEntity } = useGet(data?.entity || String(query.id), {
entity: EntityType.NLP_ENTITY,
format: Format.FULL,
});
const { mutateAsync: createNlpValue } = useCreate(EntityType.NLP_VALUE, { const { mutateAsync: createNlpValue } = useCreate(EntityType.NLP_VALUE, {
onError: () => { onError: () => {
toast.error(t("message.internal_server_error")); toast.error(t("message.internal_server_error"));
}, },
onSuccess(data) { onSuccess(data) {
refetchEntity();
closeDialog(); closeDialog();
toast.success(t("message.success_save")); toast.success(t("message.success_save"));
callback?.(data); callback?.(data);

View File

@ -23,7 +23,7 @@ import {
RadioGroup, RadioGroup,
Typography, Typography,
} from "@mui/material"; } from "@mui/material";
import { FC, useCallback, useMemo, useState } from "react"; import { FC, useCallback, useEffect, useMemo, useState } from "react";
import { Controller, useFieldArray, useForm } from "react-hook-form"; import { Controller, useFieldArray, useForm } from "react-hook-form";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useQuery } from "react-query"; import { useQuery } from "react-query";
@ -65,71 +65,40 @@ const NlpDatasetSample: FC<NlpDatasetSampleProps> = ({
{ {
hasCount: false, hasCount: false,
}, },
{
onSuccess(entities) {
// By default append trait entities
if (!sample) {
removeTraitEntity();
(entities || [])
.filter(({ lookups }) => lookups.includes("trait"))
.forEach(({ name }) => {
appendTraitEntity({
entity: name,
value: "",
});
});
}
},
},
); );
const getNlpValueFromCache = useGetFromCache(EntityType.NLP_VALUE); const getNlpValueFromCache = useGetFromCache(EntityType.NLP_VALUE);
// Default trait entities to append to the form // eslint-disable-next-line react-hooks/exhaustive-deps
const defaultTraitEntities = useMemo(() => { const defaultValues: INlpSampleFormAttributes = useMemo(
if (!sample || !entities) return []; () => ({
type: sample?.type || NlpSampleType.train,
const traitEntities = entities.filter(({ lookups }) => text: sample?.text || "",
lookups.includes("trait"), language: sample?.language || null,
); traitEntities: (entities || [])
const sampleTraitEntities = sample.entities.filter( .filter(({ lookups }) => {
(e) => "start" in e && typeof e.start === "undefined", return lookups.includes("trait");
); })
.map((e) => {
if (sampleTraitEntities.length === traitEntities.length) { return {
return sampleTraitEntities; entity: e.name,
} value: sample
? sample.entities.find(({ entity }) => entity === e.name)?.value
const sampleEntityNames = new Set(sampleTraitEntities.map((e) => e.entity)); : "",
const missingEntities = traitEntities } as INlpDatasetTraitEntity;
.filter(({ name }) => !sampleEntityNames.has(name)) }),
.map(({ name }) => ({ keywordEntities: (sample?.entities || []).filter(
entity: name, (e) => "start" in e && typeof e.start === "number",
value: "", ) as INlpDatasetKeywordEntity[],
})); }),
[sample, entities],
return [...sampleTraitEntities, ...missingEntities]; );
}, [entities, sample]);
const { handleSubmit, control, register, reset, setValue, watch } = const { handleSubmit, control, register, reset, setValue, watch } =
useForm<INlpSampleFormAttributes>({ useForm<INlpSampleFormAttributes>({
defaultValues: { defaultValues,
type: sample?.type || NlpSampleType.train,
text: sample?.text || "",
language: sample?.language,
traitEntities: defaultTraitEntities,
keywordEntities:
sample?.entities.filter(
(e) => "start" in e && typeof e.start === "number",
) || [],
},
}); });
const currentText = watch("text"); const currentText = watch("text");
const currentType = watch("type"); const currentType = watch("type");
const { apiClient } = useApiClient(); const { apiClient } = useApiClient();
const { const { fields: traitEntities, update: updateTraitEntity } = useFieldArray({
fields: traitEntities,
append: appendTraitEntity,
update: updateTraitEntity,
remove: removeTraitEntity,
} = useFieldArray({
control, control,
name: "traitEntities", name: "traitEntities",
}); });
@ -187,16 +156,14 @@ const NlpDatasetSample: FC<NlpDatasetSampleProps> = ({
} | null>(null); } | null>(null);
const onSubmitForm = (form: INlpSampleFormAttributes) => { const onSubmitForm = (form: INlpSampleFormAttributes) => {
submitForm(form); submitForm(form);
reset({
type: form?.type || NlpSampleType.train,
text: "",
language: form?.language,
traitEntities: defaultTraitEntities,
keywordEntities: [],
});
refetchEntities(); refetchEntities();
}; };
useEffect(() => {
reset(defaultValues);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [JSON.stringify(defaultValues)]);
return ( return (
<Box className="nlp-train" sx={{ position: "relative", p: 2 }}> <Box className="nlp-train" sx={{ position: "relative", p: 2 }}>
<form onSubmit={handleSubmit(onSubmitForm)}> <form onSubmit={handleSubmit(onSubmitForm)}>

View File

@ -142,8 +142,6 @@ export const NlpValues = ({ entityId }: { entityId: string }) => {
return ( return (
<Grid container gap={2} flexDirection="column"> <Grid container gap={2} flexDirection="column">
{/* <PageHeader title={t("title.nlp_train")} icon={faGraduationCap} /> */}
<Slide direction={direction} in={true} mountOnEnter unmountOnExit> <Slide direction={direction} in={true} mountOnEnter unmountOnExit>
<Grid item xs={12}> <Grid item xs={12}>
<Box sx={{ padding: 1 }}> <Box sx={{ padding: 1 }}>