-
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.
Classifier: refactor UPP data-fetching with hooks
Remove data-fetching from the classifier's UPP store and move it into a `useProjectPreferences` hook instead. Preferences should be fetched when the logged-in user changes. UPP updates are still handled by the store, so the store is updated whenever fresh preferences are received from Panoptes.
- Loading branch information
1 parent
9d9cb4a
commit 39781f6
Showing
10 changed files
with
96 additions
and
269 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
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
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,6 +1,7 @@ | ||
export { default as useHydratedStore } from './useHydratedStore' | ||
export { default as usePanoptesAuth } from './usePanoptesAuth' | ||
export { default as usePanoptesUser } from './usePanoptesUser' | ||
export { default as useProjectPreferences } from './useProjectPreferences' | ||
export { default as useProjectRoles } from './useProjectRoles' | ||
export { default as useStores } from './useStores' | ||
export { default as useWorkflowSnapshot } from './useWorkflowSnapshot' |
53 changes: 53 additions & 0 deletions
53
packages/lib-classifier/src/hooks/useProjectPreferences.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,53 @@ | ||
import useSWR from 'swr' | ||
import { panoptes } from '@zooniverse/panoptes-js' | ||
|
||
import { usePanoptesAuth } from './' | ||
|
||
const SWRoptions = { | ||
revalidateIfStale: true, | ||
revalidateOnMount: true, | ||
revalidateOnFocus: true, | ||
revalidateOnReconnect: true, | ||
refreshInterval: 0 | ||
} | ||
|
||
async function createProjectPreferences({ endpoint, project_id, authorization }) { | ||
const data = { | ||
links: { project: project_id }, | ||
preferences: {} | ||
} | ||
const { body } = await panoptes.post(endpoint, { project_preferences: data }, { authorization }) | ||
const [projectPreferences] = body.project_preferences | ||
return projectPreferences | ||
} | ||
|
||
async function fetchProjectPreferences({ endpoint, project_id, user_id, authorization }) { | ||
const { body } = await panoptes.get(endpoint, { project_id, user_id }, { authorization }) | ||
const [projectPreferences] = body.project_preferences | ||
return projectPreferences | ||
} | ||
|
||
async function fetchOrCreateProjectPreferences({ endpoint, project_id, user_id, authorization }) { | ||
// auth is undefined while loading | ||
if (authorization === undefined) { | ||
return undefined | ||
} | ||
// logged-in | ||
if (authorization) { | ||
const projectPreferences = await fetchProjectPreferences({ endpoint, project_id, user_id, authorization }) | ||
if (projectPreferences) { | ||
return projectPreferences | ||
} else { | ||
return await createProjectPreferences({ endpoint, project_id, authorization }) | ||
} | ||
} | ||
// not logged-in | ||
return null | ||
} | ||
|
||
export default function useProjectPreferences(project_id, user_id) { | ||
const authorization = usePanoptesAuth(user_id) | ||
const endpoint = '/project_preferences' | ||
const { data } = useSWR({ endpoint, project_id, user_id, authorization }, fetchOrCreateProjectPreferences, SWRoptions) | ||
return data | ||
} |
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
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
Oops, something went wrong.