Skip to content

Commit

Permalink
Merge pull request #29 from Infinity-Times-Two/dev
Browse files Browse the repository at this point in the history
Add created game to loadedGames and fix FITB with trailing punctuation
  • Loading branch information
Jamesllllllllll authored Apr 16, 2024
2 parents 3a607ba + 35c4f6f commit 5811a62
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
17 changes: 12 additions & 5 deletions src/app/components/challenges/FillInTheBlank.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export default function FillInTheBlankChallenge({
const [loading, setLoading] = useState<boolean>(true);

const router = useRouter();
const punctuationRegex = /^[,.!?]$/;
const endPunctuationRegex = /[,.!?]+$/;

// Filter correct and incorrect answer words from answer array
const filterWords = () => {
Expand Down Expand Up @@ -123,7 +125,10 @@ export default function FillInTheBlankChallenge({

// Get the last word of the answer
const answerArray = currentChallenge.answer.split(' ');
const lastWord = answerArray[answerArray.length - 1];
const lastWord = answerArray[answerArray.length - 1].replace(
endPunctuationRegex,
''
);

// find last index of the element containing lastWord
const findLastIndexWithMatch = (array: string[], searchWord: any) => {
Expand Down Expand Up @@ -205,8 +210,6 @@ export default function FillInTheBlankChallenge({
setAnswer(newAnswer);
};

const punctuationRegex = /^[,.!?]$/;

useEffect(() => {
filterWords();
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand All @@ -220,8 +223,12 @@ export default function FillInTheBlankChallenge({
.trim()
.toLowerCase()
.replace(/ (\,|\!|\?|\.)/g, '$1'); // removes single space before , . ! ?

if (checkAnswer === currentChallenge.answer.toLowerCase()) {
console.log(`checkAnswer: ${checkAnswer}`);
if (
checkAnswer ===
currentChallenge.answer.toLowerCase().replace(endPunctuationRegex, '')
) {
// removes trailing punctuation that is not included in answer
if (nextChallenge === currentGame.challenges.length) {
router.push(`../${currentGame.id}/win`);
} else {
Expand Down
23 changes: 21 additions & 2 deletions src/app/components/createGame/NewGameForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useState, useEffect, useContext } from 'react';
import { Game, Challenge } from '../../types/types';
import { UserContext } from '@/app/contexts/userContext';
import { SavedGamesContext } from '@/app/contexts/savedGamesContext';
import { LoadedGamesContext } from '@/app/contexts/loadedGamesContext';
import { SingleGameContext } from '@/app/contexts/singleGameContext';
import Input from '../ui/Input';
import TextArea from '../ui/TextArea';
Expand Down Expand Up @@ -63,6 +64,7 @@ export default function NewGameForm({ editGame }: { editGame?: string }) {
const [tooManyGames, setTooManyGames] = useState(false);

const { setSavedGames } = useContext(SavedGamesContext);
const { setLoadedGames } = useContext(LoadedGamesContext);
const { singleGame, setSingleGame } = useContext(SingleGameContext);

const [newGame, setNewGame] = useState<Game>(defaultGameData);
Expand Down Expand Up @@ -348,6 +350,23 @@ export default function NewGameForm({ editGame }: { editGame?: string }) {
return newGames;
});

setLoadedGames((prevGames: Game[]) => {
let newGames: Game[];
if (editGame) {
newGames = prevGames.map((game) => {
if (game.id === editGame) {
return newGame;
} else {
return game;
}
});
} else {
newGames = [...prevGames, newGame];
}
localStorage.setItem('loadedGames', JSON.stringify(newGames));
return newGames;
});

// Add to main games table in DB
// TO DO: Allow user to mark a game as public or private
if (user.id !== '') {
Expand Down Expand Up @@ -393,7 +412,7 @@ export default function NewGameForm({ editGame }: { editGame?: string }) {
localStorage.removeItem('newGameForm');
setSubmitError(false);
setSubmitErrorMessage([]);
saveForm(defaultGameData)
saveForm(defaultGameData);
setNewGame(defaultGameData);
};

Expand Down Expand Up @@ -689,7 +708,7 @@ export default function NewGameForm({ editGame }: { editGame?: string }) {
)}

{/* Show after creating game or after editing game*/}
{(singleGame?.id === newGame.id) && (newGame.id !== '') && (
{singleGame?.id === newGame.id && newGame.id !== '' && (
<Link
key={newGame.id}
href={`/game/${newGame.id}`}
Expand Down

0 comments on commit 5811a62

Please sign in to comment.