ADD: added initial page with login
This commit is contained in:
45
frontend/src/pages/api.tsx
Normal file
45
frontend/src/pages/api.tsx
Normal file
@@ -0,0 +1,45 @@
|
||||
const API_URL = 'http://localhost:8080/api';
|
||||
|
||||
export async function login(email: string, password: string) {
|
||||
const res = await fetch(`${API_URL}/login`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ email, password }),
|
||||
});
|
||||
if (!res.ok) throw new Error('Login fehlgeschlagen');
|
||||
return res.json(); // { token: string }
|
||||
}
|
||||
|
||||
export async function fetchTournaments() {
|
||||
const res = await fetch(`${API_URL}/tournaments`);
|
||||
if (!res.ok) throw new Error('Fehler beim Laden der Turniere');
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export async function fetchTournament(id: string, token?: string) {
|
||||
const res = await fetch(`${API_URL}/tournaments/${id}`, {
|
||||
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
||||
});
|
||||
if (!res.ok) throw new Error('Fehler beim Laden des Turniers');
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export async function updateTournament(id: string, data: any, token: string) {
|
||||
const res = await fetch(`${API_URL}/tournaments/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
if (!res.ok) throw new Error('Update fehlgeschlagen');
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export async function registerTeam(id: string, team: { name: string }, token: string) {
|
||||
const res = await fetch(`${API_URL}/tournaments/${id}/register`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
|
||||
body: JSON.stringify(team),
|
||||
});
|
||||
if (!res.ok) throw new Error('Team-Anmeldung fehlgeschlagen');
|
||||
return res.json();
|
||||
}
|
||||
Reference in New Issue
Block a user