From af44c0e43493097b049dac13906ea11d771fd3f0 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Sun, 13 Mar 2022 22:43:53 +0530 Subject: [PATCH] refactor: refactored idb related operations to custom hooks --- renderer/providers/Store/index.js | 51 ++----------------- .../Store/useLoadCollectionsFromIdb.js | 20 ++++++++ .../Store/useSyncCollectionsToIdb.js | 39 ++++++++++++++ 3 files changed, 64 insertions(+), 46 deletions(-) create mode 100644 renderer/providers/Store/useLoadCollectionsFromIdb.js create mode 100644 renderer/providers/Store/useSyncCollectionsToIdb.js diff --git a/renderer/providers/Store/index.js b/renderer/providers/Store/index.js index 622816c4b..ee1d26c51 100644 --- a/renderer/providers/Store/index.js +++ b/renderer/providers/Store/index.js @@ -1,12 +1,11 @@ import React, { useState, useEffect, useContext, useReducer, createContext } from 'react'; -import map from 'lodash/map'; -import filter from 'lodash/filter'; +import { nanoid } from 'nanoid'; import reducer from './reducer'; import useIdb from './useIdb'; +import useLoadCollectionsFromIdb from './useLoadCollectionsFromIdb'; +import useSyncCollectionsToIdb from './useSyncCollectionsToIdb'; import { sendRequest } from '../../network'; -import { nanoid } from 'nanoid'; import actions from './actions'; -import {getCollectionsFromIdb, saveCollectionToIdb} from './idb'; export const StoreContext = createContext(); @@ -125,7 +124,6 @@ const initialState = { export const StoreProvider = props => { const [state, dispatch] = useReducer(reducer, initialState); - const [collectionsSyncingToIdb, setCollectionsSyncingToIdb] = useState(false); const { collections, @@ -144,48 +142,9 @@ export const StoreProvider = props => { } }, [state.requestQueuedToSend]); - useEffect(() => { - if(idbConnection) { - getCollectionsFromIdb(idbConnection) - .then((collections) => { - dispatch({ - type: actions.LOAD_COLLECTIONS_FROM_IDB, - collections: collections - }); - }) - .catch((err) => console.log(err)); - } - }, [idbConnection]); - - useEffect(() => { - if(collectionsSyncingToIdb) { - return; - } - if(collectionsToSyncToIdb && collectionsToSyncToIdb.length) { - setCollectionsSyncingToIdb(true); - const _collections = filter(collections, (c) => { - return collectionsToSyncToIdb.indexOf(c.uid) > -1; - }); - dispatch({ - type: actions.IDB_COLLECTIONS_SYNC_STARTED - }); - saveCollectionToIdb(idbConnection, _collections) - .then(() => { - setCollectionsSyncingToIdb(false); - }) - .catch((err) => { - setCollectionsSyncingToIdb(false); - dispatch({ - type: actions.IDB_COLLECTIONS_SYNC_ERROR, - collectionUids: map(collections, (c) => c.uid) - }); - console.log(err); - }); - - } - }, [collectionsToSyncToIdb]); - useIdb(dispatch); + useLoadCollectionsFromIdb(idbConnection, dispatch); + useSyncCollectionsToIdb(collectionsToSyncToIdb, collections, idbConnection, dispatch); return ; }; diff --git a/renderer/providers/Store/useLoadCollectionsFromIdb.js b/renderer/providers/Store/useLoadCollectionsFromIdb.js new file mode 100644 index 000000000..710ec7721 --- /dev/null +++ b/renderer/providers/Store/useLoadCollectionsFromIdb.js @@ -0,0 +1,20 @@ +import { useEffect } from 'react'; +import { getCollectionsFromIdb } from './idb'; +import actions from './actions'; + +const useLoadCollectionsFromIdb = (idbConnection, dispatch) => { + useEffect(() => { + if(idbConnection) { + getCollectionsFromIdb(idbConnection) + .then((collections) => { + dispatch({ + type: actions.LOAD_COLLECTIONS_FROM_IDB, + collections: collections + }); + }) + .catch((err) => console.log(err)); + } + }, [idbConnection, dispatch]); +}; + +export default useLoadCollectionsFromIdb; \ No newline at end of file diff --git a/renderer/providers/Store/useSyncCollectionsToIdb.js b/renderer/providers/Store/useSyncCollectionsToIdb.js new file mode 100644 index 000000000..484a1b0c9 --- /dev/null +++ b/renderer/providers/Store/useSyncCollectionsToIdb.js @@ -0,0 +1,39 @@ +import { useState, useEffect } from 'react'; +import map from 'lodash/map'; +import filter from 'lodash/filter'; +import actions from './actions'; +import { saveCollectionToIdb } from './idb'; + +const useSyncCollectionsToIdb = (collectionsToSyncToIdb, collections, idbConnection, dispatch) => { + const [collectionsSyncingToIdb, setCollectionsSyncingToIdb] = useState(false); + + useEffect(() => { + if(collectionsSyncingToIdb) { + return; + } + if(collectionsToSyncToIdb && collectionsToSyncToIdb.length) { + setCollectionsSyncingToIdb(true); + const _collections = filter(collections, (c) => { + return collectionsToSyncToIdb.indexOf(c.uid) > -1; + }); + dispatch({ + type: actions.IDB_COLLECTIONS_SYNC_STARTED + }); + saveCollectionToIdb(idbConnection, _collections) + .then(() => { + setCollectionsSyncingToIdb(false); + }) + .catch((err) => { + setCollectionsSyncingToIdb(false); + dispatch({ + type: actions.IDB_COLLECTIONS_SYNC_ERROR, + collectionUids: map(collections, (c) => c.uid) + }); + console.log(err); + }); + + } + }, [collectionsToSyncToIdb]); +}; + +export default useSyncCollectionsToIdb; \ No newline at end of file