-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from sadanandpai/feature/sort
Added sort functionality
- Loading branch information
Showing
3 changed files
with
90 additions
and
32 deletions.
There are no files selected for viewing
93 changes: 65 additions & 28 deletions
93
src/apps/sorting-visualizer/components/controller/array-input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,93 @@ | ||
import React, { useState, useEffect } from 'react' | ||
import { | ||
setArray, | ||
setIsPlaying, | ||
setReset | ||
} from '@/apps/sorting-visualizer/store/sorting-visualizer.slice' | ||
import { useAppDispatch, useAppSelector } from '@/store/hooks' | ||
import { | ||
convertArrayStringToArray, | ||
convertInputToArrayString, | ||
getRndmNumInRange, | ||
} from "@/apps/sorting-visualizer/helpers/array-helpers"; | ||
import { | ||
setArray, | ||
setIsPlaying, | ||
setReset, | ||
} from "@/apps/sorting-visualizer/store/sorting-visualizer.slice"; | ||
import { useAppDispatch, useAppSelector } from "@/store/hooks"; | ||
import { useEffect, useState } from "react"; | ||
sortArray | ||
} from '@/apps/sorting-visualizer/helpers/array-helpers' | ||
|
||
import classes from "./controls.module.scss"; | ||
import classes from './controls.module.scss' | ||
|
||
function ArrayInput() { | ||
const dispatch = useAppDispatch(); | ||
const array = useAppSelector((state) => state.sortViz.array); | ||
const [input, setInput] = useState(array.join(", ")); | ||
type SortFunction = () => void | ||
|
||
function ArrayInput () { | ||
const dispatch = useAppDispatch() | ||
const array = useAppSelector(state => state.sortViz.array) | ||
const [input, setInput] = useState(array.join(', ')) | ||
// const [sortingOptionsVisible, setSortingOptionsVisible] = useState(false) | ||
|
||
useEffect(() => { | ||
dispatch(setIsPlaying(false)); | ||
dispatch(setReset()); | ||
}, [array, dispatch]); | ||
dispatch(setIsPlaying(false)) | ||
dispatch(setReset()) | ||
}, [array, dispatch]) | ||
|
||
const onRandomize = () => { | ||
const newInput = Array.from(new Array(getRndmNumInRange(10, 40)), () => | ||
getRndmNumInRange() | ||
); | ||
setInput(newInput.join(", ")); | ||
dispatch(setArray(newInput)); | ||
}; | ||
) | ||
setInput(newInput.join(', ')) | ||
dispatch(setArray(newInput)) | ||
} | ||
|
||
const handleSortAscending = () => { | ||
const sortedAscending = sortArray(input, 'asc'); | ||
setInput(sortedAscending.join(', ')); | ||
dispatch(setArray(sortedAscending)); | ||
} | ||
|
||
const handleSortDescending = () => { | ||
const sortedDescending = sortArray(input, 'desc'); | ||
setInput(sortedDescending.join(', ')); | ||
dispatch(setArray(sortedDescending)); | ||
} | ||
|
||
const onChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const inputAsStr = convertInputToArrayString(e.target.value); | ||
setInput(inputAsStr); | ||
dispatch(setArray(convertArrayStringToArray(inputAsStr))); | ||
}; | ||
const inputAsStr = convertInputToArrayString(e.target.value) | ||
setInput(inputAsStr) | ||
dispatch(setArray(convertArrayStringToArray(inputAsStr))) | ||
} | ||
|
||
const sortOptions: { [key: string]: SortFunction } = { | ||
ascending: handleSortAscending, | ||
descending: handleSortDescending | ||
} | ||
|
||
return ( | ||
<div className={classes.numbers}> | ||
<button className={classes.rndmBtn} onClick={onRandomize}> | ||
Randomize | ||
</button> | ||
{/* <div className={classes.buttonContainer}> */} | ||
<select | ||
className={classes.buttonContainer} | ||
onChange={e => { | ||
let sortOption = e.target.value | ||
if (sortOptions[sortOption]) { | ||
sortOptions[sortOption]() | ||
} | ||
}} | ||
> | ||
<option value=''>Sort </option> | ||
<option value='ascending'>Ascending</option> | ||
<option value='descending'>Descending</option> | ||
</select> | ||
{/* </div> */} | ||
|
||
<input | ||
className={classes.arrayInput} | ||
type="text" | ||
placeholder="Numbers to sort (comma separate - max 3 digits)" | ||
type='text' | ||
placeholder='Numbers to sort (comma separate - max 3 digits)' | ||
value={input} | ||
onChange={onChange} | ||
/> | ||
</div> | ||
); | ||
) | ||
} | ||
|
||
export default ArrayInput; | ||
export default ArrayInput |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters