fetch map list with react query, simplify other fetches
This commit is contained in:
parent
c978df1e4c
commit
c48514721c
1 changed files with 37 additions and 74 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import {useEffect, useState} from 'react';
|
||||
import './JoinForm.css';
|
||||
import {Player, postPlayer} from './serverCalls';
|
||||
import {makeApiUrl, Player} from './serverCalls';
|
||||
import Column from './components/Column.tsx';
|
||||
import Button from './components/Button.tsx';
|
||||
import TextInput from './components/TextInput.tsx';
|
||||
|
|
@ -16,20 +16,20 @@ export default function JoinForm({onDone}: {
|
|||
if (!clicked || data)
|
||||
return;
|
||||
|
||||
postPlayer(name).then(response => {
|
||||
if (response.ok && response.successResult) {
|
||||
onDone(response.successResult!.trim());
|
||||
const url = makeApiUrl('/player');
|
||||
url.searchParams.set('name', name);
|
||||
|
||||
fetch(url, {method: 'POST'})
|
||||
.then(async response => {
|
||||
if (!response.ok) {
|
||||
setErrorText(`${response.status} (${response.statusText}): ${await response.text()}`);
|
||||
setClicked(false);
|
||||
return;
|
||||
}
|
||||
|
||||
onDone((await response.json()).trim());
|
||||
setErrorText(null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (response.additionalErrorText)
|
||||
setErrorText(`${response.statusCode} (${response.statusText}): ${response.additionalErrorText}`);
|
||||
else
|
||||
setErrorText(`${response.statusCode} (${response.statusText})`);
|
||||
|
||||
setClicked(false);
|
||||
});
|
||||
});
|
||||
}, [clicked, setData, data, setClicked, onDone, errorText]);
|
||||
|
||||
const [name, setName] = useState('');
|
||||
|
|
|
|||
Reference in a new issue