From 8f29332048ce8fb1f06148be82542832a517ec84 Mon Sep 17 00:00:00 2001
From: Ilion Beyst <ilion.beyst@gmail.com>
Date: Thu, 17 Feb 2022 17:11:16 +0100
Subject: adapt frontend to new match api

---
 web/pw-server/src/routes/index.svelte | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

(limited to 'web/pw-server/src/routes/index.svelte')

diff --git a/web/pw-server/src/routes/index.svelte b/web/pw-server/src/routes/index.svelte
index 3bab27a..46eb635 100644
--- a/web/pw-server/src/routes/index.svelte
+++ b/web/pw-server/src/routes/index.svelte
@@ -51,13 +51,35 @@
 
   async function selectMatch(matchId: string) {
     console.log("showing match " + matchId);
-    let matchLog = await loadMatch(matchId);
+    let matchLog = await getMatchLog(matchId);
     selectedMatchId = matchId;
     selectedMatchLog = matchLog;
   }
 
-  async function loadMatch(matchId: string) {
-    const res = await fetch(`/api/matches/${matchId}`, {
+  async function getMatchData(matchId: string) {
+    let response = await fetch(`/api/matches/${matchId}`, {
+      headers: {
+        "Content-Type": "application/json",
+      },
+    });
+
+    if (!response.ok) {
+      throw Error(response.statusText);
+    }
+
+    let matchData = await response.json();
+    return matchData;
+  }
+
+  async function getMatchLog(matchId: string) {
+    const matchData = await getMatchData(matchId);
+    console.log(matchData);
+    if (matchData["state"] !== "Finished") {
+      // log is not available yet
+      return null;
+    }
+
+    const res = await fetch(`/api/matches/${matchId}/log`, {
       headers: {
         "Content-Type": "application/json",
       },
-- 
cgit v1.2.3

