فهرست منبع

Rejoin now safely checks for the existence of the game

Kirk Trombley 5 سال پیش
والد
کامیت
7fe82aa131
2فایلهای تغییر یافته به همراه36 افزوده شده و 12 حذف شده
  1. 14 7
      client/src/components/screens/HomePage/HomePage.jsx
  2. 22 5
      client/src/domain/localStorageMethods.js

+ 14 - 7
client/src/components/screens/HomePage/HomePage.jsx

@@ -1,4 +1,4 @@
-import React from 'react';
+import React, { useState, useEffect } from 'react';
 import { dispatch } from '../../../domain/gameStore';
 import { hasSavedGameInfo } from '../../../domain/localStorageMethods';
 import DelayedButton from '../../util/DelayedButton';
@@ -14,9 +14,16 @@ const Rejoin = () => (
   </div>
 );
 
-export default () => (
-  <div className={styles.page}>
-    <GameCreationForm afterCreate={(gameId) => dispatch.goToLobby(gameId)} />
-    {hasSavedGameInfo() && <Rejoin />}
-  </div>
-);
+export default () => {
+  const [hasSavedInfo, setHasSavedInfo] = useState(false);
+  useEffect(() => {
+    hasSavedGameInfo().then(setHasSavedInfo)
+  }, []);
+
+  return (
+    <div className={styles.page}>
+      <GameCreationForm afterCreate={(gameId) => dispatch.goToLobby(gameId)} />
+      {hasSavedInfo && <Rejoin />}
+    </div>
+  );
+}

+ 22 - 5
client/src/domain/localStorageMethods.js

@@ -1,3 +1,4 @@
+import { getCurrentRound } from "./apiMethods";
 
 const localStorageGameId = "terrassumptions:gameId";
 const localStoragePlayerName = "terrassumptions:playerName";
@@ -11,11 +12,27 @@ const localStoragePanoLng = "terrassumptions:pano:lng";
 const localStoragePanoHeading = "terrassumptions:pano:heading";
 const localStoragePanoPitch = "terrassumptions:pano:pitch";
 
-export const hasSavedGameInfo = () => (
-  localStorage.getItem(localStorageGameId) !== null &&
-  localStorage.getItem(localStoragePlayerName) !== null &&
-  localStorage.getItem(localStoragePlayerId) !== null
-)
+export const hasSavedGameInfo = async () => {
+  const gameId = localStorage.getItem(localStorageGameId);
+  if (gameId === null) {
+    return false;
+  }
+  const playerName = localStorage.getItem(localStoragePlayerName);
+  if (playerName === null) {
+    return false;
+  }
+  const playerId = localStorage.getItem(localStoragePlayerId);
+  if (playerId === null) {
+    return false;
+  }
+  
+  try {
+    await getCurrentRound(gameId, playerId);
+    return true;
+  } catch (_) {
+    return false;
+  }
+}
 
 export const saveGameInfoToLocalStorage = (gameId, playerName, playerId) => {
   localStorage.setItem(localStorageGameId, gameId);