fix choosing maps
This commit is contained in:
parent
f1dec16571
commit
1c5e14cd47
1 changed files with 75 additions and 18 deletions
28
tank-frontend/src/MapDropDown.tsx
Normal file
28
tank-frontend/src/MapDropDown.tsx
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { ChangeEvent, useEffect, useState } from "react";
|
||||
import { getMaps, postMaps } from "./serverCalls";
|
||||
|
||||
export default function MapDropDown() {
|
||||
const [mapList, setMaps] = useState<string[] | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let aborted = false;
|
||||
async function startFetch() {
|
||||
const response = await getMaps();
|
||||
if (!aborted && response.ok && response.successResult)
|
||||
setMaps(response.successResult);
|
||||
}
|
||||
startFetch();
|
||||
return () => { aborted = true };
|
||||
}, []);
|
||||
|
||||
const onChange = (event: ChangeEvent<HTMLSelectElement>) => {
|
||||
postMaps(event.target.options[event.target.selectedIndex].value);
|
||||
event.preventDefault();
|
||||
};
|
||||
|
||||
return <select value="maps" className='DropDown' onChange={onChange}>
|
||||
<option value="" defaultValue={""} >Choose map</option>
|
||||
{mapList?.map(m =>
|
||||
<option key={m} value={m}>{m}</option>)}
|
||||
</select>
|
||||
}
|
||||
Reference in a new issue