import {ActionDispatch, FormEvent, useState} from 'react'; import { Action, actionLoadedMaze, actionLoadingFailed, actionStartedLoading, actionToggledShowSolution } from "./state/action.ts"; import styles from "./input-form.module.scss"; import "./input-form.scss"; import {State} from "./state/state.ts"; import {ValidatingInputNumberField, ValidatingInputRegExpField, ValidatorFunction} from "./validating-input-field.tsx"; export default function InputForm({state, dispatch}: { state: State, dispatch: ActionDispatch<[Action]> }) { const [width, setWidth] = useState(10); const [height, setHeight] = useState(10); const [id, setId] = useState(); const [algorithm, setAlgorithm] = useState('wilson'); const handleSubmit = (e: FormEvent) => { e.preventDefault(); dispatch(actionStartedLoading()); dispatch(actionToggledShowSolution(false)); const url = `https://manuel.friedli.info/labyrinth/create/json?w=${width}&h=${height}&id=${id || ''}&algorithm=${algorithm}`; fetch(url) .then(response => response.json()) .then(result => { dispatch(actionLoadedMaze(result)); }) .catch(reason => { console.error("Failed to fetch maze data.", reason); dispatch(actionLoadingFailed(reason)); }); }; const validateSizeInput: ValidatorFunction = (value: string) => { const numberValue = Number(value); if (isNaN(numberValue) || "" === value || (Math.floor(numberValue) !== numberValue)) { return { valid: false, message: "Must be an integer greater than 1." }; } if (numberValue < 1) { return { valid: false, message: "Must be greater than 1." }; } return { valid: true, value: numberValue }; }; return (
{/**/}
); }