-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move user storage functions to their own js files
- Loading branch information
Showing
5 changed files
with
61 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |