Skip to content

Commit

Permalink
Fix entities and regexes requests (#791)
Browse files Browse the repository at this point in the history
* PoC Fix

* SImplify fix

* Fix other places
  • Loading branch information
IgorKrupenja authored Jan 10, 2025
1 parent b881ae0 commit be93010
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
2 changes: 2 additions & 0 deletions GUI/src/pages/Training/Intents/IntentExamplesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Intent } from '../../../types/intent';
import LoadingDialog from '../../../components/LoadingDialog';
import i18n from '../../../../i18n';
import { t } from 'i18next';
import { getEntities } from 'services/entities';

type IntentExamplesTableProps = {
intent: Intent;
Expand Down Expand Up @@ -46,6 +47,7 @@ const IntentExamplesTable: FC<IntentExamplesTableProps> = ({ intent, updateSelec

const { data: entitiesResponse } = useQuery<{ response: Entity[] }>({
queryKey: ['entities'],
queryFn: () => getEntities(),
});

const examples = useMemo(
Expand Down
22 changes: 11 additions & 11 deletions GUI/src/pages/Training/IntentsFollowupTraining/RegexDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,21 @@ import {
deleteRegexExample,
downloadExamples,
editRegex,
editRegexExample
editRegexExample,
getRegexes
} from 'services/regex';
import { Entity } from 'types/entity';
import i18n from '../../../../i18n';
import withAuthorization, { ROLES } from 'hoc/with-authorization';
import { getEntities } from 'services/entities';
import { RegexTeaser } from 'types/regex';

type Regex = {
readonly id: string;
name: string;
examples: string[];
}

type RegexTeaser = {
readonly id: number;
name: string;
}

const RegexDetail: FC = () => {
const { id } = useParams();
let updatedExampleName = '';
Expand All @@ -55,11 +53,13 @@ const RegexDetail: FC = () => {
const { data: regex, refetch } = useQuery<Regex>(['regex',id, 'examples']);
const [deletableRow, setDeletableRow] = useState<string | undefined | null>(null);
const [deletableRegex, setDeletableRegex] = useState<string | number | null>(null);
const { data: entities } = useQuery<Entity[]>({
const { data } = useQuery<{ response: Entity[]}>({
queryKey: ['entities'],
queryFn: () => getEntities(),
});
const { data: existingRegexes} = useQuery<RegexTeaser[]>({
const { data: existingRegexes } = useQuery<{ response: RegexTeaser[]}>({
queryKey: ['regexes'],
queryFn: () => getRegexes(),
});
const [editRegexName, setEditRegexName] = useState<string>('');

Expand All @@ -79,9 +79,9 @@ const RegexDetail: FC = () => {
[regex],
);

const availableEntities = useMemo(() => entities?.filter((e) => {
return !existingRegexes?.some((r) => r.name === e.name);
}).map((e) => ({ label: e.name, value: String(e.id) })), [entities, regexList]);
const availableEntities = useMemo(() => data?.response.filter((e) => {
return !existingRegexes?.response.some((r) => r.name === e.name);
}).map((e) => ({ label: e.name, value: String(e.id) })), [data, regexList]);

useEffect(() => {
const result = regex?.examples.map((e, index) => ({ id: index, value: e }));
Expand Down
8 changes: 5 additions & 3 deletions GUI/src/pages/Training/Slots/SlotsDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useToast } from 'hooks/useToast';
import { createSlot, editSlot } from 'services/slots';
import { Slot, SlotCreateDTO, SlotEditDTO } from 'types/slot';
import withAuthorization, { ROLES } from 'hoc/with-authorization';
import { getEntities } from 'services/entities';

type SlotsDetailProps = {
mode: 'new' | 'edit';
Expand Down Expand Up @@ -50,8 +51,9 @@ const SlotsDetail: FC<SlotsDetailProps> = ({ mode }) => {
const { data: intents } = useQuery<Intent[]>({
queryKey: ['intent-and-id'],
});
const { data: entities } = useQuery<Entity[]>({
const { data } = useQuery<{ response: Entity[]}>({
queryKey: ['entities'],
queryFn: () => getEntities(),
});

const { register, formState: { errors },control, handleSubmit, reset } = useForm<SlotCreateDTO>({
Expand Down Expand Up @@ -162,13 +164,13 @@ const SlotsDetail: FC<SlotsDetailProps> = ({ mode }) => {
<h2 className='h5'>{t('training.slots.mapping')}</h2>
}>
<Track direction='vertical' gap={8} align='left'>
{entities && selectedSlotType === 'from_entity' && (
{data?.response && selectedSlotType === 'from_entity' && (
<Controller name='mappings.entity' control={control} render={({ field }) =>
<FormSelect
{...field}
label='Entity'
defaultValue={selectedEntity}
options={entities.map((entity) => ({ label: entity.name, value: entity.name }))}
options={data.response.map((entity) => ({ label: entity.name, value: entity.name }))}
onSelectionChange={(selection) => {
setSelectedEntity((selection?.value));
field.onChange(selection?.value);
Expand Down
6 changes: 4 additions & 2 deletions GUI/src/pages/Training/Stories/IntentNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {Button, FormSelect, Icon, Track} from 'components';
import { Entity } from 'types/entity';
import ToolTipWarning from "../../../components/ToolTipWarning";
import { getEntities } from 'services/entities';

type NodeDataProps = {
data: {
Expand All @@ -34,8 +35,9 @@ type EntityPayload = (string | undefined)[] | undefined;
const IntentNode: FC<NodeDataProps> = ({ data }) => {
const [entityWarning, setEntityWarning] = useState(false);
const { t } = useTranslation();
const { data: entities } = useQuery<Entity[]>({
const { data: entities } = useQuery<{ response: Entity[]}>({
queryKey: ['entities'],
queryFn: () => getEntities(),
});
const { control, watch } = useForm<{ entities: { label: string; value: string }[] }>({
defaultValues: {
Expand Down Expand Up @@ -81,7 +83,7 @@ const IntentNode: FC<NodeDataProps> = ({ data }) => {
}}
label={t('training.intents.entity')}
hideLabel
options={entities?.map((e) => ({ label: e.name, value: String(e.id) })) || []}
options={entities?.response.map((e) => ({ label: e.name, value: String(e.id) })) || []}
/>
)}
/>
Expand Down
8 changes: 3 additions & 5 deletions GUI/src/services/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { PaginationParams } from 'types/api';
import { rasaApi } from './api';
import { Entity } from 'types/entity';

export const getEntities = async ({
pageParam,
pageSize,
filter,
}: PaginationParams): Promise<{ response: Entity[] }> => {
export const getEntities = async (
{ pageParam, pageSize, filter }: PaginationParams = { pageParam: 0, pageSize: 1000, filter: '' }
): Promise<{ response: Entity[] }> => {
const { data } = await rasaApi.get(`/entities?size=${pageSize}&filter=${filter}&from=${pageParam}`);
return data;
};
Expand Down
2 changes: 1 addition & 1 deletion GUI/src/services/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const getRegexes = async ({
pageParam,
pageSize,
filter,
}: PaginationParams): Promise<{ response: RegexTeaser[] }> => {
}: PaginationParams = { pageParam: 0, pageSize: 1000, filter: '' }): Promise<{ response: RegexTeaser[] }> => {
const { data } = await rasaApi.get(`/regexes?size=${pageSize}&filter=${filter}&from=${pageParam}`);
return data;
};
Expand Down

0 comments on commit be93010

Please sign in to comment.