import { useEffect } from 'react'; import { Option } from '../components/Select'; import hooks from './useAPIClient'; // Load service entities and map them into select options // setting the default value in formik const useOptions = ( service: string, field: string, values: Record, setFieldValue: (any) => void, mapper = item => ({ key: item._id, label: item.name }), ): Option[] => { const { data: items } = hooks[service].useList(); const options = items?.map(mapper); useEffect(() => { if (items?.length && !values[field]) { setFieldValue(field, items[0]._id); } }, [items, setFieldValue]); return options; }; export default useOptions;