|
1 | | -import { getAlgoByName} from "../../utils/algos" |
2 | | -import { getRandomArray } from "../../utils/getRandomArray" |
3 | | -import BubbleSort from "../../algorithms/BubbleSort" |
4 | | -import {createSlice,PayloadAction} from "@reduxjs/toolkit" |
| 1 | +import { getAlgoByName, getRandomArray } from "../../utils/exports"; |
| 2 | +import { BubbleSort } from "../../algorithms/exports"; |
| 3 | +import { createSlice, PayloadAction } from "@reduxjs/toolkit"; |
5 | 4 |
|
6 | | -export const INITIAL_ARRAY_SIZE = 20 |
7 | | -const MIN_ARRAY_ELEMENT_VALUE = 10 |
8 | | -const MAX_ARRAY_ELEMENT_VALUE = 60 |
| 5 | +export const INITIAL_ARRAY_SIZE = 20; |
| 6 | +const MIN_ARRAY_ELEMENT_VALUE = 10; |
| 7 | +const MAX_ARRAY_ELEMENT_VALUE = 60; |
9 | 8 |
|
10 | | -//Setting BUBBLESORT as initial algorithm |
| 9 | +//Setting BUBBLESORT as initial algorithm |
11 | 10 |
|
12 | 11 | const initialState = { |
13 | | - isRunning: false, |
14 | | - currentStepIndex: 0, |
15 | | - algorithm:'BubbleSort' , |
16 | | - sortingSteps: BubbleSort(getRandomArray(INITIAL_ARRAY_SIZE,MIN_ARRAY_ELEMENT_VALUE, MAX_ARRAY_ELEMENT_VALUE)) |
17 | | -} |
| 12 | + isRunning: false, |
| 13 | + currentStepIndex: 0, |
| 14 | + algorithm: "BubbleSort", |
| 15 | + sortingSteps: BubbleSort( |
| 16 | + getRandomArray( |
| 17 | + INITIAL_ARRAY_SIZE, |
| 18 | + MIN_ARRAY_ELEMENT_VALUE, |
| 19 | + MAX_ARRAY_ELEMENT_VALUE |
| 20 | + ) |
| 21 | + ), |
| 22 | +}; |
18 | 23 |
|
19 | 24 | export const sortingSlice = createSlice({ |
20 | | - name: "Sorting", |
21 | | - initialState, |
22 | | - reducers: { |
23 | | - setRunningState: (state, action: PayloadAction<boolean>) => { |
24 | | - state.isRunning = action.payload |
25 | | - }, |
26 | | - increaseCurrentStepIndex: (state) => { |
27 | | - if(state.currentStepIndex !== state.sortingSteps.length - 1){ |
28 | | - state.currentStepIndex += 1 |
29 | | - } |
30 | | - }, |
31 | | - decreaseCurrentStepIndex: (state) => { |
32 | | - if(state.currentStepIndex !== 0){ |
33 | | - state.currentStepIndex -= 1 |
34 | | - } |
35 | | - }, |
36 | | - updateAlgo: (state, action: PayloadAction<string>) => { |
37 | | - state.algorithm= action.payload |
38 | | - state.sortingSteps = getAlgoByName(state.algorithm)(state.sortingSteps[0].arrayState) |
39 | | - }, |
40 | | - updateBarsCount: (state,action: PayloadAction<number>) => { |
41 | | - state.sortingSteps = getAlgoByName(state.algorithm)(getRandomArray(action.payload,MIN_ARRAY_ELEMENT_VALUE,MAX_ARRAY_ELEMENT_VALUE)) |
42 | | - state.currentStepIndex = 0 |
43 | | - }, |
44 | | - restartSort: (state) => { |
45 | | - state.currentStepIndex = 0 |
46 | | - }, |
47 | | - regenerateBars: (state) => { |
48 | | - state.currentStepIndex = 0 |
49 | | - state.sortingSteps = getAlgoByName(state.algorithm)(getRandomArray(state.sortingSteps[0].arrayState.length, MIN_ARRAY_ELEMENT_VALUE,MAX_ARRAY_ELEMENT_VALUE)) |
50 | | - } |
51 | | - } |
52 | | -}) |
53 | | - |
54 | | -export const { setRunningState ,increaseCurrentStepIndex,updateAlgo, decreaseCurrentStepIndex,updateBarsCount, restartSort, regenerateBars} = sortingSlice.actions |
55 | | -export default sortingSlice.reducer |
| 25 | + name: "Sorting", |
| 26 | + initialState, |
| 27 | + reducers: { |
| 28 | + setRunningState: (state, action: PayloadAction<boolean>) => { |
| 29 | + state.isRunning = action.payload; |
| 30 | + }, |
| 31 | + increaseCurrentStepIndex: (state) => { |
| 32 | + if (state.currentStepIndex !== state.sortingSteps.length - 1) { |
| 33 | + state.currentStepIndex += 1; |
| 34 | + } |
| 35 | + }, |
| 36 | + decreaseCurrentStepIndex: (state) => { |
| 37 | + if (state.currentStepIndex !== 0) { |
| 38 | + state.currentStepIndex -= 1; |
| 39 | + } |
| 40 | + }, |
| 41 | + updateAlgo: (state, action: PayloadAction<string>) => { |
| 42 | + state.algorithm = action.payload; |
| 43 | + state.sortingSteps = getAlgoByName(state.algorithm)( |
| 44 | + state.sortingSteps[0].arrayState |
| 45 | + ); |
| 46 | + }, |
| 47 | + updateBarsCount: (state, action: PayloadAction<number>) => { |
| 48 | + state.sortingSteps = getAlgoByName(state.algorithm)( |
| 49 | + getRandomArray( |
| 50 | + action.payload, |
| 51 | + MIN_ARRAY_ELEMENT_VALUE, |
| 52 | + MAX_ARRAY_ELEMENT_VALUE |
| 53 | + ) |
| 54 | + ); |
| 55 | + state.currentStepIndex = 0; |
| 56 | + }, |
| 57 | + restartSort: (state) => { |
| 58 | + state.currentStepIndex = 0; |
| 59 | + }, |
| 60 | + regenerateBars: (state) => { |
| 61 | + state.currentStepIndex = 0; |
| 62 | + state.sortingSteps = getAlgoByName(state.algorithm)( |
| 63 | + getRandomArray( |
| 64 | + state.sortingSteps[0].arrayState.length, |
| 65 | + MIN_ARRAY_ELEMENT_VALUE, |
| 66 | + MAX_ARRAY_ELEMENT_VALUE |
| 67 | + ) |
| 68 | + ); |
| 69 | + }, |
| 70 | + }, |
| 71 | +}); |
56 | 72 |
|
| 73 | +export const { |
| 74 | + setRunningState, |
| 75 | + increaseCurrentStepIndex, |
| 76 | + updateAlgo, |
| 77 | + decreaseCurrentStepIndex, |
| 78 | + updateBarsCount, |
| 79 | + restartSort, |
| 80 | + regenerateBars, |
| 81 | +} = sortingSlice.actions; |
| 82 | +export default sortingSlice.reducer; |
0 commit comments