Skip to content

Commit

Permalink
fix(app-project): data-fetching for assigned workflow level
Browse files Browse the repository at this point in the history
Fix missing dependency errors in the `useAssignedLevel` hook by using `useSWR` to fetch and cache the assigned workflow. SWR also adds revalidation for cases where I might classify in the same tab over several days or weeks.
  • Loading branch information
eatyourgreens committed Aug 17, 2024
1 parent 22707c9 commit d9b87f8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/app-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"dev:inspect": "APP_ENV=${APP_ENV:-development} PANOPTES_ENV=${PANOPTES_ENV:-staging} NODE_OPTIONS='--inspect' node server/server.js",
"lint": "next lint",
"start": "NODE_ENV=${NODE_ENV:-production} PANOPTES_ENV=${PANOPTES_ENV:-production} node server/server.js",
"test": "BABEL_ENV=test mocha --config test/.mocharc.json ./.storybook/specConfig.js \"./{src,pages,stores}/**/*.spec.js\"",
"test:ci": "BABEL_ENV=test mocha --config test/.mocharc.json ./.storybook/specConfig.js --reporter=min \"./{src,pages,stores}/**/*.spec.js\"",
"test": "BABEL_ENV=test mocha --exit --config test/.mocharc.json ./.storybook/specConfig.js \"./{src,pages,stores}/**/*.spec.js\"",
"test:ci": "BABEL_ENV=test mocha --exit --config test/.mocharc.json ./.storybook/specConfig.js --reporter=min \"./{src,pages,stores}/**/*.spec.js\"",
"storybook": "storybook dev -p 9001",
"build-storybook": "storybook build"
},
Expand Down
51 changes: 26 additions & 25 deletions packages/app-project/src/hooks/useAssignedLevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,37 @@

import { useEffect, useState } from 'react'
import { panoptes } from '@zooniverse/panoptes-js'
import useSWR from 'swr'

function useAssignedLevel(assignedWorkflowID) {
const [assignedWorkflowLevel, setAssignedWorkflowLevel] = useState(1)
const SWRoptions = {
revalidateIfStale: true,
revalidateOnMount: true,
revalidateOnFocus: true,
revalidateOnReconnect: true,
refreshInterval: 0
}

async function checkAssignedLevel() {
const query = {
fields: 'configuration',
id: assignedWorkflowID
}
try {
const response = await panoptes.get('/workflows', query)
if (response.ok) {
const fetchedWorkflow = response.body.workflows?.[0]
setAssignedWorkflowLevel(parseInt(fetchedWorkflow?.configuration?.level), 10)
}
} catch (error) {
throw error
}
async function fetchAssignedWorkflow({
fields = 'configuration',
assignedWorkflowID
}) {
const query = {
fields,
id: assignedWorkflowID
}
const response = await panoptes.get('/workflows', query)
if (response.ok) {
const fetchedWorkflow = response.body.workflows?.[0]
return parseInt(fetchedWorkflow?.configuration?.level, 10)
}
return 1
}

useEffect(
function () {
if (assignedWorkflowID) {
checkAssignedLevel()
}
},
[assignedWorkflowID]
)
function useAssignedLevel(assignedWorkflowID) {
const key = assignedWorkflowID ? { assignedWorkflowID } : null
const { data: assignedWorkflowLevel } = useSWR(key, fetchAssignedWorkflow, SWRoptions)

return assignedWorkflowLevel
return assignedWorkflowLevel || 1
}

export default useAssignedLevel

0 comments on commit d9b87f8

Please sign in to comment.