Skip to content

Commit

Permalink
tweak the moveStep logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jerader committed Feb 16, 2024
1 parent 0df6814 commit 7cbc731
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions protocol-designer/src/components/steplist/DraggableStepItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export const DraggableStepItems = (
const { t } = useTranslation('shared')
const [isOver, setIsOver] = React.useState<boolean>(false)
const [stepIds, setStepIds] = React.useState<StepIdType[]>(orderedStepIds)

Check warning on line 95 in protocol-designer/src/components/steplist/DraggableStepItems.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/steplist/DraggableStepItems.tsx#L92-L95

Added lines #L92 - L95 were not covered by tests

// needed to initalize stepIds
React.useEffect(() => {
setStepIds(orderedStepIds)

Check warning on line 99 in protocol-designer/src/components/steplist/DraggableStepItems.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/steplist/DraggableStepItems.tsx#L98-L99

Added lines #L98 - L99 were not covered by tests
}, [orderedStepIds])
Expand All @@ -109,17 +111,13 @@ export const DraggableStepItems = (
stepIds.findIndex(id => stepId === id)

Check warning on line 111 in protocol-designer/src/components/steplist/DraggableStepItems.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/steplist/DraggableStepItems.tsx#L110-L111

Added lines #L110 - L111 were not covered by tests

const moveStep = (stepId: StepIdType, targetIndex: number): void => {
const currentIndex = findStepIndex(stepId)
const currentRemoved = [
...stepIds.slice(0, currentIndex),
...stepIds.slice(currentIndex + 1, stepIds.length),
]
const currentReinserted = [
...currentRemoved.slice(0, targetIndex),
stepId,
...currentRemoved.slice(targetIndex, currentRemoved.length),
]
setStepIds(currentReinserted)
const currentIndex = orderedStepIds.findIndex(id => id === stepId)

Check warning on line 114 in protocol-designer/src/components/steplist/DraggableStepItems.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/steplist/DraggableStepItems.tsx#L113-L114

Added lines #L113 - L114 were not covered by tests

const newStepIds = [...orderedStepIds]
newStepIds.splice(currentIndex, 1)
newStepIds.splice(targetIndex, 0, stepId)

Check warning on line 118 in protocol-designer/src/components/steplist/DraggableStepItems.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/steplist/DraggableStepItems.tsx#L116-L118

Added lines #L116 - L118 were not covered by tests

setStepIds(newStepIds)

Check warning on line 120 in protocol-designer/src/components/steplist/DraggableStepItems.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/steplist/DraggableStepItems.tsx#L120

Added line #L120 was not covered by tests
}

const currentIds = isOver ? stepIds : orderedStepIds
Expand Down

0 comments on commit 7cbc731

Please sign in to comment.