Skip to content

Commit

Permalink
Merge pull request #44 from skedwards88/move-validity-indication
Browse files Browse the repository at this point in the history
Move validity opacity setting into separate state
  • Loading branch information
skedwards88 authored Aug 29, 2024
2 parents 07ef46c + dc66e2f commit 4871c0c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
25 changes: 19 additions & 6 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export default function App() {
getInitialState(savedDisplay, hasVisited),
);

// Determine the opacity for the validity indicator
const savedValidityOpacity =
JSON.parse(localStorage.getItem("crossjigValidityOpacity")) ?? 0.15;
const [validityOpacity, setValidityOpacity] =
React.useState(savedValidityOpacity);

// Set up states that will be used by the handleAppInstalled and handleBeforeInstallPrompt listeners
const [installPromptEvent, setInstallPromptEvent] = React.useState();
const [showInstallButton, setShowInstallButton] = React.useState(true);
Expand Down Expand Up @@ -166,6 +172,13 @@ export default function App() {
window.localStorage.setItem("crossjigDisplay", JSON.stringify(display));
}, [display]);

React.useEffect(() => {
window.localStorage.setItem(
"crossjigValidityOpacity",
JSON.stringify(validityOpacity),
);
}, [validityOpacity]);

React.useEffect(() => {
window.localStorage.setItem("crossjigState", JSON.stringify(gameState));
}, [gameState]);
Expand Down Expand Up @@ -197,6 +210,8 @@ export default function App() {
setDisplay={setDisplay}
dispatchGameState={dispatchGameState}
gameState={gameState}
setValidityOpacity={setValidityOpacity}
originalValidityOpacity={validityOpacity}
/>
);

Expand Down Expand Up @@ -224,11 +239,8 @@ export default function App() {
</div>
<Game
dispatchGameState={dailyDispatchGameState}
gameState={{
...dailyGameState,
// todo in the settings, pass in the dailyDispatcher too and update the validityOpacity in the daily state as well. then remove this line.
validityOpacity: gameState.validityOpacity,
}}
gameState={dailyGameState}
validityOpacity={validityOpacity}
setDisplay={setDisplay}
></Game>
</div>
Expand Down Expand Up @@ -309,7 +321,7 @@ export default function App() {
</div>
<CustomCreation
dispatchCustomState={dispatchCustomState}
validityOpacity={gameState.validityOpacity}
validityOpacity={validityOpacity}
customState={customState}
setDisplay={setDisplay}
></CustomCreation>
Expand Down Expand Up @@ -362,6 +374,7 @@ export default function App() {
<Game
dispatchGameState={dispatchGameState}
gameState={gameState}
validityOpacity={validityOpacity}
></Game>
</div>
);
Expand Down
6 changes: 3 additions & 3 deletions src/components/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Result from "./Result";
import Board from "./Board";
import DragGroup from "./DragGroup";

function Game({dispatchGameState, gameState, setDisplay}) {
function Game({dispatchGameState, gameState, setDisplay, validityOpacity}) {
// dragCount ensures a different key each time, so a fresh DragGroup is mounted even if there's
// no render between one drag ending and the next one starting.
const dragGroup = gameState.dragState ? (
Expand All @@ -20,7 +20,7 @@ function Game({dispatchGameState, gameState, setDisplay}) {
style={{
"--grid-rows": gameState.gridSize,
"--grid-columns": gameState.gridSize,
"--validity-opacity": gameState.validityOpacity,
"--validity-opacity": validityOpacity,
}}
>
<Board
Expand All @@ -30,7 +30,7 @@ function Game({dispatchGameState, gameState, setDisplay}) {
dragDestination={gameState.dragState?.destination}
gameIsSolved={gameState.gameIsSolved}
dispatchGameState={dispatchGameState}
indicateValidity={gameState.validityOpacity > 0}
indicateValidity={validityOpacity > 0}
></Board>
{gameState.allPiecesAreUsed ? (
<Result
Expand Down
20 changes: 12 additions & 8 deletions src/components/Settings.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import React from "react";

export default function Settings({setDisplay, dispatchGameState, gameState}) {
export default function Settings({
setDisplay,
dispatchGameState,
gameState,
setValidityOpacity,
originalValidityOpacity,
}) {
function handleNewGame(event) {
event.preventDefault();
const newNumLetters = event.target.elements.numLetters.value;
const newValidityOpacity =
event.target.elements.validityOpacity.value / 100;

setValidityOpacity(newValidityOpacity);

dispatchGameState({
action: "newGame",
numLetters: newNumLetters,
validityOpacity: newValidityOpacity,
});
setDisplay("game");
}
Expand Down Expand Up @@ -46,7 +53,7 @@ export default function Settings({setDisplay, dispatchGameState, gameState}) {
<div className="setting-info">{`Valid words are indicated with a strikethrough. This controls the brightness of the strikethrough.`}</div>
<div
id="validity-example"
style={{"--validity-opacity": gameState.validityOpacity}}
style={{"--validity-opacity": originalValidityOpacity}}
>
EXAMPLE
</div>
Expand All @@ -61,13 +68,10 @@ export default function Settings({setDisplay, dispatchGameState, gameState}) {
type="range"
min={0}
max={100}
defaultValue={gameState.validityOpacity * 100 || 15}
defaultValue={originalValidityOpacity * 100 || 15}
onChange={(event) => {
const newValidityOpacity = event.target.value / 100;
dispatchGameState({
action: "changeValidityOpacity",
newValidityOpacity,
});
setValidityOpacity(newValidityOpacity);
}}
/>
<div id="validityOpacity-info" className="setting-info">
Expand Down
2 changes: 0 additions & 2 deletions src/logic/gameInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function validateSavedState(savedState) {

export function gameInit({
numLetters,
validityOpacity = 0.15,
useSaved = true,
isDaily = false,
isCustom = false,
Expand Down Expand Up @@ -157,7 +156,6 @@ export function gameInit({
hintTally: 0,
dragCount: 0,
dragState: undefined,
validityOpacity,
isCustom,
};
}
5 changes: 0 additions & 5 deletions src/logic/gameReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,6 @@ export function gameReducer(currentGameState, payload) {
seed: payload.representativeString,
isCustom: true,
});
} else if (payload.action === "changeValidityOpacity") {
return {
...currentGameState,
validityOpacity: payload.newValidityOpacity,
};
} else if (payload.action === "getHint") {
sendAnalytics("hint");

Expand Down

0 comments on commit 4871c0c

Please sign in to comment.