import { useEffect, useRef } from 'react';
import { useQuery } from '@tanstack/react-query';
import { FormHandles } from '@unform/core';
import { AxiosInstance } from 'axios';
import { FiX } from 'react-icons/fi';
import { insertVariablesInHref } from '../../helpers/insertVariablesInHref';
import { MaskType } from '../../helpers/masks';
import { Button } from '../Button';
import { Card } from '../Card';
import { Flex } from '../Flex';
import { Footer, FooterLeft } from '../Footer';
import {
DatePicker,
Form,
Input,
MaskInput,
MoneyInput,
NumberInput,
PercentageInput,
PhoneInput,
Select,
Switch,
} from '../Form';
import { Heading } from '../Heading';
import { IconButton } from '../IconButton';
import { Text } from '../Text';
import {
FieldType,
OptionType,
usePageBuilderStore,
} from './stores/pageBuilder';
interface RenderSelectProps {
field: FieldType;
agent: AxiosInstance;
disabled: boolean;
}
function RenderSelect({
field,
agent,
disabled,
}: RenderSelectProps): JSX.Element {
const { changeParent, filterParents } = usePageBuilderStore((state) => ({
changeParent: state.changeParent,
filterParents: state.filterParents,
}));
if (!field.options?.length && !field.url) {
return Imcomplete data for select;
}
const { data } = useQuery(
[field.url],
async () => {
const response = await agent.get(
field.parent
? insertVariablesInHref(field.url ?? '', filterParents)
: field.url ?? '',
);
return response.data;
},
{
placeholderData: [],
enabled: !field.options?.length && !!field.url && !disabled,
staleTime: 1000 * 60 * 5, // 5 minutes
},
);
return (