From dafbbc7ce42fa1fc6137658e71694b80e7e7f83c Mon Sep 17 00:00:00 2001 From: demuths1 Date: Tue, 28 May 2024 17:07:17 -0400 Subject: [PATCH] fixed algorithm for questions without multiple choice & MyTrivia now sends one API request --- trivia-forge/backend/endpoints/game.py | 2 + .../frontend/src/Components/Categories.jsx | 4 +- .../frontend/src/Components/Questions.jsx | 19 ++++--- trivia-forge/frontend/src/Pages/MyTrivia.jsx | 8 +-- .../frontend/src/Pages/TriviaGenPage.jsx | 53 ++++++++++++------- .../frontend/src/Pages/TriviaReviewPage.jsx | 4 +- 6 files changed, 54 insertions(+), 36 deletions(-) diff --git a/trivia-forge/backend/endpoints/game.py b/trivia-forge/backend/endpoints/game.py index 6b0c85d0..46a92e1a 100644 --- a/trivia-forge/backend/endpoints/game.py +++ b/trivia-forge/backend/endpoints/game.py @@ -103,6 +103,7 @@ def get_patch_delete_game(game_id): def get_games_with_details(): user_id = request.args.get('user_id') try: + print(f"Fetching games with details for user_id: {user_id}") games_query = supabase.table("Games").select("*").eq("user_id", user_id).execute() games = games_query.data @@ -122,6 +123,7 @@ def get_games_with_details(): game['categories'] = categories game_details.append(game_detail) #print(game_details) + print(f"Returning game details:") return jsonify(game_details) except Exception as e: print(f"Error fetching games with details: {str(e)}") diff --git a/trivia-forge/frontend/src/Components/Categories.jsx b/trivia-forge/frontend/src/Components/Categories.jsx index 49ffba50..f5f74714 100644 --- a/trivia-forge/frontend/src/Components/Categories.jsx +++ b/trivia-forge/frontend/src/Components/Categories.jsx @@ -1,7 +1,7 @@ import React from "react"; import Questions from "../Components/Questions"; -function Categories({ category, index, changeValue}) { +function Categories({ category, index, changeValue, isMultipleChoice }) { let questions = category.questions; const path = ['categories', index]; @@ -10,7 +10,7 @@ function Categories({ category, index, changeValue}) {

{category.title || category.name}

