import * as React from 'react'; import { useTranslation } from 'react-i18next'; import { FabAlert } from '../base/fab-alert'; import { FabModal, ModalSize } from '../base/fab-modal'; import { useForm, useWatch } from 'react-hook-form'; import { FormSelect } from '../form/form-select'; import { FormInput } from '../form/form-input'; import { LimitableType, PlanLimitation } from '../../models/plan'; import { Machine } from '../../models/machine'; import { MachineCategory } from '../../models/machine-category'; import { SelectOption } from '../../models/select'; import { FabButton } from '../base/fab-button'; import { useEffect } from 'react'; interface PlanLimitModalProps { isOpen: boolean, toggleModal: () => void, onSuccess: (limit: PlanLimitation) => void, machines: Array categories: Array, limitation?: PlanLimitation, existingLimitations: Array; } /** * Form to manage subscriptions limitations of use */ export const PlanLimitModal: React.FC = ({ isOpen, toggleModal, machines, categories, onSuccess, limitation, existingLimitations = [] }) => { const { t } = useTranslation('admin'); const { register, control, formState, setValue, handleSubmit, reset } = useForm({ defaultValues: limitation || { limitable_type: 'MachineCategory' } }); const limitType = useWatch({ control, name: 'limitable_type' }); useEffect(() => { reset(limitation); }, [limitation]); /** * Toggle the form between 'categories' and 'machine' */ const toggleLimitType = (evt: React.MouseEvent, type: LimitableType) => { evt.preventDefault(); setValue('limitable_type', type); setValue('limitable_id', null); }; /** * Callback triggered when the user validates the new limit. * We do not use handleSubmit() directly to prevent the propagaion of the "submit" event to the parent form */ const onSubmit = (event: React.FormEvent) => { if (event) { event.stopPropagation(); event.preventDefault(); } return handleSubmit((data: PlanLimitation) => { onSuccess({ ...data, _modified: true }); reset({ limitable_type: 'MachineCategory', limitable_id: null, limit: null }); toggleModal(); })(event); }; /** * Creates options to the react-select format */ const buildOptions = (): Array> => { if (limitType === 'MachineCategory') { return categories .filter(c => limitation || !existingLimitations.filter(l => l.limitable_type === 'MachineCategory').map(l => l.limitable_id).includes(c.id)) .map(cat => { return { value: cat.id, label: cat.name }; }); } else { return machines .filter(m => limitation || !existingLimitations.filter(l => l.limitable_type === 'Machine').map(l => l.limitable_id).includes(m.id)) .map(machine => { return { value: machine.id, label: machine.name }; }); } }; return ( reset({ limitable_type: 'MachineCategory' })} closeButton>

{t('app.admin.plan_limit_modal.limit_reservations')}

{limitType === 'Machine' ? t('app.admin.plan_limit_modal.machine_info') : t('app.admin.plan_limit_modal.categories_info')} {t('app.admin.plan_limit_modal.confirm')}
); };