Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-hu committed Nov 21, 2023
1 parent a0713f2 commit d6a84e3
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Connections/ConnectionsRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,28 @@ function ConnectionsRouter() {
const { nytConnections, loadedConnections } = useContext(ConnectionsContext);
const searchParams = new URLSearchParams(location.search);

const shouldLoadSavedGame = searchParams.get('game')?.split('-').length === 3;
const gameId = searchParams.get('game');
const shouldLoadSavedGame = gameId?.split('-').length === 3;
const [savedGame, setSavedGame] = useState<ConnectionsGame | null>(null);
const [loadingSavedGame, setLoadingSavedGame] = useState(shouldLoadSavedGame);

let game: ConnectionsGame = { categories: [] };
const urlGame = decodeCategories(searchParams.get('categories') || searchParams.get('game'));
const urlGame = decodeCategories(searchParams.get('categories') || gameId);

useEffect(() => {
if (!loadingSavedGame) {
if (!loadingSavedGame || !gameId) {
return;
}

loadGame(searchParams.get('game')!)
loadGame(gameId)
.then((game) => {
if (game) {
setSavedGame(game);
}
})
.finally(() => setLoadingSavedGame(false));
}, [loadingSavedGame, searchParams]);
}, [loadingSavedGame, gameId]);

let game: ConnectionsGame = { categories: [] };

if (urlGame) {
game = urlGame;
Expand Down

0 comments on commit d6a84e3

Please sign in to comment.