diff --git a/frontend/src/app-components/inputs/NlpPatternSelect.tsx b/frontend/src/app-components/inputs/NlpPatternSelect.tsx index f8e5782..5d241ee 100644 --- a/frontend/src/app-components/inputs/NlpPatternSelect.tsx +++ b/frontend/src/app-components/inputs/NlpPatternSelect.tsx @@ -12,11 +12,12 @@ import { CircularProgress, IconButton, InputAdornment, + Skeleton, Typography, useTheme, } from "@mui/material"; import Autocomplete from "@mui/material/Autocomplete"; -import { forwardRef, SyntheticEvent, useState } from "react"; +import { forwardRef, SyntheticEvent, useRef, useState } from "react"; import { Input } from "@/app-components/inputs/Input"; import { useFind } from "@/hooks/crud/useFind"; @@ -26,6 +27,7 @@ import { useTranslate } from "@/hooks/useTranslate"; import { EntityType, Format } from "@/services/types"; import { NlpPattern } from "@/types/block.types"; import { INlpEntity } from "@/types/nlp-entity.types"; +import { INlpValue } from "@/types/nlp-value.types"; type NlpPatternSelectProps = { patterns: NlpPattern[]; onChange: any }; @@ -33,6 +35,7 @@ const NlpPatternSelect = ( { patterns, onChange }: NlpPatternSelectProps, ref, ) => { + const inputRef = useRef(null); const [selected, setSelected] = useState(patterns); const theme = useTheme(); const { t } = useTranslate(); @@ -94,6 +97,13 @@ const NlpPatternSelect = ( onChange(undefined, newSelection); }; + + if (!options.length) { + return ( + + ); + } + const defaultValue = options.filter(({ name }) => selected.find(({ entity: entityName }) => entityName === name), @@ -154,21 +164,23 @@ const NlpPatternSelect = ( {entities.map((entity, index) => { const { key, onDelete } = getTagProps({ index }); const handleChange = ( - event: SyntheticEvent, + _event: SyntheticEvent, valueId: string, ) => { handleNlpValueChange(entity, valueId); }; - const selected = ( - Array.isArray(patterns) - ? patterns?.find((e) => e.entity === entity.name) - : {} - ) as NlpPattern; + const values = entity.values.map((vId) => + getNlpValueFromCache(vId), + ) as INlpValue[]; + const selectedValue = patterns?.find( + (e) => e.entity === entity.name, + )?.value; + const value = values.find(({ value }) => value === selectedValue); return ( ( - + renderInput={(props) => { + return ( + {entity.name} - - ), - endAdornment: ( - - {isLoading ? ( - - ) : ( - { - onDelete(e); + ), + endAdornment: ( + + {isLoading ? ( + + ) : ( + { + onDelete(e); - onChange( - undefined, - patterns.filter( - (p) => p.entity !== entity.name, - ), - ); - }} - edge="end" - size="small" - > - - - )} - - ), - }} - /> - )} + onChange( + undefined, + patterns.filter( + (p) => p.entity !== entity.name, + ), + ); + }} + edge="end" + size="small" + > + + + )} + + ), + }} + /> + ); + }} /> ); })} @@ -264,6 +280,13 @@ const NlpPatternSelect = ( onChange={(e) => onSearch(e.target.value)} InputProps={{ ...props.InputProps, + inputRef, + onClick: (event) => { + if (event.target !== inputRef.current) { + event.stopPropagation(); + event.preventDefault(); + } + }, endAdornment: isLoading ? ( ) : null,