import React from 'react'; import { SubmitHandler, useForm, useWatch } from 'react-hook-form'; import { Machine } from '../../models/machine'; import MachineAPI from '../../api/machine'; import { useTranslation } from 'react-i18next'; import { FormInput } from '../form/form-input'; import { FormImageUpload } from '../form/form-image-upload'; import { IApplication } from '../../models/application'; import { Loader } from '../base/loader'; import { react2angular } from 'react2angular'; import { ErrorBoundary } from '../base/error-boundary'; import { FormRichText } from '../form/form-rich-text'; import { FormSwitch } from '../form/form-switch'; import { FormMultiFileUpload } from '../form/form-multi-file-upload'; import { FabButton } from '../base/fab-button'; declare const Application: IApplication; interface MachineFormProps { action: 'create' | 'update', machine?: Machine, onError: (message: string) => void, onSuccess: (message: string) => void, } /** * Form to edit or create machines */ export const MachineForm: React.FC = ({ action, machine, onError, onSuccess }) => { const { handleSubmit, register, control, setValue, formState } = useForm({ defaultValues: { ...machine } }); const output = useWatch({ control }); const { t } = useTranslation('admin'); /** * Callback triggered when the user validates the machine form: handle create or update */ const onSubmit: SubmitHandler = (data: Machine) => { MachineAPI[action](data).then(() => { onSuccess(t(`app.admin.machine_form.${action}_success`)); }).catch(error => { onError(error); }); }; return (

{t('app.admin.machine_form.attached_files_pdf')}

{t('app.admin.machine_form.ACTION_machine', { ACTION: action })} ); }; const MachineFormWrapper: React.FC = (props) => { return ( ); }; Application.Components.component('machineForm', react2angular(MachineFormWrapper, ['action', 'machine', 'onError', 'onSuccess']));