import React, { useRef, useEffect } from 'react'; import { useDispatch } from 'react-redux'; import { useFormik } from 'formik'; import * as Yup from 'yup'; import { browseDirectory } from 'providers/ReduxStore/slices/collections/actions'; import { createCollection } from 'providers/ReduxStore/slices/collections/actions'; import toast from 'react-hot-toast'; import Tooltip from 'components/Tooltip'; import Modal from 'components/Modal'; const CreateCollection = ({ onClose }) => { const inputRef = useRef(); const dispatch = useDispatch(); const formik = useFormik({ enableReinitialize: true, initialValues: { collectionName: '', collectionFolderName: '', collectionLocation: '' }, validationSchema: Yup.object({ collectionName: Yup.string() .min(1, 'must be atleast 1 characters') .max(50, 'must be 50 characters or less') .required('collection name is required'), collectionFolderName: Yup.string() .min(1, 'must be atleast 1 characters') .max(50, 'must be 50 characters or less') .required('folder name is required'), collectionLocation: Yup.string().required('location is required') }), onSubmit: (values) => { dispatch(createCollection(values.collectionName, values.collectionFolderName, values.collectionLocation)) .then(() => { toast.success('Collection created'); onClose(); }) .catch(() => toast.error('An error occured while creating the collection')); } }); const browse = () => { dispatch(browseDirectory()) .then((dirPath) => { formik.setFieldValue('collectionLocation', dirPath); }) .catch((error) => { formik.setFieldValue('collectionLocation', ''); console.error(error); }); }; useEffect(() => { if (inputRef && inputRef.current) { inputRef.current.focus(); } }, [inputRef]); const onSubmit = () => formik.handleSubmit(); return (
{formik.touched.collectionName && formik.errors.collectionName ? (
{formik.errors.collectionName}
) : null} {formik.touched.collectionFolderName && formik.errors.collectionFolderName ? (
{formik.errors.collectionFolderName}
) : null} <> {formik.touched.collectionLocation && formik.errors.collectionLocation ? (
{formik.errors.collectionLocation}
) : null}
Browse
); }; export default CreateCollection;