import React, { useEffect } from 'react'; import { useForm, SubmitHandler } from 'react-hook-form'; import { Button, FormControl, FormLabel, Input, Drawer, DrawerBody, DrawerCloseButton, DrawerContent, DrawerFooter, DrawerHeader, DrawerOverlay, useToast, InputGroup, InputLeftElement, FormErrorMessage, } from '@chakra-ui/react'; import { useTranslation } from 'react-i18next'; import { PhoneIcon } from '@chakra-ui/icons'; import { api } from '../../__data__/service/api'; import showToast from '../../helpers/showToast'; import { DrawerInputs } from '../../models/arm/form'; interface MasterDrawerProps { isOpen: boolean; onClose: () => void; } const MasterDrawer = ({ isOpen, onClose }: MasterDrawerProps) => { const { register, handleSubmit, reset, formState: { errors }, } = useForm(); const { t } = useTranslation('~', { keyPrefix: 'dry-wash.arm.master.drawer', }); const onSubmit: SubmitHandler = async (data) => { const trimMaster = { name: data.name.trim(), phone: data.phone.trim(), }; const isEmptyFields = trimMaster.name === '' || trimMaster.phone === ''; if (isEmptyFields) { showToast({ toast, title: t('toast.error.base'), description: t('toast.error.empty-fields'), status: 'error', }); return; } await addMaster(trimMaster); }; const [addMaster, { error, isSuccess }] = api.useAddMasterMutation(); const toast = useToast(); useEffect(() => { if (isSuccess) { showToast({ toast, title: t('toast.create-master'), status: 'success', }); reset(); onClose(); } }, [isSuccess]); useEffect(() => { if (error) { showToast({ toast, title: t('toast.error.create-master'), description: t('toast.error.create-master-details'), status: 'error', }); console.error(error); } }, [error]); return (
{t('title')} {t('inputName.label')} {errors.name && errors.name.message} {t('inputPhone.label')} value.replace(/[^\d+]/g, ''), })} placeholder={t('inputPhone.placeholder')} /> {errors.phone && errors.phone.message}
); }; export default MasterDrawer;