-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor useStore and useWorkflowSnapshot
- Add a `hooks` directory to the `Classifier` component. Import the `useStore` and `useWorkflowSnapshot` hooks from that directory. - Explicitly declare `SWR` options in the the `useWorkflowSnapshot` hook.
- Loading branch information
1 parent
0796c1e
commit 65121a0
Showing
4 changed files
with
54 additions
and
39 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
2 changes: 2 additions & 0 deletions
2
packages/lib-classifier/src/components/Classifier/hooks/index.js
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,2 @@ | ||
export { default as useStore } from './useStore' | ||
export { default as useWorkflowSnapshot } from './useWorkflowSnapshot' |
25 changes: 25 additions & 0 deletions
25
packages/lib-classifier/src/components/Classifier/hooks/useStore.js
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,25 @@ | ||
import makeInspectable from 'mobx-devtools-mst' | ||
import { useMemo } from 'react' | ||
|
||
import RootStore from '@store' | ||
|
||
let store | ||
|
||
function initStore({ authClient, client, initialState }) { | ||
if (!store) { | ||
store = RootStore.create(initialState, { | ||
authClient, | ||
client | ||
}) | ||
makeInspectable(store) | ||
} | ||
return store | ||
} | ||
/** | ||
useStore hook adapted from | ||
https://github.com/vercel/next.js/blob/5201cdbaeaa72b54badc8f929ddc73c09f414dc4/examples/with-mobx-state-tree/store.js#L49-L52 | ||
*/ | ||
export default function useStore({ authClient, client, initialState }) { | ||
const _store = useMemo(() => initStore({ authClient, client, initialState }), [authClient, initialState]) | ||
return _store | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/lib-classifier/src/components/Classifier/hooks/useWorkflowSnapshot.js
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,25 @@ | ||
import useSWR from 'swr' | ||
|
||
import { panoptes } from '@zooniverse/panoptes-js' | ||
|
||
const SWRoptions = { | ||
revalidateIfStale: true, | ||
revalidateOnMount: true, | ||
revalidateOnFocus: true, | ||
revalidateOnReconnect: true, | ||
refreshInterval: 0 | ||
} | ||
|
||
async function fetchWorkflow(workflowID) { | ||
if (workflowID) { | ||
const { body } = await panoptes.get(`/workflows/${workflowID}`) | ||
const [ workflowSnapshot ] = body.workflows | ||
return workflowSnapshot | ||
} | ||
return null | ||
} | ||
|
||
export default function useWorkflowSnapshot(workflowID) { | ||
const { data } = useSWR(workflowID, fetchWorkflow, SWRoptions) | ||
return data ?? null | ||
} |