import React, { useRef, useEffect } from 'react'; import { useFormik } from 'formik'; import * as Yup from 'yup'; import Modal from 'components/Modal'; import actions from 'providers/Store/actions' import { useStore } from 'providers/Store'; const NewRequest = ({collectionUid, handleCancel, handleClose}) => { const [store, storeDispatch] = useStore(); const inputRef = useRef(); const formik = useFormik({ enableReinitialize: true, initialValues: { requestName: '' }, validationSchema: Yup.object({ requestName: Yup.string() .min(1, 'must be atleast 1 characters') .max(50, 'must be 50 characters or less') .required('name is required') }), onSubmit: (values) => { storeDispatch({ type: actions.SIDEBAR_COLLECTION_NEW_REQUEST, collectionUid: collectionUid, requestName: values.requestName }); handleClose(); } }); useEffect(() => { if(inputRef && inputRef.current) { inputRef.current.focus(); } }, [inputRef]); const onSubmit = () => formik.handleSubmit(); return (
{formik.touched.requestName && formik.errors.requestName ? (
{formik.errors.requestName}
) : null}
); }; export default NewRequest;