From bd71eee8fd614f52fb116528e0df52ff748006a9 Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Tue, 1 Jul 2025 01:03:43 +0700 Subject: [PATCH] add first custom component --- src/components/AdminWindow.vue | 44 +++++----- src/components/TeamQRCode.vue | 28 +++++++ src/components/client.ts | 144 +++++++++++++++++---------------- src/components/net.ts | 5 -- src/components/qr.ts | 4 +- 5 files changed, 129 insertions(+), 96 deletions(-) create mode 100644 src/components/TeamQRCode.vue delete mode 100644 src/components/net.ts diff --git a/src/components/AdminWindow.vue b/src/components/AdminWindow.vue index 7515984..61704dd 100644 --- a/src/components/AdminWindow.vue +++ b/src/components/AdminWindow.vue @@ -1,10 +1,9 @@ + + + + diff --git a/src/components/client.ts b/src/components/client.ts index 7b68d9a..515f903 100644 --- a/src/components/client.ts +++ b/src/components/client.ts @@ -1,5 +1,3 @@ -import type { Ref } from 'vue'; -import { getApiUrl } from './net'; import type { Game, Teams } from './models'; import { downloadData } from './qr'; @@ -32,83 +30,93 @@ export const apiAddTeam = async (teamName: string) => { if (!response.ok) { throw new Error(`http error status: ${response.status}`) } - return await response.json() } catch (error) { console.error('[apiAddTeam] error:', error) throw 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 = "Игра ещё не началась" +export const apiGetGame = async (): Promise => { + try { + const response = await fetch( + getApiUrl("/game") + ) + if (!response.ok) { + throw new Error(`http error status: ${response.status}`) } - 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) - }); + return await response.json() + } catch (error) { + console.error('[apiGetGame] error:', error) + throw 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) - }); -} - - -export const apiGaveApplication = (teamId: number, id: number) => { - fetch( - getApiUrl("/teams/" + teamId + "/applications"), - { - method: "POST", - body: JSON.stringify({ - "applications": [{ "id": id }] - }) +export const apiStartGame = async () => { + try { + const response = await fetch( + getApiUrl("/game/start"), + { method: "POST" } + ) + if (!response.ok) { + throw new Error(`http error status: ${response.status}`) } - ) - .then(() => { }) - .catch(error => { - console.error('Ошибка:', error) - }); + } catch (error) { + console.error('[apiStartGame] error:', error) + throw error + } } -export const apiDownloadQrCodesFile = () => { - fetch( - getApiUrl("/teams/pdf") - ) - .then(response => response.json()) - .then(data => { +export const apiStopGame = async () => { + try { + const response = await fetch( + getApiUrl("/game/stop"), + { method: "POST" } + ) + if (!response.ok) { + throw new Error(`http error status: ${response.status}`) + } + } catch (error) { + console.error('[apiStopGame] error:', error) + throw error + } +} + +export const apiGaveApplication = async (teamId: number, id: number) => { + try { + const response = await fetch( + getApiUrl("/teams/" + teamId + "/applications"), + { + method: "POST", + body: JSON.stringify({ + "applications": [{ "id": id }] + }) + } + ) + if (!response.ok) { + throw new Error(`http error status: ${response.status}`) + } + } catch (error) { + console.error('[apiGaveApplication] error:', error) + throw error + } +} + +export const apiDownloadQrCodesFile = async () => { + try { + const response = await fetch( + getApiUrl("/teams/pdf") + ) + if (!response.ok) { + throw new Error(`http error status: ${response.status}`) + } + const data = await response.json(); downloadData(data.result) - }) - .catch(error => { - console.error('Ошибка:', error) - }); + } catch (error) { + console.error('[apiDownloadQrCodesFile] error:', error) + throw error + } +} + +function getApiUrl(path: string) { + return "http://" + window.location.host.split(":")[0] + ":8090" + path } diff --git a/src/components/net.ts b/src/components/net.ts deleted file mode 100644 index d8f1ce0..0000000 --- a/src/components/net.ts +++ /dev/null @@ -1,5 +0,0 @@ -export function getApiUrl(path: string) { - const url = "http://" + window.location.host.split(":")[0] + ":8090" + path - console.log(url) - return url -} diff --git a/src/components/qr.ts b/src/components/qr.ts index a845025..103211c 100644 --- a/src/components/qr.ts +++ b/src/components/qr.ts @@ -23,7 +23,7 @@ export const downloadData = (data: string) => { downloadFile(b, 'teams_qr_code.pdf', 'application/pdf;teams_qr_code.pdf') } -export const base64ToBytes = (base64: string): Uint8Array => { +const base64ToBytes = (base64: string): Uint8Array => { const binaryString = atob(base64); const bytes = new Uint8Array(binaryString.length); for (let i = 0; i < binaryString.length; i++) { @@ -32,7 +32,7 @@ export const base64ToBytes = (base64: string): Uint8Array => { return bytes; } -export const downloadFile = (bytes: Uint8Array, fileName: string, mimeType: string) => { +const downloadFile = (bytes: Uint8Array, fileName: string, mimeType: string) => { const blob = new Blob([bytes], { type: mimeType }); const url = URL.createObjectURL(blob); const link = document.createElement('a');