Skip to content

Commit cbeacff

Browse files
Merge pull request #9 from danilhendrasr/refactoring
Refactored lots of code
2 parents 45feeb6 + 23dcedd commit cbeacff

20 files changed

+238
-512
lines changed

algorithms-helper/bubble-sort.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ import {
77
postSortAnimation,
88
} from "../utils"
99

10-
const bubbleSort = (array: number[]): [number, number][] => {
10+
const bubbleSort = (array: number[]) => {
1111
let animaSeq: [number, number][] = []
12-
let copyArray: number[] = [...array]
12+
let copyArray = [...array]
1313

14-
let isSortingDone: boolean = false
14+
let isSortingDone = false
1515
while (!isSortingDone) {
1616
for (let i = 0; i < copyArray.length - 1; i++) {
1717
const isInLastPosition = i === copyArray.length - 1
1818
if (isInLastPosition) break
1919

20-
const left: number = copyArray[i]
21-
const right: number = copyArray[i + 1]
20+
const left = copyArray[i]
21+
const right = copyArray[i + 1]
2222

2323
if (left > right) {
2424
const temp = right
@@ -28,10 +28,10 @@ const bubbleSort = (array: number[]): [number, number][] => {
2828
}
2929
}
3030

31-
let isArraySorted: boolean = true
31+
let isArraySorted = true
3232
for (let j = 0; j < copyArray.length - 1; j++) {
33-
const left: number = copyArray[j]
34-
const right: number = copyArray[j + 1]
33+
const left = copyArray[j]
34+
const right = copyArray[j + 1]
3535

3636
if (left > right) isArraySorted = false
3737
}
@@ -46,7 +46,7 @@ export const animateBubbleSort = (
4646
barHeights: number[],
4747
sortingSpeed: number,
4848
callback?: () => void
49-
): void => {
49+
) => {
5050
const animationSequence = bubbleSort(barHeights)
5151
const bars = getAllBars()
5252

algorithms-helper/insertion-sort.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ const insertionSort = (arrayToSort: number[]): InsertionAnimationSequence[] => {
1212
let sortedArray: number[] = []
1313

1414
for (let i = 0; i < arrayToSort.length; i++) {
15-
const currentItem: number = arrayToSort[i]
15+
const currentItem = arrayToSort[i]
1616

17-
const isFirstIteration: boolean = i === 0
17+
const isFirstIteration = i === 0
1818
if (isFirstIteration) {
1919
animaSeq.push({ idx: 0, moveFrom: i, shift: 0 })
2020
sortedArray.push(currentItem)
2121
continue
2222
}
2323

2424
for (let j = sortedArray.length - 1; j >= 0; j--) {
25-
const isLastInneriteration: boolean = j === 0
25+
const isLastInneriteration = j === 0
2626

27-
const leftSide: number = isLastInneriteration ? -Infinity : sortedArray[j - 1]
28-
const rightSide: number = sortedArray[j]
27+
const leftSide = isLastInneriteration ? -Infinity : sortedArray[j - 1]
28+
const rightSide = sortedArray[j]
2929

30-
const isLeftSideLesser: boolean = leftSide <= currentItem
31-
const isRightSideGreater: boolean = currentItem <= rightSide
30+
const isLeftSideLesser = leftSide <= currentItem
31+
const isRightSideGreater = currentItem <= rightSide
3232

3333
if (isLeftSideLesser && isRightSideGreater) {
3434
const aboveRightSide = sortedArray.splice(j)
@@ -56,7 +56,7 @@ export const animateInsertionSort = (
5656
barHeights: number[],
5757
sortingSpeed: number,
5858
callback?: () => void
59-
): void => {
59+
) => {
6060
const animationSequence = insertionSort(barHeights)
6161
const bars = getAllBars()
6262

algorithms-helper/selection-sort.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { INACTIVE_BAR_COLOR, ACTIVE_BAR_COLOR, SORTING_SPEED } from "../constants"
1+
import { INACTIVE_BAR_COLOR, ACTIVE_BAR_COLOR } from "../constants"
22
import { ActiveBar } from "../types"
33
import { changeBarsColor, getAllBars, makeBarsActive, postSortAnimation } from "../utils"
44

5-
const selectionSort = (inputArray: number[]): [number[][], [number, number][]] => {
5+
const selectionSort = (inputArray: number[]) => {
66
let animationSequence: [number, number][] = []
77
let unsortedArray = [...inputArray]
88
let arrayState: number[][] = []
@@ -13,7 +13,7 @@ const selectionSort = (inputArray: number[]): [number[][], [number, number][]] =
1313
let currentMinIdx = x
1414

1515
// These indexes will be stored as an item in the animation sequence array
16-
let selectedBars: [number, number] = [currentMinIdx, currentMinIdx]
16+
let selectedBars = [currentMinIdx, currentMinIdx] as [number, number]
1717

1818
for (let y = x + 1; y < unsortedArray.length; y++) {
1919
let currentItem = unsortedArray[y]
@@ -48,7 +48,7 @@ export const animateSelectionSort = (
4848
barHeights: number[],
4949
sortingSpeed: number,
5050
callback?: () => void
51-
): void => {
51+
) => {
5252
const [arrayStates, animationSequence] = selectionSort(barHeights)
5353
const bars = getAllBars()
5454

components/AlgorithmSelector.tsx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
1-
import { Select, Text, Spacer } from "@geist-ui/react"
2-
import { SortingAlgorithms } from "../types"
3-
41
// @ts-ignore
52
import styles from "../styles/Home.module.scss"
63

7-
const algorithms: SortingAlgorithms[] = ["Selection", "Insertion", "Bubble"]
4+
import { Select, Text, Spacer } from "@geist-ui/react"
5+
import { SortingAlgorithms } from "../types"
6+
import { useControlsDisabled } from "../hooks"
7+
import { ALGORITHMS_LIST } from "../constants"
88

99
interface Props {
10-
disabled: boolean
11-
selectedAlgorithm: string | string[]
12-
onChangeHandler: (algorithm: string | string[]) => void
10+
selectedAlgorithm: SortingAlgorithms
11+
onChange: (algorithm: SortingAlgorithms) => void
1312
}
1413

15-
export const AlgorithmSelector: React.FC<Props> = (props) => {
16-
let { disabled, selectedAlgorithm, onChangeHandler } = props
17-
18-
const selectOptions = algorithms.map((algorithm, idx) => (
14+
const getSelectOptionsFromAlgorithmsList = (algorithmsList: SortingAlgorithms[]) => {
15+
const selectOptions = algorithmsList.map((algorithm, idx) => (
1916
<Select.Option key={idx} value={algorithm}>
20-
{`${algorithm} Sort`}
17+
{`${algorithm} sort`}
2118
</Select.Option>
2219
))
2320

21+
return selectOptions
22+
}
23+
24+
export const AlgorithmSelector: React.FC<Props> = ({ selectedAlgorithm, onChange }) => {
25+
const isControlsDisabled = useControlsDisabled()
26+
const selectOptions = getSelectOptionsFromAlgorithmsList(ALGORITHMS_LIST)
27+
2428
return (
2529
<div className={styles.sideBarInputContainer}>
2630
<Text>Select Algorithm</Text>
2731
<Spacer y={-0.4} />
2832
<Select
29-
disabled={disabled}
30-
value={selectedAlgorithm}
31-
onChange={(algorithm) => onChangeHandler(algorithm)}
32-
size="large">
33+
disabled={isControlsDisabled}
34+
onChange={(algorithm) => onChange(algorithm as SortingAlgorithms)}
35+
size="large"
36+
value={selectedAlgorithm}>
3337
{selectOptions}
3438
</Select>
3539
</div>

components/AppTitle.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Text } from "@geist-ui/react"
2-
31
// @ts-ignore
42
import styles from "../styles/Home.module.scss"
53

4+
import { Text } from "@geist-ui/react"
5+
66
export const AppTitle: React.FC = () => {
77
return (
88
<Text h1 size="2em" className={styles.textCenter}>

components/ArrayLengthModifier.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import { Text, Slider } from "@geist-ui/react"
2-
31
// @ts-ignore
42
import styles from "../styles/Home.module.scss"
53

4+
import { Text, Slider } from "@geist-ui/react"
5+
import { useControlsDisabled } from "../hooks"
6+
67
interface Props {
7-
disabled: boolean
88
value: number
99
onChange: (value: number) => void
1010
}
1111

12-
export const ArrayLengthModifier: React.FC<Props> = (props): JSX.Element => {
13-
const { disabled, value, onChange } = props
12+
export const ArrayLengthModifier: React.FC<Props> = ({ value, onChange }) => {
13+
const isControlsDisabled = useControlsDisabled()
1414

1515
return (
1616
<div className={styles.sideBarInputContainer}>
1717
<Text>Length of array</Text>
1818
<Slider
19-
disabled={disabled}
19+
disabled={isControlsDisabled}
2020
max={100}
2121
min={10}
2222
onChange={onChange}

components/Bar.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { useTheme } from "@geist-ui/react"
22

3-
export const Bar: React.FC<{
3+
interface Props {
44
height: number
55
width: number
6-
}> = (props) => {
7-
const { height, width } = props
6+
}
87

8+
export const Bar: React.FC<Props> = ({ height, width }) => {
99
const { palette } = useTheme()
1010

1111
const barStylings: React.CSSProperties = {

components/LinkToRepo.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Github } from "@geist-ui/react-icons"
2-
31
// @ts-ignore
42
import styles from "../styles/Home.module.scss"
53

6-
export const LinkToRepo: React.FC = (props) => {
4+
import { Github } from "@geist-ui/react-icons"
5+
6+
export const LinkToRepo: React.FC = () => {
77
return (
88
<a
99
className={styles.githubIconWrapper}

components/ResetButton.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { Button } from "@geist-ui/react"
2+
import { useControlsDisabled } from "../hooks"
23

34
interface Props {
4-
disabled: boolean
55
onClick: () => void
66
}
77

8-
export const ResetButton: React.FC<Props> = (props) => {
9-
const { disabled, onClick } = props
8+
export const ResetButton: React.FC<Props> = ({ onClick }) => {
9+
const isControlsDisabled = useControlsDisabled()
10+
1011
return (
11-
<Button disabled={disabled} ghost onClick={onClick} type="error">
12+
<Button disabled={isControlsDisabled} ghost onClick={onClick} type="error">
1213
Reset
1314
</Button>
1415
)

components/SortButton.tsx

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,30 @@
1-
import { useEffect, useState } from "react"
1+
import { useContext } from "react"
22
import { Button, Spacer } from "@geist-ui/react"
33
import { Check, Play } from "@geist-ui/react-icons"
4-
import { SortingState } from "../types"
4+
import { AppStateContext } from "../contexts/app-state"
55

6-
interface SortButtonProps {
7-
clickAction: () => void
8-
sortState: SortingState
6+
interface Props {
7+
onClick: () => void
98
}
109

11-
export const SortButton: React.FC<SortButtonProps> = (props) => {
12-
const { sortState, clickAction } = props
13-
const [text, setText] = useState<SortingState>("Sort")
10+
export const SortButton: React.FC<Props> = ({ onClick }) => {
11+
const sortingState = useContext(AppStateContext).sortingState
1412

15-
useEffect(() => {
16-
if (sortState !== text) setText(sortState)
17-
}, [sortState])
18-
19-
let isSorting = text === "Sorting"
20-
let isSorted = text === "Sorted"
21-
const onClick = () => {
22-
if (isSorted) return
23-
clickAction()
24-
}
13+
const isIdle = sortingState === "Sort"
14+
const isSorting = sortingState === "Sorting"
15+
const isSorted = sortingState === "Sorted"
2516

2617
return (
2718
<Button
28-
style={text === "Sorted" ? { pointerEvents: "none" } : undefined}
2919
loading={isSorting}
3020
onClick={onClick}
31-
shadow={text === "Sort"}
21+
shadow={isIdle}
22+
style={isSorted ? { pointerEvents: "none" } : undefined}
3223
type="secondary">
33-
{text === "Sort" && <Play size={15} />}
34-
{isSorted && (
35-
<>
36-
<Check size={20} />
37-
</>
38-
)}
24+
{isIdle && <Play size={15} />}
25+
{isSorted && <Check size={20} />}
3926
<Spacer inline x={0.2} />
40-
{text}
27+
{sortingState}
4128
</Button>
4229
)
4330
}

0 commit comments

Comments
 (0)