import React, { useRef, useEffect } from 'react'; import { useDispatch } from 'react-redux'; import { useFormik } from 'formik'; import * as Yup from 'yup'; import { browserLocalDirectory } from 'providers/ReduxStore/slices/collections/actions'; import { isElectron } from 'utils/common/platform'; import { createCollection, createLocalCollection } from 'providers/ReduxStore/slices/collections/actions'; import toast from 'react-hot-toast'; import Modal from 'components/Modal'; const CreateCollection = ({onClose, isLocal}) => { const inputRef = useRef(); const dispatch = useDispatch(); const isPlatformElectron = isElectron(); const formik = useFormik({ enableReinitialize: true, initialValues: { collectionName: '', collectionLocation: '' }, validationSchema: Yup.object({ collectionName: Yup.string() .min(1, 'must be atleast 1 characters') .max(50, 'must be 50 characters or less') .required('name is required') }), onSubmit: (values) => { const action = isLocal && isPlatformElectron ? createLocalCollection : createCollection; dispatch(action(values.collectionName, values.collectionLocation)) .then(() => { toast.success("Collection created"); onClose(); }) .catch(() => toast.error("An error occured while creating the collection")); } }); const browse = () => { dispatch(browserLocalDirectory()) .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} {isLocal && isPlatformElectron ? ( <> ) : null} {isLocal && isPlatformElectron && formik.touched.collectionLocation && formik.errors.collectionLocation ? (
{formik.errors.collectionLocation}
) : null} {isLocal && isPlatformElectron ? (
Browse
) : null }
); }; export default CreateCollection;