2023-04-17 02:43:36 +02:00
|
|
|
import {useState} from 'react';
|
|
|
|
import ValidatingInputNumberField from "./ValidatingInputNumberField.tsx";
|
|
|
|
import {actionLoadedMaze, actionLoadingFailed, actionStartedLoading} from "./state/action.ts";
|
2023-04-15 19:37:50 +02:00
|
|
|
|
2023-04-17 01:19:42 +02:00
|
|
|
export default function InputForm({state, dispatch}) {
|
2023-04-16 02:34:11 +02:00
|
|
|
const [width, setWidth] = useState(10);
|
|
|
|
const [height, setHeight] = useState(10);
|
2023-04-17 02:43:36 +02:00
|
|
|
const [id, setId] = useState(null as number);
|
2023-04-15 19:37:50 +02:00
|
|
|
|
2023-04-17 01:19:42 +02:00
|
|
|
const handleSubmit = (e) => {
|
|
|
|
e.preventDefault();
|
2023-04-17 02:43:36 +02:00
|
|
|
dispatch(actionStartedLoading());
|
2023-04-16 04:04:00 +02:00
|
|
|
const url = `https://manuel.friedli.info/labyrinth/create/json?w=${width}&h=${height}&id=${id || ''}`;
|
2023-04-15 19:37:50 +02:00
|
|
|
fetch(url)
|
|
|
|
.then(response => response.json())
|
2023-04-16 04:04:00 +02:00
|
|
|
// .then(result => new Promise(resolve => setTimeout(resolve, 600, result)))
|
2023-04-15 19:37:50 +02:00
|
|
|
.then(result => {
|
2023-04-17 02:43:36 +02:00
|
|
|
dispatch(actionLoadedMaze(result));
|
2023-04-15 19:37:50 +02:00
|
|
|
})
|
|
|
|
.catch(reason => {
|
|
|
|
console.error("Failed to fetch maze data.", reason);
|
2023-04-17 02:43:36 +02:00
|
|
|
dispatch(actionLoadingFailed(reason));
|
2023-04-15 19:37:50 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
const validateWidthHeightInput = value => {
|
|
|
|
if (isNaN(value) || "" === value || (Math.floor(value) !== Number(value))) {
|
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
message: "Must be an integer greater than 1.",
|
|
|
|
value
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (value < 1) {
|
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
message: "Must be greater than 1.",
|
|
|
|
value
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
valid: true,
|
|
|
|
value
|
|
|
|
};
|
|
|
|
};
|
|
|
|
const validateIdInput = value => {
|
|
|
|
// FIXME doesn't handle strings with characters correctly (e.g. "asdf" yields an empty value, due to "type=number").
|
|
|
|
if (isNaN(value) || ("" !== value && ((Math.floor(value) !== Number(value))))) {
|
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
message: "Must be empty or an integer",
|
|
|
|
value
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
valid: true,
|
|
|
|
value
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<ValidatingInputNumberField id={"width"}
|
|
|
|
label={"Width"}
|
2023-04-16 01:01:23 +02:00
|
|
|
value={width}
|
2023-04-15 19:37:50 +02:00
|
|
|
constraints={{
|
|
|
|
min: 2
|
|
|
|
}}
|
|
|
|
validatorFn={validateWidthHeightInput}
|
2023-04-17 01:19:42 +02:00
|
|
|
disabled={state.loading}
|
2023-04-15 19:37:50 +02:00
|
|
|
onChange={setWidth}
|
|
|
|
/><br/>
|
|
|
|
<ValidatingInputNumberField id={"height"}
|
|
|
|
label={"Height"}
|
2023-04-16 01:01:23 +02:00
|
|
|
value={height}
|
2023-04-15 19:37:50 +02:00
|
|
|
constraints={{
|
|
|
|
min: 2
|
|
|
|
}}
|
|
|
|
validatorFn={validateWidthHeightInput}
|
2023-04-17 01:19:42 +02:00
|
|
|
disabled={state.loading}
|
2023-04-15 19:37:50 +02:00
|
|
|
onChange={setHeight}
|
|
|
|
/><br/>
|
|
|
|
<ValidatingInputNumberField id={"id"}
|
|
|
|
label={"ID (optional)"}
|
2023-04-16 01:01:23 +02:00
|
|
|
value={id}
|
2023-04-15 19:37:50 +02:00
|
|
|
validatorFn={validateIdInput}
|
2023-04-17 01:19:42 +02:00
|
|
|
disabled={state.loading}
|
2023-04-15 19:37:50 +02:00
|
|
|
onChange={setId}
|
|
|
|
/><br/>
|
2024-12-14 13:31:51 +01:00
|
|
|
<label for="algorithm">Algorithmus:</label>
|
|
|
|
<select id={"algorithm"}>
|
|
|
|
<option>wilson</option>
|
|
|
|
<option>random</option>
|
|
|
|
</select><br/>
|
2023-04-15 19:37:50 +02:00
|
|
|
<button type={"submit"}
|
2023-04-17 01:19:42 +02:00
|
|
|
disabled={state.loading
|
2023-04-15 19:37:50 +02:00
|
|
|
|| isNaN(width)
|
|
|
|
|| isNaN(height)
|
2023-04-17 01:19:42 +02:00
|
|
|
}>{state.loading ? "Loading ..." : "Create Maze!"}
|
2023-04-15 19:37:50 +02:00
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|