Skip to content

Commit

Permalink
feat: add hooks for reading / writing userstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
shahramk committed Sep 6, 2023
1 parent 47f9a9c commit 61464d2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export { default as useFetchKpis } from './use-fetch-kpis';
export { default as useFlowLoader } from './use-flow-loader';
export { default as useFlowWriter } from './use-flow-writer';
export { default as useFetchUser } from './use-fetch-user';
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 61464d2

Please sign in to comment.