feature/add-user-interaction #3

Merged
manuel merged 4 commits from feature/add-user-interaction into main 2023-04-17 01:25:58 +02:00
4 changed files with 11 additions and 11 deletions
Showing only changes of commit 0e5892147a - Show all commits

View file

@ -4,6 +4,7 @@
"react-dom": "^18.0.0" "react-dom": "^18.0.0"
}, },
"main": "/index.js", "main": "/index.js",
"homepage": ".",
"devDependencies": { "devDependencies": {
"react-scripts": "^5.0.1" "react-scripts": "^5.0.1"
}, },

View file

@ -5,19 +5,17 @@ export default function InputForm({handleResult}) {
const [width, setWidth] = useState(10); const [width, setWidth] = useState(10);
const [height, setHeight] = useState(10); const [height, setHeight] = useState(10);
const [id, setId] = useState(null); const [id, setId] = useState(null);
const [status, setStatus] = useState("typing"); // "typing", "submitting" const [status, setStatus] = useState("ready"); // "ready", "submitting"
if (status === "submitted") { if (status === "submitted") {
return <span/>; return <span/>;
} }
const callAPI = () => { const callAPI = () => {
let url = "https://manuel.friedli.info/labyrinth/create/json?w=" + width + handleResult({});
"&h=" + height; const url = `https://manuel.friedli.info/labyrinth/create/json?w=${width}&h=${height}&id=${id || ''}`;
if (!!id) {
url += "&id=" + id;
}
fetch(url) fetch(url)
.then(response => response.json()) .then(response => response.json())
// .then(result => new Promise(resolve => setTimeout(resolve, 600, result)))
.then(result => { .then(result => {
handleResult(result); handleResult(result);
setId(_ => result.id); setId(_ => result.id);
@ -25,10 +23,10 @@ export default function InputForm({handleResult}) {
.catch(reason => { .catch(reason => {
console.error("Failed to fetch maze data.", reason); console.error("Failed to fetch maze data.", reason);
// FIXME alert is not user friendly // FIXME alert is not user friendly
alert("Failed to fetch maze data: " + reason); alert(`Failed to fetch maze data: ${reason}`);
}) })
.finally(() => { .finally(() => {
setStatus("typing"); setStatus("ready");
}); });
}; };
const handleSubmit = (e) => { const handleSubmit = (e) => {
@ -103,7 +101,7 @@ export default function InputForm({handleResult}) {
disabled={status === "submitting" disabled={status === "submitting"
|| isNaN(width) || isNaN(width)
|| isNaN(height) || isNaN(height)
}>GO! }>{status === "ready" ? "Create" : "Loading ..."}
</button> </button>
</form> </form>
); );

View file

@ -7,7 +7,8 @@ export default function Maze({labyrinth, showSolution = false}) {
return <div>No valid maze.</div> return <div>No valid maze.</div>
} }
const maze = labyrinth.grid.map((row, rowIdx) => <Row key={"r" + rowIdx} spec={row} const maze = labyrinth.grid.map((row, rowIdx) => <Row key={rowIdx}
spec={row}
index={rowIdx} index={rowIdx}
showSolution={showSolution}/>); showSolution={showSolution}/>);
return ( return (

View file

@ -2,7 +2,7 @@ import React from 'react';
import Cell from "./Cell"; import Cell from "./Cell";
export default function Row({spec, index, showSolution}) { export default function Row({spec, index, showSolution}) {
const cells = spec.map((cell, cellIdx) => <Cell key={"c" + index + "-" + cellIdx} const cells = spec.map((cell, cellIdx) => <Cell key={`${index}-${cellIdx}`}
spec={cell} spec={cell}
rowIndex={index} rowIndex={index}
cellIndex={cellIdx} cellIndex={cellIdx}