{questions.map((question, index) => { return ( - + ); })} diff --git a/trivia-forge/frontend/src/Components/Questions.jsx b/trivia-forge/frontend/src/Components/Questions.jsx index 029d9918..8378d019 100644 --- a/trivia-forge/frontend/src/Components/Questions.jsx +++ b/trivia-forge/frontend/src/Components/Questions.jsx @@ -11,7 +11,7 @@ import { Question } from "../Models/Question"; -function Questions({ data, path, index, changeValue }) { +function Questions({ data, path, index, changeValue, isMultipleChoice }) { let choices = data.choices; let newPath = structuredClone(path) newPath.push('questions', index) @@ -24,13 +24,16 @@ function Questions({ data, path, index, changeValue }) {
-

Choices

- {choices.map((choice, index) => { - return ( - - ); - })} - + {isMultipleChoice && ( + <> +

Choices

+ {choices.map((choice, index) => { + return ( + + ); + })} + + )}

Answer

diff --git a/trivia-forge/frontend/src/Pages/MyTrivia.jsx b/trivia-forge/frontend/src/Pages/MyTrivia.jsx index 57d9ed25..e976b275 100644 --- a/trivia-forge/frontend/src/Pages/MyTrivia.jsx +++ b/trivia-forge/frontend/src/Pages/MyTrivia.jsx @@ -45,10 +45,10 @@ function MyTrivia() { } // Cleanup function to reset loaded when component unmounts - return () => { - setLoaded(false); - }; - }, [currentUser, userGames, setUserGames]); + // return () => { + // setLoaded(false); + // }; + }, [loaded]); useEffect(() => { if (currentGame && showWarning === false) { diff --git a/trivia-forge/frontend/src/Pages/TriviaGenPage.jsx b/trivia-forge/frontend/src/Pages/TriviaGenPage.jsx index 5e171813..42ca6098 100644 --- a/trivia-forge/frontend/src/Pages/TriviaGenPage.jsx +++ b/trivia-forge/frontend/src/Pages/TriviaGenPage.jsx @@ -62,7 +62,7 @@ function TriviaGenPage() { if (isMultipleChoice) { prompt += "Each question should be in the format Question:...\nChoice:...\nChoice:...\nChoice:...\nChoice:...\nAnswer:...\nHint:...\n---\nQuestion:... ect. Do not include A), B), C), or D) in the choices."; } else { - prompt += "Each question should be in the format \nQuestion:...\nAnswer:...\n---\nQuestion:... ect."; + prompt += "Each question should be in the format \nQuestion:...\nAnswer:...\nHint:...\n---\nQuestion:... ect."; } // api call @@ -101,28 +101,41 @@ function TriviaGenPage() { for (let i = 0; i < sections.length; i++) { if (sections[i] === '') { sections.splice(i, 1); } } - //loop through sections and create question and choice objects - for (let i = 0; i < sections.length; i += 7) { - let question = sections[i].slice(10); - let choices = []; - for (let k = 0; k < 4; k++) { - let choice = sections[i + k + 1]; - let newChoice = new Choice(choice.slice(8)); - choices.push(newChoice); - } - let answer = sections[i + 5].slice(8); - let hint = sections[i + 6].slice(6); - //create question object and add it to category - let newQuestion = new Question(question, answer, hint, isMultipleChoice); - newCategory.addQuestion(newQuestion); - //add choices to question object - for (let i = 0; i < choices.length; i++) { - newQuestion.addChoice(choices[i]); + //loop through sections and create question and choice objects + if (isMultipleChoice) { + for (let i = 0; i < sections.length; i += 7) { + let question = sections[i].slice(10); + let choices = []; + for (let k = 0; k < 4; k++) { + let choice = sections[i + k + 1]; + let newChoice = new Choice(choice.slice(8)); + choices.push(newChoice); + } + let answer = sections[i + 5].slice(8); + let hint = sections[i + 6].slice(6); + + //create question object and add it to category + let newQuestion = new Question(question, answer, hint, isMultipleChoice); + newCategory.addQuestion(newQuestion); + + //add choices to question object + for (let i = 0; i < choices.length; i++) { + newQuestion.addChoice(choices[i]); + } + } + } else { + for (let j = 0; j < sections.length; j += 3) { + let question = sections[j].slice(10); + let answer = sections[j + 1].slice(8); + let hint = sections[j + 2].slice(6); + + //create question object and add it to category + let newQuestion = new Question(question, answer, hint, isMultipleChoice); + newCategory.addQuestion(newQuestion); } } - console.log("Category Questions:", newCategory.questions); } console.log("Game Categories", game.categories); @@ -130,7 +143,7 @@ function TriviaGenPage() { // Save game to global state and local storage addGame(game); // state property to pass data as object to new route - navigate('/review', { state: { game: game, page: 'review' } }); + navigate('/review', { state: { game: game, page: 'review', isMultipleChoice: isMultipleChoice } }); //console.log(completion.choices[0].message); diff --git a/trivia-forge/frontend/src/Pages/TriviaReviewPage.jsx b/trivia-forge/frontend/src/Pages/TriviaReviewPage.jsx index caca9c8d..2b028a10 100644 --- a/trivia-forge/frontend/src/Pages/TriviaReviewPage.jsx +++ b/trivia-forge/frontend/src/Pages/TriviaReviewPage.jsx @@ -11,7 +11,7 @@ function TriviaReviewPage() { // Reference: https://reactrouter.com/en/main/hooks/use-location // pulls object from state property in TriviaGenPage const location = useLocation(); - const { game, page } = location.state; + const { game, page, isMultipleChoice } = location.state; let categories = game.categories; const navigate = useNavigate(); const updateGame = useStore(state => state.updateGame); @@ -53,7 +53,7 @@ function TriviaReviewPage() {
- +
))}