Skip to content

Commit 1fb4b6d

Browse files
committed
fixed nulls
1 parent e08c8db commit 1fb4b6d

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

jam-25/src/providers/GameDataProvider.jsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ const GameDataProvider = ({ children }) => {
110110
}, [gridSizeY, gridSizeX, tileLibrary, funds, target, bonusSpaces, round, inventory, jokers, maxJokers, gameStarted]);
111111

112112
const savedGame = useCallback(() => {
113-
const data = JSON.parse(localStorage.getItem('gameData'));
114-
data.inventory = data.inventory.map((i) => <InventoryItem key={i.id} item={i} />);
115-
data.jokers = data.jokers.map((j) => <Joker joker={j} id={j.id} />);
113+
const data = JSON.parse(localStorage.getItem('gameData')) ?? {};
114+
data.inventory = data?.inventory?.map((i) => <InventoryItem key={i.id} item={i} />) ?? [];
115+
data.jokers = data?.jokers?.map((j) => <Joker joker={j} id={j.id} />) ?? [];
116116
return data;
117117
}, []);
118118

@@ -123,21 +123,21 @@ const GameDataProvider = ({ children }) => {
123123
const loadGame = useCallback(() => {
124124
const data = savedGame();
125125
if (data) {
126-
setGridSizeY(data.gridSizeY);
127-
setGridSizeX(data.gridSizeX);
128-
setTileLibrary(data.tileLibrary);
129-
setFunds(data.funds);
130-
setTarget(data.target);
131-
setBonusSpaces(data.bonusSpaces);
132-
setRound(data.round);
133-
setInventory(data.inventory);
134-
setJokers(data.jokers);
135-
setMaxJokers(data.maxJokers);
126+
setGridSizeY(data.gridSizeY ?? 7);
127+
setGridSizeX(data.gridSizeX ?? 7);
128+
setTileLibrary(data.tileLibrary ?? getStarterLetters());
129+
setFunds(data.funds ?? 3);
130+
setTarget(data.target ?? calcTarget(0));
131+
setBonusSpaces(data.bonusSpaces ?? [{ id: `${Math.floor((gridSizeY ?? 7) / 2)},${Math.floor((gridSizeX ?? 7) / 2)}`, bonus: 'BDW' }]);
132+
setRound(data.round ?? 0);
133+
setInventory(data.inventory ?? []);
134+
setJokers(data.jokers ?? []);
135+
setMaxJokers(data.maxJokers ?? 5);
136136
setTimeout(() => {
137137
setGameStarted(true);
138138
}, 500);
139139
}
140-
}, [savedGame]);
140+
}, [gridSizeX, gridSizeY, savedGame]);
141141

142142
const value = useMemo(() => {
143143
return {

0 commit comments

Comments
 (0)