From 6d29bfdf65a1136daec09456c4f04bde8304e359 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Tue, 1 Mar 2022 20:44:32 -0800 Subject: [PATCH] Fix game state on refresh. There was accidentally a second layer of callback here. I'm not sure why it worked. I think the lambda being returned was being used as the cleanup function? --- client/src/App.tsx | 2 +- client/src/api/useInitialGameState.tsx | 44 ++++++++++++-------------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/client/src/App.tsx b/client/src/App.tsx index ba50bc42..54bf3f0c 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -2,7 +2,7 @@ import "./App.css"; import { LatLng } from "leaflet"; import LiberationMap from "./components/liberationmap"; -import { useInitialGameState } from "./api/useInitialGameState"; +import useInitialGameState from "./api/useInitialGameState"; function App() { const mapCenter: LatLng = new LatLng(25.58, 54.9); diff --git a/client/src/api/useInitialGameState.tsx b/client/src/api/useInitialGameState.tsx index c400b865..dac752f2 100644 --- a/client/src/api/useInitialGameState.tsx +++ b/client/src/api/useInitialGameState.tsx @@ -10,30 +10,28 @@ import { useEffect } from "react"; // are smart enough to only initialize once which get called in the components // that use them rather than forcibly loading the whole game in the root // component. -export function useInitialGameState() { +export const useInitialGameState = () => { const dispatch = useAppDispatch(); useEffect(() => { - return () => { - backend - .get("/control-points") - .catch((error) => - console.log(`Error fetching control points: ${error}`) - ) - .then((response) => { - if (response != null) { - dispatch(setControlPoints(response.data as ControlPoint[])); + backend + .get("/control-points") + .catch((error) => console.log(`Error fetching control points: ${error}`)) + .then((response) => { + if (response != null) { + dispatch(setControlPoints(response.data as ControlPoint[])); + } + }); + backend + .get("/flights?with_waypoints=true") + .catch((error) => console.log(`Error fetching flights: ${error}`)) + .then((response) => { + if (response != null) { + for (const flight of response.data) { + dispatch(registerFlight(flight as Flight)); } - }); - backend - .get("/flights?with_waypoints=true") - .catch((error) => console.log(`Error fetching flights: ${error}`)) - .then((response) => { - if (response != null) { - for (const flight of response.data) { - dispatch(registerFlight(flight as Flight)); - } - } - }); - }; + } + }); }); -} +}; + +export default useInitialGameState;