Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/pages/GamePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ function GamePage({ match }) {

const [waiting, setWaiting] = useState(false);
const [redirect, setRedirect] = useState(null);
const [selected, setSelected] = useState([]);
let [selected, setSelected] = useState([]);
// needed so that in memory 3 incorrect cards stay selected (and thus visible)
const clearSelected = useRef(false);
const [snack, setSnack] = useState({ open: false });

Expand Down Expand Up @@ -285,18 +286,20 @@ function GamePage({ match }) {
});
return;
}
setSelected((selected) => {
if (clearSelected.current) {
selected = [];
clearSelected.current = false;
}
if (selected.includes(card)) {
return gameMode === "memory" ? selected : removeCard(selected, card);
}
if (clearSelected.current) {
selected = [];
clearSelected.current = false;
}
if (selected.includes(card)) {
setSelected(
gameMode === "memory" ? selected : removeCard(selected, card)
);
} else {
const state = addCard(selected, card, gameMode, findState);
switch (state.kind) {
case "pending":
return state.cards;
setSelected(state.cards);
break;
case "set":
handleSet(state.cards);
if (volume > 0) playSuccess();
Expand All @@ -307,7 +310,8 @@ function GamePage({ match }) {
message: `Found ${formatANoun(state.setType)}`,
});
}
return [];
setSelected([]);
break;
case "error":
if (volume > 0) playFail();
if (notifications === "on") {
Expand All @@ -319,12 +323,14 @@ function GamePage({ match }) {
}
if (gameMode === "memory") {
clearSelected.current = true;
return state.cards;
setSelected(state.cards);
} else {
setSelected([]);
}
return [];
break;
// no default
}
});
}
}

function handleClear() {
Expand Down