PluginProbe
HubSpot All-In-One Marketing – Forms, Popups, Live Chat / 11.3.69
HubSpot All-In-One Marketing – Forms, Popups, Live Chat v11.3.69
11.3.75 11.3.73 11.3.71 11.3.70 11.3.69 11.3.64 11.3.65 11.3.62 11.3.61 11.3.56 11.3.58 11.0.31 11.0.52 11.0.54 11.0.56 11.0.58 11.0.7 11.1.10 11.1.11 11.1.13 11.1.14 11.1.15 11.1.2 11.1.20 11.1.21 All 73 releases
leadin / scripts / shared / Form / hooks / useForms.ts

useForms.ts in HubSpot All-In-One Marketing – Forms, Popups, Live Chat 11.3.69, at scripts/shared/Form/hooks/useForms.ts

54 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useState } from 'react';
2 import debounce from 'lodash/debounce';
3 import { usePostAsyncBackgroundMessage } from '../../../iframe/useBackgroundApp';
4 import { ProxyMessages } from '../../../iframe/integratedMessages';
5 import { IForm } from '../../types';
6 import useGetTemplateAvailability, {
7 getTemplateOptions,
8 } from './useGetTemplateAvailability';
9
10 export default function useForms() {
11 const proxy = usePostAsyncBackgroundMessage();
12 const [formApiError, setFormApiError] = useState<any>(null);
13 const { availabilityPromise } = useGetTemplateAvailability();
14
15 const search = debounce(
16 (search: string, callback: Function) => {
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
31 callback([
32 ...forms.map((form: IForm) => ({
33 label: form.name,
34 value: form.guid,
35 embedVersion: form.embedVersion,
36 })),
37 TEMPLATE_OPTIONS,
38 ]);
39 })
40 .catch(error => {
41 setFormApiError(error);
42 });
43 },
44 300,
45 { trailing: true }
46 );
47
48 return {
49 search,
50 formApiError,
51 reset: () => setFormApiError(null),
52 };
53 }
54