blob: 2a5aee695749a3104c7305ca2f2977fb0e90eb5b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
import { useEffect } from 'react';
import _ from 'lodash';
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,
fields: string[],
values: Record<string, any>,
setFieldValue: (any) => void,
mapper = item => ({ key: item._id, label: item.name }),
): Option[] => {
const { data: items } = hooks[service].useList();
const options = items?.map(mapper);
useEffect(() => {
// Initialize all empty fields
if (items?.length) fields.forEach(field => {
if (!_.get(values, field)) setFieldValue(field, items[0]._id);
});
}, [items, values, setFieldValue]);
return options;
};
export default useOptions;
|