fetch map list with react query, simplify other fetches

This commit is contained in:
Vinzenz Schroeter 2024-04-29 12:51:58 +02:00
parent c978df1e4c
commit c48514721c
3 changed files with 38 additions and 75 deletions

View file

@ -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('');

View file

@ -1,29 +1,34 @@
import { ChangeEvent, useEffect, useState } from "react";
import { getMaps, postMaps } from "./serverCalls";
import './MapChooser.css'
import {ChangeEvent} from 'react';
import {makeApiUrl} from './serverCalls';
import './MapChooser.css';
import {useQuery} from '@tanstack/react-query';
export default function MapChooser() {
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);
const query = useQuery({
queryKey: ['get-maps'],
queryFn: async () => {
const url = makeApiUrl('/map');
const response = await fetch(url, {method: 'GET'});
if (!response.ok)
throw new Error(`response failed with code ${response.status} (${response.status})${await response.text()}`);
return await response.json() as string[];
}
startFetch();
return () => { aborted = true };
}, []);
});
const onChange = (event: ChangeEvent<HTMLSelectElement>) => {
postMaps(event.target.options[event.target.selectedIndex].value);
if (event.target.selectedIndex < 1)
return;
event.preventDefault();
const url = makeApiUrl('/map');
url.searchParams.set('name', event.target.options[event.target.selectedIndex].value);
fetch(url, {method: 'POST'});
};
return <select value="maps" className='MapChooser-DropDown' onChange={onChange}>
<option value="" defaultValue={""} >Choose map</option>
{mapList?.map(m =>
return <select value="maps" className="MapChooser-DropDown" onChange={onChange}>
<option value="" defaultValue={''}>Choose map</option>
{query.isSuccess && query.data.map(m =>
<option key={m} value={m}>{m}</option>)}
</select>
</select>;
}

View file

@ -2,14 +2,6 @@ export function makeApiUrl(path: string, protocol: 'http' | 'ws' = 'http') {
return new URL(`${protocol}://${window.location.hostname}${path}`);
}
export type ServerResponse<T> = {
ok: boolean;
statusCode: number;
statusText: string;
additionalErrorText?: string;
successResult?: T;
}
export type Scores = {
readonly kills: number;
readonly deaths: number;
@ -22,37 +14,3 @@ export type Player = {
readonly name: string;
readonly scores: Scores;
};
export async function fetchTyped<T>({url, method}: { url: URL; method: string; }): Promise<ServerResponse<T>> {
const response = await fetch(url, {method});
const result: ServerResponse<T> = {
ok: response.ok,
statusCode: response.status,
statusText: response.statusText
};
if (response.ok)
result.successResult = await response.json();
else
result.additionalErrorText = await response.text();
return result;
}
export function postPlayer(name: string) {
const url = makeApiUrl('/player');
url.searchParams.set('name', name);
return fetchTyped<string>({url, method: 'POST'});
}
export async function getMaps() {
const url = makeApiUrl('/map');
return await fetchTyped<string[]>({url, method: 'GET'});
}
export function postMaps(map: string) {
const url = makeApiUrl('/map');
url.searchParams.set('name', map);
return fetchTyped<string>({url, method: 'POST'});
}