diff options
author | eug-vs <eug-vs@keemail.me> | 2021-03-17 03:46:21 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-03-17 03:46:21 +0300 |
commit | 2305464999fdccdb809ce425cda8346ddc3df493 (patch) | |
tree | 6e511f2fda9f019ecb6cddde8ec079c2ef0a278b /src/hooks | |
parent | 640739f3234dfe3392566e76fc950dde4b42af09 (diff) | |
download | commercel-ui-2305464999fdccdb809ce425cda8346ddc3df493.tar.gz |
feat: adapt useOptions to the new logic
Diffstat (limited to 'src/hooks')
-rw-r--r-- | src/hooks/useOptions.ts | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/src/hooks/useOptions.ts b/src/hooks/useOptions.ts index de8fc95..2a5aee6 100644 --- a/src/hooks/useOptions.ts +++ b/src/hooks/useOptions.ts @@ -1,4 +1,5 @@ import { useEffect } from 'react'; +import _ from 'lodash'; import { Option } from '../components/Select'; import hooks from './useAPIClient'; @@ -6,7 +7,7 @@ import hooks from './useAPIClient'; // setting the default value in formik const useOptions = ( service: string, - field: string, + fields: string[], values: Record<string, any>, setFieldValue: (any) => void, mapper = item => ({ key: item._id, label: item.name }), @@ -16,10 +17,11 @@ const useOptions = ( const options = items?.map(mapper); useEffect(() => { - if (items?.length && !values[field]) { - setFieldValue(field, items[0]._id); - } - }, [items, setFieldValue]); + // Initialize all empty fields + if (items?.length) fields.forEach(field => { + if (!_.get(values, field)) setFieldValue(field, items[0]._id); + }); + }, [items, values, setFieldValue]); return options; }; |