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 Modal from 'components/Modal'; import { sanitizeName, validateName, validateNameError } from 'utils/common/regex'; import PathDisplay from 'components/PathDisplay/index'; import { useState } from 'react'; import { IconArrowBackUp, IconEdit } from '@tabler/icons'; import Help from 'components/Help'; import { multiLineMsg } from "utils/common"; import { formatIpcError } from "utils/common/error"; const CreateCollection = ({ onClose }) => { const inputRef = useRef(); const dispatch = useDispatch(); const [isEditing, toggleEditing] = useState(false); const formik = useFormik({ enableReinitialize: true, initialValues: { collectionName: '', collectionFolderName: '', collectionLocation: '' }, validationSchema: Yup.object({ collectionName: Yup.string() .min(1, 'must be at least 1 character') .max(255, 'must be 255 characters or less') .required('collection name is required'), collectionFolderName: Yup.string() .min(1, 'must be at least 1 character') .max(255, 'must be 255 characters or less') .test('is-valid-collection-name', function(value) { const isValid = validateName(value); return isValid ? true : this.createError({ message: validateNameError(value) }); }) .required('folder name is required'), collectionLocation: Yup.string().min(1, 'location is required').required('location is required') }), onSubmit: (values) => { dispatch(createCollection(values.collectionName, values.collectionFolderName, values.collectionLocation)) .then(() => { toast.success('Collection created!'); onClose(); }) .catch((e) => toast.error(multiLineMsg('An error occurred while creating the collection', formatIpcError(e)))); } }); const browse = () => { dispatch(browseDirectory()) .then((dirPath) => { // When the user closes the dialog without selecting anything dirPath will be false if (typeof dirPath === 'string') { 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 (
e.preventDefault()}>
{ formik.handleChange(e); !isEditing && formik.setFieldValue('collectionFolderName', sanitizeName(e.target.value)); }} autoComplete="off" autoCorrect="off" autoCapitalize="off" spellCheck="false" value={formik.values.collectionName || ''} /> {formik.touched.collectionName && formik.errors.collectionName ? (
{formik.errors.collectionName}
) : null} { formik.setFieldValue('collectionLocation', e.target.value); }} /> {formik.touched.collectionLocation && formik.errors.collectionLocation ? (
{formik.errors.collectionLocation}
) : null}
Browse
{formik.values.collectionName?.trim()?.length > 0 && (
{isEditing ? ( toggleEditing(false)} /> ) : ( toggleEditing(true)} /> )}
{isEditing ? ( ) : (
)} {formik.touched.collectionFolderName && formik.errors.collectionFolderName ? (
{formik.errors.collectionFolderName}
) : null}
)}
); }; export default CreateCollection;