Skip to content

Commit d29e69b

Browse files
committed
Data loading for the word pack
1 parent 6922786 commit d29e69b

4 files changed

Lines changed: 117 additions & 17 deletions

File tree

public/fruits.txt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
APPLE
2+
APRICOT
3+
BANANA
4+
BLACKBERRY
5+
BLUEBERRY
6+
BOYSENBERRY
7+
BREADFRUIT
8+
CANTALOUPE
9+
CARAMBOLA
10+
CHERIMOYA
11+
CHERRY
12+
CLEMENTINE
13+
COCONUT
14+
CRANBERRY
15+
DATE
16+
DRAGONFRUIT
17+
DURIAN
18+
ELDERBERRY
19+
FIG
20+
GOOSEBERRY
21+
GRAPE
22+
GRAPEFRUIT
23+
GUAVA
24+
HONEYDEW
25+
JACKFRUIT
26+
JUJUBE
27+
KIWI
28+
KUMQUAT
29+
LEMON
30+
LIME
31+
LYCHEE
32+
MANGO
33+
MARIONBERRY
34+
MULBERRY
35+
NECTARINE
36+
ORANGE
37+
PAPAYA
38+
PASSIONFRUIT
39+
PEACH
40+
PEAR
41+
PERSIMMON
42+
PINEAPPLE
43+
PLANTAIN
44+
PLUM
45+
POMEGRANATE
46+
POMELO
47+
QUINCE
48+
RAMBUTAN
49+
RASPBERRY
50+
STRAWBERRY
51+
TAMARIND
52+
TANGELO
53+
TANGERINE
54+
TOMATO
55+
WATERMELON
56+
YUZU

src/App.tsx

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,44 @@
1-
import React from "react";
1+
import React, { useEffect } from "react";
22

33
import styles from "./App.module.css";
44
import useAppState from "./useAppState";
55

66
function App() {
77
const [state, dispatch] = useAppState();
88

9+
useEffect(() => {
10+
fetch("fruits.txt")
11+
.then((response) => response.text())
12+
.then((text) => {
13+
setTimeout(
14+
() =>
15+
dispatch({
16+
type: "load-data",
17+
wordPack: text
18+
.split("\n")
19+
.map((word) => word.toUpperCase().trim())
20+
.filter(Boolean),
21+
}),
22+
3000,
23+
);
24+
});
25+
}, []);
26+
927
let content = null;
1028
switch (state.phase) {
1129
case "pre-game": {
30+
if (state.wordPack == null) {
31+
content = <>Loading data...</>;
32+
break;
33+
}
34+
1235
content = (
13-
<button onClick={() => dispatch({ type: "start-game" })}>
14-
Begin new game
15-
</button>
36+
<>
37+
<div>Word pack is ready with {state.wordPack.length} words!</div>
38+
<button onClick={() => dispatch({ type: "start-game" })}>
39+
Begin new game
40+
</button>
41+
</>
1642
);
1743
break;
1844
}

src/getRandomWord.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/useAppState.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,62 @@
11
import { useReducer, type ActionDispatch } from "react";
22

3-
import getRandomWord from "./getRandomWord";
3+
import getRandomElement from "./util/getRandomElement";
44

5-
export type State =
5+
export type State = Readonly<
66
| {
77
phase: "pre-game";
8+
wordPack: readonly string[] | null;
89
}
910
| {
1011
phase: "in-game";
1112
goal: string;
1213
guess: string;
14+
wordPack: readonly string[];
1315
}
1416
| {
1517
phase: "post-game";
1618
goal: string;
17-
};
19+
wordPack: readonly string[];
20+
}
21+
>;
1822

1923
export function getInitialState(): State {
20-
return { phase: "pre-game" };
24+
return { phase: "pre-game", wordPack: null };
2125
}
2226

2327
export type Action =
28+
| { type: "load-data"; wordPack: readonly string[] }
2429
| { type: "start-game" }
2530
| { type: "update-guess"; newGuess: string };
2631

2732
export function reducer(state: State, action: Action): State {
2833
switch (action.type) {
34+
case "load-data": {
35+
// No-op if not in pre-game phase.
36+
if (state.phase !== "pre-game") {
37+
return state;
38+
}
39+
40+
return { ...state, wordPack: action.wordPack };
41+
}
42+
2943
case "start-game": {
3044
// No-op if already in a game.
3145
if (state.phase === "in-game") {
3246
return state;
3347
}
3448

49+
// No-op if data is not loaded.
50+
const { wordPack } = state;
51+
if (wordPack == null) {
52+
return state;
53+
}
54+
3555
return {
3656
phase: "in-game",
37-
goal: getRandomWord(),
57+
goal: getRandomElement(wordPack),
3858
guess: "",
59+
wordPack,
3960
};
4061
}
4162

@@ -46,7 +67,11 @@ export function reducer(state: State, action: Action): State {
4667
}
4768

4869
if (action.newGuess === state.goal) {
49-
return { phase: "post-game", goal: state.goal };
70+
return {
71+
phase: "post-game",
72+
goal: state.goal,
73+
wordPack: state.wordPack,
74+
};
5075
}
5176

5277
return { ...state, guess: action.newGuess };

0 commit comments

Comments
 (0)