import React, { useRef, useState } from 'react'; import { Form, Input } from 'UI'; import { useStore } from 'App/mstore'; import { useModal } from 'Components/Modal'; import { toast } from 'react-toastify'; import { Button, Modal } from 'antd'; import { Trash } from 'UI/Icons'; import { observer } from 'mobx-react-lite'; import styles from './customFieldForm.module.css'; import { useTranslation } from 'react-i18next'; interface CustomFieldFormProps { siteId: string; } const CustomFieldForm: React.FC = ({ siteId }) => { const { t } = useTranslation(); const focusElementRef = useRef(null); const { customFieldStore: store } = useStore(); const field = store.instance; const { hideModal } = useModal(); const [loading, setLoading] = useState(false); const write = ({ target: { value, name } }: any) => store.edit({ [name]: value }); const exists = field?.exists(); const onDelete = async () => { Modal.confirm({ title: t('Metadata'), content: t('Are you sure you want to remove?'), onOk: async () => { await store.remove(siteId, field?.index!); hideModal(); }, }); }; const onSave = (field: any) => { setLoading(true); store .save(siteId, field) .then((response) => { if (!response || !response.errors || response.errors.size === 0) { hideModal(); toast.success(t('Metadata added successfully!')); } else { toast.error(response.errors[0]); } }) .catch(() => { toast.error(t('An error occurred while saving metadata.')); }) .finally(() => { setLoading(false); }); }; return (

{exists ? t('Update') : 'Add'} {t('Metadata Field')}

); }; export default observer(CustomFieldForm);