diff --git a/trivia-forge/backend/endpoints/game.py b/trivia-forge/backend/endpoints/game.py index 3b2fa15e..6b0c85d0 100644 --- a/trivia-forge/backend/endpoints/game.py +++ b/trivia-forge/backend/endpoints/game.py @@ -121,7 +121,7 @@ def get_games_with_details(): category['questions'] = questions game['categories'] = categories game_details.append(game_detail) - print(game_details) + #print(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/Pages/LoginPage.jsx b/trivia-forge/frontend/src/Pages/LoginPage.jsx index 3b5a5b59..540600cc 100644 --- a/trivia-forge/frontend/src/Pages/LoginPage.jsx +++ b/trivia-forge/frontend/src/Pages/LoginPage.jsx @@ -28,13 +28,13 @@ function LoginPage() { // set user and games in global store setCurrentUser(user); - setUserGames(games); - + //setUserGames(games); + // console.log('User games:', games); //pass user and games as state to myTrivia page navigate('/myTrivia', { state: { user, games } }); - + } catch (error) { console.error('Error during login:', error); alert('Error logging in. Please try again.'); diff --git a/trivia-forge/frontend/src/Pages/MyTrivia.jsx b/trivia-forge/frontend/src/Pages/MyTrivia.jsx index 32d817c8..4677c30a 100644 --- a/trivia-forge/frontend/src/Pages/MyTrivia.jsx +++ b/trivia-forge/frontend/src/Pages/MyTrivia.jsx @@ -1,5 +1,5 @@ import { React, useState, useEffect } from "react"; -import { getGames, getGamesWithDetails } from "../Services/TF-db_services"; +import { getGames, getGamesWithDetails, deleteGame } from "../Services/TF-db_services"; import Card from 'react-bootstrap/Card'; import Col from 'react-bootstrap/Col'; import Row from 'react-bootstrap/Row'; @@ -14,7 +14,11 @@ import useStore from '../Components/useStore'; function MyTrivia() { // const [games, setGames] = useState(null); // store list of games const [show, setShow] = useState(false); // visibility of modal + + const [showWarning, setShowWarning] = useState(false); // visibility of warning modal const [currentGame, setCurrentGame] = useState(null); // store current game + + const [loaded, setLoaded] = useState(false); // const [gamesWithDetails, setGamesWithDetails] = useState([]); // store games from user const navigate = useNavigate(); // const location = useLocation(); @@ -22,19 +26,31 @@ function MyTrivia() { const userGames = useStore(state => state.userGames); const setUserGames = useStore(state => state.setUserGames); + + const handleShow = async (game) => { + setCurrentGame(game); + setShow(true); + }; // const user = location.state?.user; // fetch game with details when the user changes useEffect(() => { - if (currentUser && userGames.length === 0) { + if (currentUser && !loaded) { + setLoaded(true); + //console.log("calling getGamesWithDetails"); getGamesWithDetails(currentUser.id).then(res => { setUserGames(res); }); } + + // Cleanup function to reset loaded when component unmounts + return () => { + setLoaded(false); + }; }, [currentUser, userGames, setUserGames]); useEffect(() => { - if (currentGame) { + if (currentGame && showWarning === false) { setShow(true); } }, [currentGame]); @@ -44,9 +60,21 @@ function MyTrivia() { setCurrentGame(null); } - function handleShow(game) { + + function handleShowWarning(game) { setCurrentGame(game); - setShow(true); + console.log("current game", currentGame); + setShowWarning(true); + } + function handleWarningClose() { + setShowWarning(false); + } + function handleDelete() { + console.log("deleting game", currentGame); + deleteGame(currentGame).then(res => { + setUserGames(userGames.filter(g => g.id !== currentGame.id)); + }); + setShowWarning(false); } console.log(userGames); @@ -73,6 +101,7 @@ function MyTrivia() {