From abac8ac03cd6f4392e4cbb14cfac230e226ea710 Mon Sep 17 00:00:00 2001 From: shahram Date: Wed, 26 Jul 2023 15:14:16 -0700 Subject: [PATCH] refactor: move user storage functions to their own js files --- nerdlets/home/index.js | 40 +++-------------------- src/constants/nerd-storage.js | 4 +-- src/hooks/index.js | 2 ++ src/hooks/use-fetch-user-config/index.js | 22 +++++++++++++ src/hooks/use-update-user-config/index.js | 31 ++++++++++++++++++ 5 files changed, 61 insertions(+), 38 deletions(-) create mode 100644 src/hooks/use-fetch-user-config/index.js create mode 100644 src/hooks/use-update-user-config/index.js diff --git a/nerdlets/home/index.js b/nerdlets/home/index.js index 3338df3f..91590cb2 100644 --- a/nerdlets/home/index.js +++ b/nerdlets/home/index.js @@ -14,12 +14,10 @@ import { PlatformStateContext, Spinner, useAccountStorageMutation, - useUserStorageQuery, - useUserStorageMutation, } from 'nr1'; import { Flow, FlowList, NoFlows } from '../../src/components'; -import { useFlowLoader } from '../../src/hooks'; +import { useFlowLoader, useFetchUserConfig, useUpdateUserConfig } from '../../src/hooks'; import { MODES, NERD_STORAGE } from '../../src/constants'; import { uuid } from '../../src/utils'; @@ -29,40 +27,10 @@ const HomeNerdlet = () => { const [currentFlowIndex, setCurrentFlowIndex] = useState(-1); const { accountId } = useContext(PlatformStateContext); - const [hhUserConfig, { error: userStorageWriteError }] = - useUserStorageMutation({ - actionType: useUserStorageMutation.ACTION_TYPE.WRITE_DOCUMENT, - collection: NERD_STORAGE.USER_COLLECTION, - }); - useEffect( - () => console.error('User storage write error: ', userStorageWriteError), - [userStorageWriteError] - ); - const userStorageHandler = useCallback((updatedConfig) => { - hhUserConfig({ - documentId: NERD_STORAGE.USER_DOCUMENT_ID, - document: { - id: NERD_STORAGE.USER_DOCUMENT_ID, - ...userConfig, - ...updatedConfig, - }, - }); - }, []); - const [userConfig, setUserConfig] = useState({}); - const { data: userStorageOptions, error: userStorageError } = - useUserStorageQuery({ - collection: NERD_STORAGE.USER_COLLECTION, - documentId: NERD_STORAGE.USER_DOCUMENT_ID, - }); - useEffect( - () => setUserConfig(userStorageOptions || {}), - [userStorageOptions] - ); - useEffect( - () => console.error('User storage error: ', userStorageError), - [userStorageError] - ); + const { userStorageConfig } = useFetchUserConfig(); + useEffect( () => setUserConfig(userStorageConfig || {}), [userStorageConfig] ); + const { userStorageHandler } = useUpdateUserConfig(); const { flows: flowsData, diff --git a/src/constants/nerd-storage.js b/src/constants/nerd-storage.js index 6c70b2e3..60c1313a 100644 --- a/src/constants/nerd-storage.js +++ b/src/constants/nerd-storage.js @@ -1,5 +1,5 @@ export const NERD_STORAGE = { FLOWS_COLLECTION: 'flows', - USER_COLLECTION: 'hedgehog', - USER_DOCUMENT_ID: 'config', + USER_COLLECTION: 'user', + CONFIG_DOCUMENT_ID: 'config', }; diff --git a/src/hooks/index.js b/src/hooks/index.js index 52a4e67c..706d0f77 100644 --- a/src/hooks/index.js +++ b/src/hooks/index.js @@ -2,3 +2,5 @@ export { default as useServiceLevelsSearch } from './use-service-levels-search'; export { default as useFetchServiceLevels } from './use-fetch-service-levels'; export { default as useFetchKpis } from './use-fetch-kpis'; export { default as useFlowLoader } from './use-flow-loader'; +export { default as useFetchUserConfig } from './use-fetch-user-config'; +export { default as useUpdateUserConfig } from './use-update-user-config'; diff --git a/src/hooks/use-fetch-user-config/index.js b/src/hooks/use-fetch-user-config/index.js new file mode 100644 index 00000000..b0b66f24 --- /dev/null +++ b/src/hooks/use-fetch-user-config/index.js @@ -0,0 +1,22 @@ +import { useEffect } from 'react'; +import { useUserStorageQuery } from 'nr1'; +import { NERD_STORAGE } from '../../constants'; + +const useFetchUserConfig = () => { + const { data: userStorageConfig, error: userStorageError } = + useUserStorageQuery({ + collection: NERD_STORAGE.USER_COLLECTION, + documentId: NERD_STORAGE.CONFIG_DOCUMENT_ID, + }); + + useEffect( + () => + userStorageError && + console.error('User storage read error: ', userStorageError), + [userStorageError] + ); + + return { userStorageConfig }; +}; + +export default useFetchUserConfig; diff --git a/src/hooks/use-update-user-config/index.js b/src/hooks/use-update-user-config/index.js new file mode 100644 index 00000000..6e8cc14b --- /dev/null +++ b/src/hooks/use-update-user-config/index.js @@ -0,0 +1,31 @@ +import { useCallback, useEffect } from 'react'; +import { useUserStorageMutation } from 'nr1'; +import { NERD_STORAGE } from '../../constants'; + +const useUpdateUserConfig = () => { + const [hhUserConfig, { error: userStorageWriteError }] = + useUserStorageMutation({ + actionType: useUserStorageMutation.ACTION_TYPE.WRITE_DOCUMENT, + collection: NERD_STORAGE.USER_COLLECTION, + }); + + useEffect( + () => + userStorageWriteError && + console.error('User storage write error: ', userStorageWriteError), + [userStorageWriteError] + ); + + const userStorageHandler = useCallback((updatedUserConfig) => { + hhUserConfig({ + documentId: NERD_STORAGE.CONFIG_DOCUMENT_ID, + document: { + ...updatedUserConfig, + }, + }); + }, []); + + return { userStorageHandler }; +}; + +export default useUpdateUserConfig;