From fedeeb9b824514a8a23e5a595fe658b8e5aa99e6 Mon Sep 17 00:00:00 2001 From: Thomas Gautvedt Date: Sun, 10 Sep 2023 18:46:55 +0200 Subject: [PATCH] fix(wordlist): store settings in state \o,0/ --- src/App.tsx | 6 ++++-- src/pages/game/Game.tsx | 10 ++++++++-- src/pages/game/components/Stats.tsx | 10 ++++++++-- src/pages/settings/Settings.tsx | 10 ++++++---- src/wordListContainer/wordListContainer.ts | 5 ++--- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 2e0144f..dde913a 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -3,9 +3,11 @@ import React, { useState } from 'react'; import { Layout } from './components'; import { Game, Settings } from './pages'; import { Pages } from './types'; +import { getSettingsFromLocalStorage } from './utilities'; export const App = () => { const [page, setPage] = useState(Pages.Game); + const [settings, setSettings] = useState(getSettingsFromLocalStorage()); const AppWrapper = ({ children }: { children: React.ReactNode }) => ( @@ -17,14 +19,14 @@ export const App = () => { case Pages.Settings: return ( - setPage(Pages.Game)} /> + setPage(Pages.Game)} settings={settings} setSettings={setSettings} /> ); case Pages.Game: default: return ( - + ); } diff --git a/src/pages/game/Game.tsx b/src/pages/game/Game.tsx index aa3f497..5a9c55c 100644 --- a/src/pages/game/Game.tsx +++ b/src/pages/game/Game.tsx @@ -8,7 +8,13 @@ import { GuessesStateProps, PreviousWordStateProps } from './stateProps'; const wordListContainer = new WordListContainer(); -export const Game = () => { +interface GameProps { + settings: string[]; +} + +export const Game = (props: GameProps) => { + const { settings } = props; + const inputRef = React.useRef(null); // I don't really know if this works, but it is set after the wordlist has been filtered @@ -36,7 +42,7 @@ export const Game = () => { useEffect(() => { if (!gameStarted) { - wordListContainer.applySettings().then(() => { + wordListContainer.applySettings(settings).then(() => { // Make the game playable after the wordlist has been loaded with applied filters setReady(true); setCurrentWord(wordListContainer.getRandomWord()); diff --git a/src/pages/game/components/Stats.tsx b/src/pages/game/components/Stats.tsx index ecad287..26ec4fb 100644 --- a/src/pages/game/components/Stats.tsx +++ b/src/pages/game/components/Stats.tsx @@ -18,8 +18,14 @@ interface StatsProps { } // Avoiding 0 division here (if that is even possible?) -const calculatePerMinute = (correct: number, clockDuration: number): string => - Math.round(correct / (clockDuration === 0 ? 0.0001 : clockDuration / (1000 * 60))).toLocaleString(); +const calculatePerMinute = (correct: number, clockDuration: number): string => { + const value = Math.round(correct / (clockDuration === 0 ? 0.0001 : clockDuration / (1000 * 60))); + if (value <= 0) { + return '0'; + } + + return value.toLocaleString(); +}; export const Stats = ({ wordListSize, clockDuration, guesses }: StatsProps) => (
diff --git a/src/pages/settings/Settings.tsx b/src/pages/settings/Settings.tsx index 41e2a55..eac09f5 100644 --- a/src/pages/settings/Settings.tsx +++ b/src/pages/settings/Settings.tsx @@ -1,11 +1,13 @@ -import React, { useState } from 'react'; +import React from 'react'; import { LETTERS, NUMBERS, SIGNS } from '../../symbols/keys'; -import { getSettingsFromLocalStorage, saveSettingsInLocalStorage } from '../../utilities'; +import { saveSettingsInLocalStorage } from '../../utilities'; import { SettingsSection } from './components'; interface SettingsProps { goToGame: () => void; + settings: string[]; + setSettings: React.Dispatch>; } const numberOfSymbolsSelectedForSection = (symbols: string[], settings: string[]): string => @@ -26,8 +28,8 @@ const sections: { symbols: string[]; heading: string }[] = [ }, ]; -export const Settings = ({ goToGame }: SettingsProps) => { - const [settings, setSettings] = useState(getSettingsFromLocalStorage()); +export const Settings = (props: SettingsProps) => { + const { settings, setSettings, goToGame } = props; return (
diff --git a/src/wordListContainer/wordListContainer.ts b/src/wordListContainer/wordListContainer.ts index dc23f04..1aefcc6 100644 --- a/src/wordListContainer/wordListContainer.ts +++ b/src/wordListContainer/wordListContainer.ts @@ -1,4 +1,3 @@ -import { getSettingsFromLocalStorage } from '../utilities'; import wordsPayload from './words.json'; // TODO @@ -15,8 +14,8 @@ export class WordListContainer { this.lookup = wordsPayload.words; } - async applySettings() { - this.settings = getSettingsFromLocalStorage(); + async applySettings(settings: string[]) { + this.settings = settings; // There has got to be a better way of doing this lmao for (const word of this.lookup) {