From 695b5b149f2bd8e51fdd6d281cad429e0d2682e2 Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Mon, 30 Jun 2025 23:48:39 +0700 Subject: [PATCH] add async await --- src/components/AdminWindow.vue | 19 +++++------ src/components/client.ts | 61 +++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/components/AdminWindow.vue b/src/components/AdminWindow.vue index 5064334..7515984 100644 --- a/src/components/AdminWindow.vue +++ b/src/components/AdminWindow.vue @@ -15,12 +15,9 @@ const game = ref() const teams = ref({ teams: [] }) const teamName = ref("") -function addTeam() { - apiAddTeam(teamName) -} - -function getGame() { - apiGetGame(game, gameState) +async function addTeam() { + await apiAddTeam(teamName.value) + teamName.value = "" } function startGame() { @@ -32,12 +29,12 @@ function stopGame() { } let intervalId = 0 -onMounted(() => { - apiGetTeams(teams) +onMounted(async () => { + teams.value = await apiGetTeams() - intervalId = setInterval(() => { - apiGetTeams(teams) - getGame() + intervalId = setInterval(async () => { + teams.value = await apiGetTeams() + apiGetGame(game, gameState) }, 2000); router.beforeEach((to, from, next) => { diff --git a/src/components/client.ts b/src/components/client.ts index c7cb7f5..7b68d9a 100644 --- a/src/components/client.ts +++ b/src/components/client.ts @@ -3,33 +3,40 @@ import { getApiUrl } from './net'; import type { Game, Teams } from './models'; import { downloadData } from './qr'; -export const apiGetTeams = (teams: Ref) => { - fetch( - getApiUrl("/teams") - ) - .then(response => response.json()) - .then(data => { - teams.value = data - }) - .catch(error => { - console.error('Ошибка:', error) - }); +export const apiGetTeams = async (): Promise => { + try { + const response = await fetch( + getApiUrl("/teams") + ) + if (!response.ok) { + throw new Error(`http error status: ${response.status}`) + } + return await response.json() + } catch (error) { + console.error('[apiGetTeams] error:', error) + throw error + } } -export const apiAddTeam = (teamName: Ref) => { - fetch( - getApiUrl("/teams"), - { - method: "POST", - body: JSON.stringify({ - "teams": [{ "name": teamName.value }] - }) +export const apiAddTeam = async (teamName: string) => { + try { + const response = await fetch( + getApiUrl("/teams"), + { + method: "POST", + body: JSON.stringify({ + "teams": [{ "name": teamName }] + }) + } + ) + if (!response.ok) { + throw new Error(`http error status: ${response.status}`) } - ) - .then(() => { teamName.value = "" }) - .catch(error => { - console.error('Ошибка:', error) - }); + return await response.json() + } catch (error) { + console.error('[apiAddTeam] error:', error) + throw error + } } export const apiGetGame = (game: Ref, gameState: Ref) => { @@ -71,9 +78,9 @@ export const apiStopGame = (gameState: Ref) => { getApiUrl("/game/stop"), { method: "POST" } ) - .catch(error => { - console.error('Ошибка:', error) - }); + .catch(error => { + console.error('Ошибка:', error) + }); }