diff --git a/src/components/AdminWindow.vue b/src/components/AdminWindow.vue index 084a798..0510bbb 100644 --- a/src/components/AdminWindow.vue +++ b/src/components/AdminWindow.vue @@ -3,20 +3,14 @@ import { onMounted, ref } from 'vue'; import { getApiUrl } from './net'; import router from '@/router'; import VueQrcode from '@chenfengyuan/vue-qrcode'; -import type { Teams } from './models'; -import { getTeams } from './client'; - -type Game = { - state: string - startAt: string - endAt: string -} +import type { Game, Teams } from './models'; +import { apiGetTeams, apiAddTeam, apiGetGame, apiStartGame, apiStopGame } from './client'; const qrurl = ref("-") const qrteam = ref("-") const gameState = ref("") -const game = ref() +const game = ref() const teams = ref({ teams: [] }) @@ -38,19 +32,7 @@ function gaveApplication(teamId: number, id: number) { const teamName = ref("") function addTeam() { - fetch( - getApiUrl("/teams"), - { - method: "POST", - body: JSON.stringify({ - "teams": [{ "name": teamName.value }] - }) - } - ) - .then(() => { teamName.value = "" }) - .catch(error => { - console.error('Ошибка:', error) - }); + apiAddTeam(teamName) } interface QROptions { @@ -71,47 +53,15 @@ const qrOptions = ref({ }); function getGame() { - fetch( - getApiUrl("/game") - ) - .then(response => response.json()) - .then(data => { - game.value = data - if (data.state === "NEW") { - gameState.value = "Игра ещё не началась" - } - if (data.state === "RUN") { - gameState.value = "Игра идет с " + game.value?.startAt.substring(11) - } - if (data.state === "STOP") { - gameState.value = "Игра остановлена " + game.value?.startAt.substring(11) + " - " + game.value?.endAt.substring(11) - } - }) - .catch(error => { - console.error('Ошибка:', error) - }); + apiGetGame(game, gameState) } function startGame() { - gameState.value = "Загрузка..." - fetch( - getApiUrl("/game/start"), - { method: "POST" } - ) - .catch(error => { - console.error('Ошибка:', error) - }); + apiStartGame(gameState) } function stopGame() { - gameState.value = "Загрузка..." - fetch( - getApiUrl("/game/stop"), - { method: "POST" } - ) - .catch(error => { - console.error('Ошибка:', error) - }); + apiStopGame(gameState) } function base64ToBytes(base64: string): Uint8Array { @@ -149,10 +99,10 @@ function downloadFile(bytes: Uint8Array, fileName: string, mimeType: string) { let intervalId = 0 onMounted(() => { - getTeams(teams) + apiGetTeams(teams) intervalId = setInterval(() => { - getTeams(teams) + apiGetTeams(teams) getGame() }, 2000); diff --git a/src/components/client.ts b/src/components/client.ts index 4ea4fdc..99047a1 100644 --- a/src/components/client.ts +++ b/src/components/client.ts @@ -1,8 +1,8 @@ import type { Ref } from 'vue'; import { getApiUrl } from './net'; -import type { Teams } from './models'; +import type { Game, Teams } from './models'; -export const getTeams = (teams : Ref) => { +export const apiGetTeams = (teams: Ref) => { fetch( getApiUrl("/teams") ) @@ -14,3 +14,63 @@ export const getTeams = (teams : Ref) => { console.error('Ошибка:', error) }); } + +export const apiAddTeam = (teamName: Ref) => { + fetch( + getApiUrl("/teams"), + { + method: "POST", + body: JSON.stringify({ + "teams": [{ "name": teamName.value }] + }) + } + ) + .then(() => { teamName.value = "" }) + .catch(error => { + console.error('Ошибка:', error) + }); +} + +export const apiGetGame = (game: Ref, gameState: Ref) => { + fetch( + getApiUrl("/game") + ) + .then(response => response.json()) + .then(data => { + game.value = data + if (data.state === "NEW") { + gameState.value = "Игра ещё не началась" + } + if (data.state === "RUN") { + gameState.value = "Игра идет с " + game.value?.startAt.substring(11) + } + if (data.state === "STOP") { + gameState.value = "Игра остановлена " + game.value?.startAt.substring(11) + " - " + game.value?.endAt.substring(11) + } + }) + .catch(error => { + console.error('Ошибка:', error) + }); +} + +export const apiStartGame = (gameState: Ref) => { + gameState.value = "Загрузка..." + fetch( + getApiUrl("/game/start"), + { method: "POST" } + ) + .catch(error => { + console.error('Ошибка:', error) + }); +} + +export const apiStopGame = (gameState: Ref) => { + gameState.value = "Загрузка..." + fetch( + getApiUrl("/game/stop"), + { method: "POST" } + ) + .catch(error => { + console.error('Ошибка:', error) + }); +} diff --git a/src/components/models.ts b/src/components/models.ts index 13da626..1611d5a 100644 --- a/src/components/models.ts +++ b/src/components/models.ts @@ -15,3 +15,9 @@ export type Team = { export type Teams = { teams: Team[] } + +export type Game = { + state: string + startAt: string + endAt: string +}