Skip to content

Commit 474504c

Browse files
committed
fix: updated auto sorting status when animation is changed
1 parent 25cd84f commit 474504c

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/hooks/useSort.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { useDispatch, useSelector } from 'react-redux'
33

44
import { Bar } from '@/utils/Bar'
55
import { RootState } from '@/store'
6-
import {
6+
import sorting, {
77
goToNextStep,
88
goToPreviousStep,
9-
resetSorting
9+
resetSorting,
10+
getSortingStatus
1011
} from '@/store/slice/sorting'
1112

1213
type IState = number[]
@@ -24,6 +25,7 @@ const useSort = (): {
2425
const { animationSpeed, currentStep, steps, arraySize } = useSelector(
2526
(state: RootState) => state.sorting
2627
)
28+
const status = useSelector(getSortingStatus)
2729
const dispatch = useDispatch()
2830

2931
const pause = (): void => {
@@ -33,14 +35,18 @@ const useSort = (): {
3335

3436
useEffect(() => {
3537
pause()
36-
return pause
38+
return () => {
39+
pause
40+
}
3741
}, [steps, arraySize])
3842

3943
useEffect(() => {
40-
if (currentStep > 0) {
44+
if (status === 'SORTING') {
4145
sort()
4246
}
43-
return sort
47+
return () => {
48+
sort
49+
}
4450
}, [animationSpeed])
4551

4652
const sort = (): void => {

src/store/slice/sorting.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ export const getCurrentArray = ({ sorting }: RootState): string =>
141141
sorting.steps[sorting.currentStep].map(i => i.value).join(', ')
142142
export const getInitialArray = ({ sorting }: RootState): string =>
143143
sorting.initialArray.map(i => i.value).join(', ')
144-
export const getSortingStatus = ({ sorting }: RootState): string => {
144+
145+
type SortingStatus = BarStatus | 'SORTING'
146+
export const getSortingStatus = ({ sorting }: RootState): SortingStatus => {
145147
if (sorting.currentStep === 0) {
146148
return BarStatus.UNSORTED
147149
} else if (

0 commit comments

Comments
 (0)