import { observer } from 'mobx-react-lite'; import React from 'react'; import { useStore } from 'App/mstore'; import { namedStore } from 'App/mstore/integrationsStore'; import { Button, Checkbox, Form, Input, Loader } from 'UI'; import { toast } from 'react-toastify'; function IntegrationForm(props: any) { const { formFields, name, integrated } = props; const { integrationsStore } = useStore(); const initialSiteId = integrationsStore.integrations.siteId; const integrationStore = integrationsStore[name as unknown as namedStore]; const config = integrationStore.instance; const loading = integrationStore.loading; const onSave = integrationStore.saveIntegration; const onRemove = integrationStore.deleteIntegration; const edit = integrationStore.edit; const fetchIntegrationList = integrationsStore.integrations.fetchIntegrations; const fetchList = () => { void fetchIntegrationList(initialSiteId); }; const write = ({ target: { value, name: key, type, checked } }) => { if (type === 'checkbox') edit({ [key]: checked }); else edit({ [key]: value }); }; const save = () => { const { name, customPath } = props; onSave(customPath || name).then(() => { fetchList(); props.onClose(); }).catch(async (error) => { if (error.response) { const errorResponse = await error.response.json(); if (errorResponse.errors && Array.isArray(errorResponse.errors)) { toast.error(errorResponse.errors.map((e: any) => e).join(', ')); } else { toast.error('Failed to save integration'); } } }); }; const remove = () => { onRemove().then(() => { props.onClose(); fetchList(); }); }; return (
{formFields.map( ({ key, label, placeholder = label, component: Component = 'input', type = 'text', checkIfDisplayed, autoFocus = false, }) => (typeof checkIfDisplayed !== 'function' || checkIfDisplayed(config)) && (type === 'checkbox' ? ( ) : ( )) )} {integrated && ( )}
); } export default observer(IntegrationForm);