Skip to content

Commit

Permalink
avoid returning NaN for the workflow level
Browse files Browse the repository at this point in the history
  • Loading branch information
eatyourgreens committed Aug 23, 2024
1 parent 6464b95 commit 56a2c14
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
21 changes: 9 additions & 12 deletions packages/app-project/src/hooks/useAssignedLevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,28 @@ const SWRoptions = {
async function fetchAssignedWorkflow({
fields = 'configuration',
assignedWorkflowID,
workflows = []
}) {
const existingWorkflow = workflows.find(workflow => workflow.id === assignedWorkflowID)
if (existingWorkflow) {
return parseInt(existingWorkflow.configuration?.level, 10)
}
const query = {
fields,
id: assignedWorkflowID
}
const response = await panoptes.get('/workflows', query)
if (response.ok) {
const fetchedWorkflow = response.body.workflows?.[0]
if (response.ok && response.body.workflows?.length) {
const fetchedWorkflow = response.body.workflows[0]
return parseInt(fetchedWorkflow?.configuration?.level, 10)
}
return 1
}

function useAssignedLevel(assignedWorkflowID, workflows = []) {
const key = assignedWorkflowID ? { assignedWorkflowID, workflows } : null
const { data: assignedWorkflowLevel } = useSWR(key, fetchAssignedWorkflow, SWRoptions)
const existingWorkflow = workflows.find(workflow => workflow.id === assignedWorkflowID)
let defaultWorkflowLevel = existingWorkflow?.configuration?.level ?
parseInt(existingWorkflow.configuration?.level, 10) :
1
const defaultWorkflowLevel = existingWorkflow?.configuration?.level
? parseInt(existingWorkflow.configuration.level, 10)
: 1
const key = !existingWorkflow && assignedWorkflowID
? { assignedWorkflowID }
: null // skip data fetching when we already have the workflow level
const { data: assignedWorkflowLevel } = useSWR(key, fetchAssignedWorkflow, SWRoptions)

return assignedWorkflowLevel || defaultWorkflowLevel
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, waitFor } from '@testing-library/react'
import { render } from '@testing-library/react'
import { expect } from 'chai'
import { RouterContext } from 'next/dist/shared/lib/router-context.shared-runtime'
import WorkflowSelectButtons from './WorkflowSelectButtons'
Expand Down Expand Up @@ -54,13 +54,13 @@ describe('Component > WorkflowSelector > WorkflowSelectorButtons', function () {

describe('when workflow assignment is enabled', function () {
describe('when there is an assigned workflow', function () {
it('should only render links for unlocked workflows', async function () {
it('should only render links for unlocked workflows', function () {
const { getAllByRole } = render(
<RouterContext.Provider value={mockRouter}>
<WorkflowSelectButtons assignedWorkflowID='2' workflowAssignmentEnabled workflows={workflows} />
</RouterContext.Provider>
)
await waitFor(() => expect(getAllByRole('link')).to.have.lengthOf(2))
expect(getAllByRole('link')).to.have.lengthOf(2)
})

it('should render other workflows as just text', function () {
Expand Down

0 comments on commit 56a2c14

Please sign in to comment.