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'; function IntegrationForm(props: any) { const { formFields, name, integrated } = props; const { integrationsStore, projectsStore } = useStore(); const initialSiteId = projectsStore.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(); }); }; 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);