plugin updates
This commit is contained in:
@@ -1,53 +0,0 @@
|
||||
import { useState } from 'react';
|
||||
import {
|
||||
usePostAsyncBackgroundMessage,
|
||||
usePostBackgroundMessage,
|
||||
} from '../../../iframe/useBackgroundApp';
|
||||
import { FormType } from '../../../constants/defaultFormOptions';
|
||||
import LoadState, { LoadStateType } from '../../enums/loadState';
|
||||
import { ProxyMessages } from '../../../iframe/integratedMessages';
|
||||
|
||||
export default function useCreateFormFromTemplate(origin = 'gutenberg') {
|
||||
const proxy = usePostAsyncBackgroundMessage();
|
||||
const track = usePostBackgroundMessage();
|
||||
const [loadState, setLoadState] = useState<LoadStateType>(LoadState.Idle);
|
||||
const [formApiError, setFormApiError] = useState<any>(null);
|
||||
|
||||
const createFormByTemplate = (type: FormType) => {
|
||||
setLoadState(LoadState.Loading);
|
||||
track({
|
||||
key: ProxyMessages.TrackFormCreatedFromTemplate,
|
||||
payload: {
|
||||
type,
|
||||
origin,
|
||||
},
|
||||
});
|
||||
|
||||
return proxy({
|
||||
key: ProxyMessages.CreateFormFromTemplate,
|
||||
payload: type,
|
||||
})
|
||||
.then(form => {
|
||||
setLoadState(LoadState.Idle);
|
||||
return form;
|
||||
})
|
||||
.catch(err => {
|
||||
setFormApiError(err);
|
||||
track({
|
||||
key: ProxyMessages.TrackFormCreationFailed,
|
||||
payload: {
|
||||
origin,
|
||||
},
|
||||
});
|
||||
setLoadState(LoadState.Failed);
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
isCreating: loadState === LoadState.Loading,
|
||||
hasError: loadState === LoadState.Failed,
|
||||
formApiError,
|
||||
createFormByTemplate,
|
||||
reset: () => setLoadState(LoadState.Idle),
|
||||
};
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
import { useState } from 'react';
|
||||
import debounce from 'lodash/debounce';
|
||||
import { usePostAsyncBackgroundMessage } from '../../../iframe/useBackgroundApp';
|
||||
import { DEFAULT_OPTIONS } from '../../../constants/defaultFormOptions';
|
||||
import { ProxyMessages } from '../../../iframe/integratedMessages';
|
||||
import { IForm } from '../../types';
|
||||
|
||||
export default function useForms() {
|
||||
const proxy = usePostAsyncBackgroundMessage();
|
||||
const [formApiError, setFormApiError] = useState<any>(null);
|
||||
|
||||
const search = debounce(
|
||||
(search: string, callback: Function) => {
|
||||
return proxy({
|
||||
key: ProxyMessages.FetchForms,
|
||||
payload: {
|
||||
search,
|
||||
},
|
||||
})
|
||||
.then(forms => {
|
||||
callback([
|
||||
...forms.map((form: IForm) => ({
|
||||
label: form.name,
|
||||
value: form.guid,
|
||||
})),
|
||||
DEFAULT_OPTIONS,
|
||||
]);
|
||||
})
|
||||
.catch(error => {
|
||||
setFormApiError(error);
|
||||
});
|
||||
},
|
||||
300,
|
||||
{ trailing: true }
|
||||
);
|
||||
|
||||
return {
|
||||
search,
|
||||
formApiError,
|
||||
reset: () => setFormApiError(null),
|
||||
};
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
import $ from 'jquery';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { formsScript } from '../../../constants/leadinConfig';
|
||||
import Raven from '../../../lib/Raven';
|
||||
|
||||
let promise: Promise<string | undefined>;
|
||||
|
||||
function loadFormsScript() {
|
||||
if (!promise) {
|
||||
promise = new Promise((resolve, reject) =>
|
||||
$.getScript(formsScript)
|
||||
.done(resolve)
|
||||
.fail(reject)
|
||||
);
|
||||
}
|
||||
return promise;
|
||||
}
|
||||
|
||||
export default function useFormScript() {
|
||||
const [ready, setReady] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
loadFormsScript()
|
||||
.then(() => setReady(true))
|
||||
.catch(error => Raven.captureException(error));
|
||||
}, []);
|
||||
|
||||
return ready;
|
||||
}
|
||||
Reference in New Issue
Block a user