import { useEffect, useMemo, useRef, useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { FormHandles } from '@unform/core';
import { AxiosInstance } from 'axios';
import * as Yup from 'yup';
import { getValidationErrors } from '../../helpers/getValidationErrors';
import { insertVariablesInHref } from '../../helpers/insertVariablesInHref';
import { MaskType } from '../../helpers/masks';
import { useLoading } from '../../hooks/loading';
import { Button } from '../Button';
import { Card } from '../Card';
import { Footer } from '../Footer';
import {
DatePicker,
Form,
Input,
MaskInput,
MoneyInput,
NumberInput,
PercentageInput,
PhoneInput,
Select,
Switch,
} from '../Form';
import { Grid } from '../Grid';
import { Text } from '../Text';
import {
ChangeParentData,
ConfigType,
FormFieldType,
OptionType,
} from './stores/pageBuilder';
interface RenderSelectProps {
field: FormFieldType;
agent: AxiosInstance;
disabled: boolean;
changeParent: (changeParent: ChangeParentData) => void;
parents: { [key: string]: string | number };
}
function RenderSelect({
field,
agent,
disabled,
changeParent,
parents,
}: RenderSelectProps): JSX.Element {
if (!field.options?.length && !field.url) {
return Imcomplete data for select;
}
const { data, refetch } = useQuery(
[field.url],
async () => {
const response = await agent.get(
field.parent
? insertVariablesInHref(field.url ?? '', parents)
: field.url ?? '',
);
return response.data;
},
{
placeholderData: [],
enabled: !field.options?.length && !!field.url && !disabled,
staleTime: 1000 * 60 * 5, // 5 minutes
},
);
useEffect(() => {
if (parents[field.parent ?? '']) {
refetch();
}
}, [parents, refetch, field.parent]);
return (