Skip to content

Commit cae76f3

Browse files
committed
ASP-98 FEAT: adapt AVL Tree with new url param functionalities
1 parent 9bc1e27 commit cae76f3

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

src/algorithms/parameters/AVLTreeParam.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import {
2727
shuffleArray,
2828
} from './helpers/ParamHelper';
2929

30+
import PropTypes from 'prop-types'; // Import this for URL Param
31+
import { withAlgorithmParams } from './helpers/urlHelpers' // Import this for URL Param
32+
import { URLContext } from '../../context/urlState';
33+
3034
const DEFAULT_NODES = genRandNumList(10, 1, 100);
3135
const DEFAULT_TARGET = '2';
3236
const INSERTION = 'insertion';
@@ -49,26 +53,34 @@ const BlueRadio = withStyles({
4953
checked: {},
5054
})((props) => <Radio {...props} />);
5155

52-
function AVLTreeParam() {
56+
function AVLTreeParam({ mode, list, value }) {
5357
const { algorithm, dispatch } = useContext(GlobalContext);
5458
const [message, setMessage] = useState(null);
55-
const [nodes, setNodes] = useState(DEFAULT_NODES);
59+
const [localNodes, setlocalNodes] = useState(list || DEFAULT_NODES);
60+
const { setNodes, setSearchValue } = useContext(URLContext);
5661
const [avlCase, setAVLCase] = useState({
5762
random: true,
5863
sorted: false,
5964
balanced: false,
6065
});
6166

67+
const [localValue, setLocalValue] = useState(DEFAULT_TARGET);
68+
69+
useEffect(() => {
70+
setNodes(localNodes);
71+
setSearchValue(localValue);
72+
}, [localNodes, localValue, setNodes, setSearchValue]);
73+
6274
const handleChange = (e) => {
6375
switch (e.target.name) {
6476
case 'random':
65-
setNodes(shuffleArray(nodes));
77+
setlocalNodes(shuffleArray(localNodes));
6678
break;
6779
case 'sorted':
68-
setNodes([...nodes].sort((a, b) => a - b));
80+
setlocalNodes([...localNodes].sort((a, b) => a - b));
6981
break;
7082
case 'balanced':
71-
setNodes(balanceBSTArray([...nodes].sort((a, b) => a - b)));
83+
setlocalNodes(balanceBSTArray([...localNodes].sort((a, b) => a - b)));
7284
break;
7385
default:
7486
}
@@ -83,6 +95,7 @@ function AVLTreeParam() {
8395
const handleSearch = (e) => {
8496
e.preventDefault();
8597
const inputValue = e.target[0].value;
98+
setLocalValue(inputValue);
8699

87100
if (singleNumberValidCheck(inputValue)) {
88101
const target = parseInt(inputValue, 10);
@@ -134,13 +147,13 @@ function AVLTreeParam() {
134147
formClassName="formLeft"
135148
DEFAULT_VAL={(() => {
136149
if (avlCase.balanced) {
137-
return balanceBSTArray([...nodes].sort((a, b) => a - b));
150+
return balanceBSTArray([...localNodes].sort((a, b) => a - b));
138151
} if (avlCase.sorted) {
139-
return [...nodes].sort((a, b) => a - b);
152+
return [...localNodes].sort((a, b) => a - b);
140153
}
141-
return nodes;
154+
return localNodes;
142155
})()}
143-
SET_VAL={setNodes}
156+
SET_VAL={setlocalNodes}
144157
ALGORITHM_NAME={INSERTION}
145158
EXAMPLE={INSERTION_EXAMPLE}
146159
setMessage={setMessage}
@@ -152,7 +165,7 @@ function AVLTreeParam() {
152165
buttonName="Search"
153166
mode="search"
154167
formClassName="formRight"
155-
DEFAULT_VAL={DEFAULT_TARGET}
168+
DEFAULT_VAL={value || localValue}
156169
ALGORITHM_NAME={SEARCH}
157170
EXAMPLE={SEARCH_EXAMPLE}
158171
handleSubmit={handleSearch}
@@ -199,4 +212,12 @@ function AVLTreeParam() {
199212
);
200213
}
201214

202-
export default AVLTreeParam;
215+
// Define the prop types for URL Params
216+
AVLTreeParam.propTypes = {
217+
alg: PropTypes.string.isRequired,
218+
mode: PropTypes.string.isRequired,
219+
list: PropTypes.string.isRequired,
220+
value: PropTypes.string.isRequired
221+
};
222+
223+
export default withAlgorithmParams(AVLTreeParam); // Export with the wrapper for URL Params

0 commit comments

Comments
 (0)