import BreweryPostQueryResult from '@/services/BreweryPost/types/BreweryPostQueryResult'; import { FunctionComponent } from 'react'; import { SubmitHandler, useForm } from 'react-hook-form'; // import { useNavigate } from 'react-router-dom'; // import createBeerPost from '../api/beerPostRoutes/createBeerPost'; // import getAllBreweryPosts from '../api/breweryPostRoutes/getAllBreweryPosts'; // import BreweryPostI from '../types/BreweryPostI'; // import isValidUuid from '../util/isValidUuid'; import Button from './ui/Button'; import FormError from './ui/forms/FormError'; import FormInfo from './ui/forms/FormInfo'; import FormLabel from './ui/forms/FormLabel'; import FormSegment from './ui/forms/FormSegment'; import FormSelect from './ui/forms/FormSelect'; import FormTextArea from './ui/forms/FormTextArea'; import FormTextInput from './ui/forms/FormTextInput'; interface IFormInput { name: string; description: string; type: string; abv: number; ibu: number; breweryId: string; } interface BeerFormProps { type: 'edit' | 'create'; // eslint-disable-next-line react/require-default-props defaultValues?: IFormInput; breweries?: BreweryPostQueryResult[]; } const BeerForm: FunctionComponent = ({ type, defaultValues, breweries = [], }) => { const { register, handleSubmit, formState: { errors }, } = useForm({ defaultValues: { name: defaultValues?.name, description: defaultValues?.description, abv: defaultValues?.abv, ibu: defaultValues?.ibu, }, }); // const navigate = useNavigate(); const nameValidationSchema = register('name', { required: 'Beer name is required.', }); const breweryValidationSchema = register('breweryId', { required: 'Brewery name is required.', }); const abvValidationSchema = register('abv', { required: 'ABV is required.', valueAsNumber: true, max: { value: 50, message: 'ABV must be less than 50%.' }, min: { value: 0.1, message: 'ABV must be greater than 0.1%', }, validate: (abv) => !Number.isNaN(abv) || 'ABV is invalid.', }); const ibuValidationSchema = register('ibu', { required: 'IBU is required.', min: { value: 2, message: 'IBU must be greater than 2.', }, valueAsNumber: true, validate: (ibu) => !Number.isNaN(ibu) || 'IBU is invalid.', }); const onSubmit: SubmitHandler = (data) => { console.log(data); }; return (
Name {errors.name?.message} {type === 'create' && breweries.length && ( <> Brewery {errors.breweryId?.message} ({ value: brewery.id, text: brewery.name, }))} placeholder="Brewery" message="Pick a brewery" /> )}
ABV {errors.abv?.message}
IBU {errors.ibu?.message}
Description {errors.description?.message} Type {errors.type?.message}
); }; export default BeerForm;