import * as React from 'react'; import { useEffect, useState } 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'; import { AdvancedAccountingForm } from '../accounting/advanced-accounting-form'; import { FormSelect } from '../form/form-select'; import { SelectOption } from '../../models/select'; import MachineCategoryAPI from '../../api/machine-category'; import SettingAPI from '../../api/setting'; import { MachineCategory } from '../../models/machine-category'; import { FabAlert } from '../base/fab-alert'; 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'); const [machineCategories, setMachineCategories] = useState>([]); const [isActiveAccounting, setIsActiveAccounting] = useState(false); // retrieve the full list of machine categories on component mount // check advanced accounting activation useEffect(() => { MachineCategoryAPI.index() .then(data => setMachineCategories(data)) .catch(e => onError(e)); SettingAPI.get('advanced_accounting').then(res => setIsActiveAccounting(res.value === 'true')).catch(onError); }, []); /** * Callback triggered when the user validates the machine form: handle create or update */ const onSubmit: SubmitHandler = (data: Machine) => { MachineAPI[action](data).then((res) => { onSuccess(t(`app.admin.machine_form.${action}_success`)); window.location.href = `/#!/machines/${res.slug}`; }).catch(error => { onError(error); }); }; /** * Callack triggered when the user changes the 'reservable' status of the machine: * A reservable machine cannot be disabled */ const onReservableToggled = (reservable: boolean) => { if (reservable) { setValue('disabled', false); } }; /** * Callack triggered when the user changes the 'disabled' status of the machine: * A disabled machine cannot be reservable */ const onDisabledToggled = (disabled: boolean) => { if (disabled) { setValue('reservable', false); } }; /** * Convert all machine categories to the select format */ const buildOptions = (): Array> => { return machineCategories.map(t => { return { value: t.id, label: t.name }; }); }; return (

{t('app.admin.machine_form.ACTION_title', { ACTION: action })}

{t('app.admin.machine_form.save')}
{action === 'create' && {t('app.admin.machine_form.watch_out_when_creating_a_new_machine_its_prices_are_initialized_at_0_for_all_subscriptions')} {t('app.admin.machine_form.consider_changing_them_before_creating_any_reservation_slot')} }

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

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

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

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

{isActiveAccounting &&
}
); }; const MachineFormWrapper: React.FC = (props) => { return ( ); }; Application.Components.component('machineForm', react2angular(MachineFormWrapper, ['action', 'machine', 'onError', 'onSuccess']));