From 2f594835d800515f4385ffef97cd122db8a07822 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Sun, 13 Mar 2022 19:59:10 +0530 Subject: [PATCH] feat: AddFolder Modal --- .../components/Sidebar/AddFolder/index.js | 67 +++++++++++++++++++ .../Sidebar/Collections/Collection/index.js | 21 +++--- renderer/components/Sidebar/TitleBar/index.js | 1 + renderer/providers/Store/reducer.js | 2 +- renderer/providers/Store/useIdb.js | 3 +- 5 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 renderer/components/Sidebar/AddFolder/index.js diff --git a/renderer/components/Sidebar/AddFolder/index.js b/renderer/components/Sidebar/AddFolder/index.js new file mode 100644 index 000000000..f50038f0f --- /dev/null +++ b/renderer/components/Sidebar/AddFolder/index.js @@ -0,0 +1,67 @@ +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 AddFolder = ({collectionUid, handleCancel, handleClose}) => { + const [store, storeDispatch] = useStore(); + const inputRef = useRef(); + const formik = useFormik({ + enableReinitialize: true, + initialValues: { + folderName: '' + }, + validationSchema: Yup.object({ + folderName: 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_ADD_FOLDER, + collectionUid: collectionUid, + folderName: values.folderName + }); + handleClose(); + } + }); + + useEffect(() => { + if(inputRef && inputRef.current) { + inputRef.current.focus(); + } + }, [inputRef]); + + const onSubmit = () => formik.handleSubmit(); + + return ( + +
+
+ + + {formik.touched.folderName && formik.errors.folderName ? ( +
{formik.errors.folderName}
+ ) : null} +
+
+
+ ); +}; + +export default AddFolder; diff --git a/renderer/components/Sidebar/Collections/Collection/index.js b/renderer/components/Sidebar/Collections/Collection/index.js index 548b08887..edff7c592 100644 --- a/renderer/components/Sidebar/Collections/Collection/index.js +++ b/renderer/components/Sidebar/Collections/Collection/index.js @@ -1,15 +1,17 @@ -import React, { forwardRef, useRef } from 'react'; +import React, { useState, forwardRef, useRef } from 'react'; import get from 'lodash/get'; import classnames from 'classnames'; import { IconChevronRight, IconDots } from '@tabler/icons'; import Dropdown from 'components/Dropdown'; import actions from 'providers/Store/actions' import { useStore } from 'providers/Store'; +import AddFolder from 'components/Sidebar/AddFolder'; import CollectionItem from './CollectionItem'; import StyledWrapper from './StyledWrapper'; const Collection = ({collection}) => { + const [showAddFolderModal, setShowAddFolderModal] = useState(false); const [store, storeDispatch] = useStore(); const { @@ -42,17 +44,18 @@ const Collection = ({collection}) => { }); }; - const addFolder = () => { - storeDispatch({ - type: actions.SIDEBAR_COLLECTION_ADD_FOLDER, - collectionUid: collection.uid - }); - }; - + const hideAddFolderModal = () => setShowAddFolderModal(false); const collectionItems = get(collection, 'current.items'); return ( + {showAddFolderModal && ( + + )}
{collection.current.name} @@ -68,7 +71,7 @@ const Collection = ({collection}) => {
{ menuDropdownTippyRef.current.hide(); - addFolder(); + setShowAddFolderModal(true) }}> Add Folder
diff --git a/renderer/components/Sidebar/TitleBar/index.js b/renderer/components/Sidebar/TitleBar/index.js index e6748b73a..43a4d4a2a 100644 --- a/renderer/components/Sidebar/TitleBar/index.js +++ b/renderer/components/Sidebar/TitleBar/index.js @@ -59,6 +59,7 @@ const TitleBar = ({dispatch, actions}) => { saveCollectionToIdb(store.idbConnection, newCollection) .then(() => console.log('Collection created')) .catch((err) => { + console.log(err); setShowToast({ show: true, type: 'error', diff --git a/renderer/providers/Store/reducer.js b/renderer/providers/Store/reducer.js index 75b5ef39b..60722f727 100644 --- a/renderer/providers/Store/reducer.js +++ b/renderer/providers/Store/reducer.js @@ -76,7 +76,7 @@ const reducer = (state, action) => { if(collection) { collection.current.items.push({ "uid": nanoid(), - "name": "New Folder", + "name": action.folderName, "depth": 1, "items": [] }); diff --git a/renderer/providers/Store/useIdb.js b/renderer/providers/Store/useIdb.js index 951c3250d..7ba130eff 100644 --- a/renderer/providers/Store/useIdb.js +++ b/renderer/providers/Store/useIdb.js @@ -20,7 +20,8 @@ const useIdb = (dispatch) => { type: actions.IDB_CONNECTION_READY, connection: connection }); - }); + }) + .catch((err) => console.log(err)); }, []); };