From d319632e0da0e3c7df0f751232255d05f5fd2550 Mon Sep 17 00:00:00 2001 From: Arnold Bhebhe Date: Tue, 14 Mar 2023 10:56:33 -0500 Subject: [PATCH] fix infinite renders, and update --- elevateyou/src/components/CardControls.jsx | 25 ++-- elevateyou/src/components/NumbersCard.jsx | 110 +++++++++++++++--- .../src/components/NumbersFlashcards.jsx | 30 +++-- elevateyou/src/components/RestartModal.jsx | 41 ++++--- elevateyou/src/styles/RestartModal.css | 18 +++ elevateyou/src/utils/fetchNumbersFacts.jsx | 1 + 6 files changed, 164 insertions(+), 61 deletions(-) create mode 100644 elevateyou/src/styles/RestartModal.css diff --git a/elevateyou/src/components/CardControls.jsx b/elevateyou/src/components/CardControls.jsx index 590ef0c..f28c6e4 100644 --- a/elevateyou/src/components/CardControls.jsx +++ b/elevateyou/src/components/CardControls.jsx @@ -5,18 +5,20 @@ import { BsShuffle, BsArrowRepeat, } from "react-icons/bs"; +import RestartModal from "./RestartModal"; -const CardControls = ( +const CardControls = ({ + showRestartModal, cards, currentCardIndex, handleNextCard, handlePrevCard, handleShuffle, - showRestartModal, - setShowRestartModal, + handleShowModal, + handleHideModal, handleRestartCurrent, - handleStartNew -) => ( + handleStartNew, +}) => (
- {showRestartModal && ( )}
diff --git a/elevateyou/src/components/NumbersCard.jsx b/elevateyou/src/components/NumbersCard.jsx index ff949a6..5631daf 100644 --- a/elevateyou/src/components/NumbersCard.jsx +++ b/elevateyou/src/components/NumbersCard.jsx @@ -1,17 +1,101 @@ -const NumbersCard = ({ currentCard, flipped, setFlipped }) => ( -
-
setFlipped(!flipped)} - > -
-
{currentCard.text}
+import React, { useState, useEffect } from "react"; +import { Card, Button, Form } from "react-bootstrap"; + +const NumbersCard = ({ cards, currentCard, flipped, setFlipped }) => { + const [answer, setAnswer] = useState(""); + const [currentScore, setCurrentScore] = useState(0); + const [bestScore, setBestScore] = useState(0); + const [correct, setCorrect] = useState(false); + + useEffect(() => { + setAnswer(""); + }, [currentCard]); + + const handleFlip = () => { + if (flipped) { + setFlipped(!flipped); + } + }; + + const handleFormClick = (event) => { + event.stopPropagation(); // prevent click event from propagating up to card + }; + + const handleAnswerSubmit = (event) => { + event.preventDefault(); + setFlipped(!flipped); + }; + + const handleAnswerChange = (event) => { + const value = event.target.value; + if (value.toLowerCase() == currentCard.number) { + setCorrect(true); + setCurrentScore((prevScore) => prevScore + 1); + setBestScore((prevBestScore) => { + if (currentScore + 1 > prevBestScore) { + return currentScore + 1; + } else { + return prevBestScore; + } + }); + } + setAnswer(value); + }; + + return ( + <> +
+

{`Current Score: ${currentScore}/${cards.length}`}

+

Best Score: {bestScore}

-
-
{currentCard.number}
+
+ +
+
+ {" "} + + {currentCard.text} +
+ + Answer: + + + +
+
+
+
+
+
+ {" "} + + + {correct ? ( +

Correct

+ ) : ( + <> +

Incorrect

+
{`Correct Answer: ${currentCard.number}`}
+ + )} +
+
+
+
+
-
-
-); + + ); +}; export default NumbersCard; diff --git a/elevateyou/src/components/NumbersFlashcards.jsx b/elevateyou/src/components/NumbersFlashcards.jsx index e541134..351cd4b 100644 --- a/elevateyou/src/components/NumbersFlashcards.jsx +++ b/elevateyou/src/components/NumbersFlashcards.jsx @@ -1,20 +1,7 @@ import React, { useState, useEffect } from "react"; -import NextButton from "./NextButton"; import "../styles/Card.css"; import NumbersStartForm from "./NumbersStartForm"; -import axios from "axios"; -import { - BsArrowRight, - BsArrowLeft, - BsShuffle, - BsArrowRepeat, -} from "react-icons/bs"; import "../styles/NumbersFlashcards.css"; -import RestartModal from "./RestartModal"; -import { - getRandomNumFacts, - getCustomNumFacts, -} from "../utils/fetchNumbersFacts"; import shuffleArray from "../utils/shuffleArray"; import StartCard from "./StartCard"; import NumbersCard from "./NumbersCard"; @@ -67,6 +54,7 @@ const NumbersFlashCards = () => { const handleNextCard = () => { if (currentCardIndex < cards.length - 1) { + setFlipped(false); setPreviousCardIndex(currentCardIndex); setCurrentCardIndex(currentCardIndex + 1); } @@ -92,8 +80,6 @@ const NumbersFlashCards = () => { getCards(rangeType, min, max, numQuestions); }; - console.log(currentCard); - const handleRestartCurrent = () => { setShowRestartModal(false); setCurrentCardIndex(0); @@ -104,6 +90,14 @@ const NumbersFlashCards = () => { setShowStartForm(true); }; + const handleShowModal = () => { + setShowRestartModal(true); + }; + + const handleHideModal = () => { + setShowRestartModal(false); + }; + const renderProgressBar = () => // TODO: render the progress bar console.log("renderProgressBar"); @@ -125,18 +119,20 @@ const NumbersFlashCards = () => { {renderProgressBar()} {renderScore()} diff --git a/elevateyou/src/components/RestartModal.jsx b/elevateyou/src/components/RestartModal.jsx index bca68d9..f332059 100644 --- a/elevateyou/src/components/RestartModal.jsx +++ b/elevateyou/src/components/RestartModal.jsx @@ -1,11 +1,26 @@ import React from "react"; -import { Modal } from "react-bootstrap"; +import { Button } from "react-bootstrap"; +import Modal from "react-bootstrap/Modal"; +import "../styles/RestartModal.css"; -const RestartModal = ({ showModal, hideModal, restartCurrent, startNew }) => { +const RestartModal = ({ + showRestartModal, + handleHideModal, + handleRestartCurrent, + handleStartNew, +}) => { return ( - hideModal(false)}> + - Restart Cards + + Restart Cards +

@@ -13,22 +28,12 @@ const RestartModal = ({ showModal, hideModal, restartCurrent, startNew }) => {

- - + +
); diff --git a/elevateyou/src/styles/RestartModal.css b/elevateyou/src/styles/RestartModal.css new file mode 100644 index 0000000..b44e5a6 --- /dev/null +++ b/elevateyou/src/styles/RestartModal.css @@ -0,0 +1,18 @@ +.restart-modal-overlay { + position: fixed; + top: 0; + left: 0; + width: 100%; + height: 100%; + background-color: rgba(0, 0, 0, 0.5); + z-index: 9999; + display: flex; + justify-content: center; + align-items: center; +} + +.restart-modal { + background-color: white; + border-radius: 5px; + padding: 20px; +} \ No newline at end of file diff --git a/elevateyou/src/utils/fetchNumbersFacts.jsx b/elevateyou/src/utils/fetchNumbersFacts.jsx index b984a3c..e966c63 100644 --- a/elevateyou/src/utils/fetchNumbersFacts.jsx +++ b/elevateyou/src/utils/fetchNumbersFacts.jsx @@ -1,4 +1,5 @@ import React from "react"; +import axios from "axios"; const getRandomNumFacts = async (url, numQuestions) => { try {