| @@ -1,30 +1,41 @@ | ||
| 1 | 1 | import { useState } from 'react'; |
| 2 | 2 | import debounce from 'lodash/debounce'; |
| 3 | 3 | import { usePostAsyncBackgroundMessage } from '../../../iframe/useBackgroundApp'; |
| 4 | -import { DEFAULT_OPTIONS } from '../../../constants/defaultFormOptions'; | |
| 5 | 4 | import { ProxyMessages } from '../../../iframe/integratedMessages'; |
| 6 | 5 | import { IForm } from '../../types'; |
| 6 | +import useGetTemplateAvailability, { | |
| 7 | + getTemplateOptions, | |
| 8 | +} from './useGetTemplateAvailability'; | |
| 7 | 9 | |
| 8 | 10 | export default function useForms() { |
| 9 | 11 | const proxy = usePostAsyncBackgroundMessage(); |
| 10 | 12 | const [formApiError, setFormApiError] = useState<any>(null); |
| 13 | + const { availabilityPromise } = useGetTemplateAvailability(); | |
| 11 | 14 | |
| 12 | 15 | const search = debounce( |
| 13 | 16 | (search: string, callback: Function) => { |
| 14 | - return proxy({ | |
| 15 | - key: ProxyMessages.FetchForms, | |
| 16 | - payload: { | |
| 17 | - search, | |
| 18 | - }, | |
| 19 | - }) | |
| 20 | - .then(forms => { | |
| 17 | + return Promise.all([ | |
| 18 | + availabilityPromise, | |
| 19 | + proxy({ | |
| 20 | + key: ProxyMessages.FetchForms, | |
| 21 | + payload: { | |
| 22 | + search, | |
| 23 | + }, | |
| 24 | + }), | |
| 25 | + ]) | |
| 26 | + .then(([templateAvailabilityResponse, forms]) => { | |
| 27 | + const TEMPLATE_OPTIONS = getTemplateOptions( | |
| 28 | + templateAvailabilityResponse.templateAvailability | |
| 29 | + ); | |
| 30 | + | |
| 21 | 31 | callback([ |
| 22 | 32 | ...forms.map((form: IForm) => ({ |
| 23 | 33 | label: form.name, |
| 24 | 34 | value: form.guid, |
| 35 | + embedVersion: form.embedVersion, | |
| 25 | 36 | })), |
| 26 | - DEFAULT_OPTIONS, | |
| 37 | + TEMPLATE_OPTIONS, | |
| 27 | 38 | ]); |
| 28 | 39 | }) |
| 29 | 40 | .catch(error => { |
| 30 | 41 | setFormApiError(error); |