Merge pull request #3 from kaesaecracker/use-mutation
useMutation for POST requests
This commit is contained in:
commit
85fab801c5
|
@ -1,40 +1,33 @@
|
||||||
import {useEffect, useState} from 'react';
|
import {useState} from 'react';
|
||||||
import './JoinForm.css';
|
import './JoinForm.css';
|
||||||
import {makeApiUrl, Player} from './serverCalls';
|
import {makeApiUrl} from './serverCalls';
|
||||||
import Column from './components/Column.tsx';
|
import Column from './components/Column.tsx';
|
||||||
import Button from './components/Button.tsx';
|
import Button from './components/Button.tsx';
|
||||||
import TextInput from './components/TextInput.tsx';
|
import TextInput from './components/TextInput.tsx';
|
||||||
|
import {useMutation} from '@tanstack/react-query';
|
||||||
|
|
||||||
export default function JoinForm({onDone}: {
|
export default function JoinForm({onDone}: {
|
||||||
onDone: (name: string) => void;
|
onDone: (name: string) => void;
|
||||||
}) {
|
}) {
|
||||||
const [clicked, setClicked] = useState(false);
|
const postPlayer = useMutation({
|
||||||
const [data, setData] = useState<Player | null>(null);
|
retry: false,
|
||||||
const [errorText, setErrorText] = useState<string | null>();
|
mutationFn: async ({name}: { name: string }) => {
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!clicked || data)
|
|
||||||
return;
|
|
||||||
|
|
||||||
const url = makeApiUrl('/player');
|
const url = makeApiUrl('/player');
|
||||||
url.searchParams.set('name', name);
|
url.searchParams.set('name', name);
|
||||||
|
|
||||||
fetch(url, {method: 'POST'})
|
const response = await fetch(url, {method: 'POST'});
|
||||||
.then(async response => {
|
|
||||||
if (!response.ok) {
|
if (!response.ok)
|
||||||
setErrorText(`${response.status} (${response.statusText}): ${await response.text()}`);
|
throw new Error(`${response.status} (${response.statusText}): ${await response.text()}`);
|
||||||
setClicked(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
onDone((await response.json()).trim());
|
onDone((await response.json()).trim());
|
||||||
setErrorText(null);
|
}
|
||||||
});
|
});
|
||||||
}, [clicked, setData, data, setClicked, onDone, errorText]);
|
|
||||||
|
|
||||||
const [name, setName] = useState('');
|
const [name, setName] = useState('');
|
||||||
const disableButtons = clicked || name.trim() === '';
|
const disableButtons = postPlayer.isPending || name.trim() === '';
|
||||||
const setClickedTrue = () => setClicked(true);
|
|
||||||
|
const confirm = () => postPlayer.mutate({name});
|
||||||
|
|
||||||
return <Column className="JoinForm">
|
return <Column className="JoinForm">
|
||||||
<h3> Enter your name to play </h3>
|
<h3> Enter your name to play </h3>
|
||||||
|
@ -42,12 +35,12 @@ export default function JoinForm({onDone}: {
|
||||||
value={name}
|
value={name}
|
||||||
placeholder="player name"
|
placeholder="player name"
|
||||||
onChange={e => setName(e.target.value)}
|
onChange={e => setName(e.target.value)}
|
||||||
onEnter={setClickedTrue}
|
onEnter={confirm}
|
||||||
/>
|
/>
|
||||||
<Button
|
<Button
|
||||||
onClick={setClickedTrue}
|
onClick={confirm}
|
||||||
disabled={disableButtons}
|
disabled={disableButtons}
|
||||||
text="INSERT COIN"/>
|
text="INSERT COIN"/>
|
||||||
{errorText && <p>{errorText}</p>}
|
{postPlayer.isError && <p>{postPlayer.error.message}</p>}
|
||||||
</Column>;
|
</Column>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import {ChangeEvent} from 'react';
|
import {ChangeEvent} from 'react';
|
||||||
import {makeApiUrl} from './serverCalls';
|
import {makeApiUrl} from './serverCalls';
|
||||||
import './MapChooser.css';
|
import './MapChooser.css';
|
||||||
import {useQuery} from '@tanstack/react-query';
|
import {useMutation, useQuery} from '@tanstack/react-query';
|
||||||
|
|
||||||
export default function MapChooser() {
|
export default function MapChooser() {
|
||||||
const query = useQuery({
|
const query = useQuery({
|
||||||
|
@ -15,20 +15,31 @@ export default function MapChooser() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const postMap = useMutation({
|
||||||
|
retry: false,
|
||||||
|
mutationFn: async (map: string) => {
|
||||||
|
const url = makeApiUrl('/map');
|
||||||
|
url.searchParams.set('name', map);
|
||||||
|
|
||||||
|
const response = await fetch(url, {method: 'POST'});
|
||||||
|
if (!response.ok)
|
||||||
|
throw new Error(`${response.status} (${response.statusText}): ${await response.text()}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const onChange = (event: ChangeEvent<HTMLSelectElement>) => {
|
const onChange = (event: ChangeEvent<HTMLSelectElement>) => {
|
||||||
if (event.target.selectedIndex < 1)
|
if (event.target.selectedIndex < 1)
|
||||||
return;
|
return;
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
const url = makeApiUrl('/map');
|
const chosenMap = event.target.options[event.target.selectedIndex].value;
|
||||||
url.searchParams.set('name', event.target.options[event.target.selectedIndex].value);
|
postMap.mutate(chosenMap);
|
||||||
|
|
||||||
fetch(url, {method: 'POST'});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return <select value="maps" className="MapChooser-DropDown" onChange={onChange}>
|
const disabled = !query.isSuccess || postMap.isPending;
|
||||||
|
|
||||||
|
return <select className="MapChooser-DropDown" onChange={onChange} disabled={disabled}>
|
||||||
<option value="" defaultValue={''}>Choose map</option>
|
<option value="" defaultValue={''}>Choose map</option>
|
||||||
{query.isSuccess && query.data.map(m =>
|
{query.isSuccess && query.data.map(m => <option key={m} value={m}>{m}</option>)}
|
||||||
<option key={m} value={m}>{m}</option>)}
|
|
||||||
</select>;
|
</select>;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue