Skip to content

Commit

Permalink
refactor: move user storage functions to their own js files
Browse files Browse the repository at this point in the history
  • Loading branch information
shahramk committed Jul 26, 2023
1 parent 47bf150 commit abac8ac
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 38 deletions.
40 changes: 4 additions & 36 deletions nerdlets/home/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Check failure on line 20 in nerdlets/home/index.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

nerdlets/home/index.js#L20

[prettier/prettier] Replace `·useFlowLoader,·useFetchUserConfig,·useUpdateUserConfig·` with `⏎··useFlowLoader,⏎··useFetchUserConfig,⏎··useUpdateUserConfig,⏎`
import { MODES, NERD_STORAGE } from '../../src/constants';
import { uuid } from '../../src/utils';

Expand All @@ -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] );

Check failure on line 32 in nerdlets/home/index.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

nerdlets/home/index.js#L32

[prettier/prettier] Replace `·()·=>·setUserConfig(userStorageConfig·||·{}),·[userStorageConfig]·` with `()·=>·setUserConfig(userStorageConfig·||·{}),·[userStorageConfig]`
const { userStorageHandler } = useUpdateUserConfig();

const {
flows: flowsData,
Expand Down
4 changes: 2 additions & 2 deletions src/constants/nerd-storage.js
Original file line number Diff line number Diff line change
@@ -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',
};
2 changes: 2 additions & 0 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
22 changes: 22 additions & 0 deletions src/hooks/use-fetch-user-config/index.js
Original file line number Diff line number Diff line change
@@ -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;
31 changes: 31 additions & 0 deletions src/hooks/use-update-user-config/index.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit abac8ac

Please sign in to comment.