-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PHC-4151 Update useActiveProject to read/write to async storage #68
Conversation
Pull Request Test Coverage Report for Build 4368773861
💛 - Coveralls |
6845898
to
32d8a2c
Compare
32d8a2c
to
60b829b
Compare
src/hooks/useActiveProject.tsx
Outdated
@@ -50,35 +59,54 @@ export const ActiveProjectContextProvider = ({ | |||
return; | |||
} | |||
|
|||
// TODO: Save for previously-selected project | |||
storeProjectId(selectedProject.id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to await
this, I'd think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may want to catch any errors here but I'm not sure why we would need to wait for this to complete before proceeding. At this point, we've already set the activeAccountId in state so we really only need to store this for the next session. This goes to the below explanation about why I wrapped the get async storage with react-query
, mostly to keep the effect cleaner without having to convert it to be async.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But in this case, the callback is already async
. And agreed- if you still feel we shouldn't wait/verify it's stored before moving on, we need to (whether in this PR or down the road) log the error or something so it doesn't just go unnoticed.
c4217cf
to
40ca665
Compare
src/hooks/useActiveProject.tsx
Outdated
if (!selectedProject || !selectedSubject) { | ||
console.warn('Ignoring attempt to set invalid projectId', projectId); | ||
|
||
// Attempt to fallback to first available project | ||
const defaultProject = projects?.[0]; | ||
const defaultSubject = subjects?.find( | ||
(s) => s.projectId === defaultProject?.id, | ||
); | ||
if (!defaultProject || !defaultSubject) { | ||
console.warn('Failed attempt to select default projectId', projectId); | ||
return { selectedProject: undefined, selectedSubject: undefined }; | ||
} | ||
return { selectedProject: defaultProject, selectedSubject: defaultSubject }; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
General question about how we want to use console.warn
in this project. Are these warnings for us, or for the end consumer/developer who might be building with this SDK in their app? Obviously these are only shown in development mode. Also, isn't it a normal, supported case that a user may not have any projects, and thus a console warning might be noise?
The PR looks good though, not wanting to hold it up with my questions. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cleaned this up a bit to not warn so often, should only warn if we explicitly set an invalid project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should add something like [LifeOmicSDK]
prefixes to logs and errors so that user's can quickly identify where the error is coming from?
src/hooks/useActiveProject.tsx
Outdated
@@ -36,40 +65,63 @@ export const ActiveProjectContextProvider = ({ | |||
const [hookReturnValue, setHookReturnValue] = useState<ActiveProjectProps>( | |||
{}, | |||
); | |||
const { useGetItem, setItem } = useAsyncStorage(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this hook return a hook? why cant we do something like
const [storedProjectIdResult, setStoredProjectId] = useAsyncStorage(selectedProjectIdKey);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can and we should, I like that even better.
src/hooks/useActiveProject.test.tsx
Outdated
useAsyncStorageSpy = jest.spyOn(useAsyncStorage, 'useAsyncStorage'); | ||
}); | ||
|
||
test('initial render writes selected account to async storage', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this test title correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opps, nope, copy/paste error.
f24c645
to
951fea3
Compare
AsyncStorage.setItem(key, value); | ||
const setItem = (value: string) => | ||
AsyncStorage.setItem(key, value).catch((error) => | ||
console.error(`[LifeOmicSDK]:${error}`), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in a future PR I will purpose a wrapper or something around console to include that prefix.
activeSubjectId: mockMe[1].subjectId, | ||
}); | ||
|
||
useAsyncStorageSpy = jest.spyOn(useAsyncStorage, 'useAsyncStorage'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be better to move this to beforeEach
incase this test fails.
🎉 This PR is included in version 1.1.15 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Changes
Screenshots