import { useState, useEffect, useContext } from 'react'; import { Link, useParams } from 'react-router-dom'; import axios from 'axios'; import { UserContext } from '../context/UserContext'; import { Box, Typography, Grid2, Card, CardContent, CardMedia, Button, IconButton, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle, TextField } from '@mui/material'; import EditIcon from '@mui/icons-material/Edit'; import PaidIcon from '@mui/icons-material/Paid'; import CalendarTodayIcon from '@mui/icons-material/CalendarToday'; import PetsIcon from '@mui/icons-material/Pets'; import WcIcon from '@mui/icons-material/Wc'; import WorkIcon from '@mui/icons-material/Work'; import defaultCharacterImage from '../assets/default-character.png'; import defaultItemImage from '../assets/default-item.png'; import './games.css'; const GamesPage = () => { const { userId } = useContext(UserContext); const { gameId } = useParams(); const [character, setCharacter] = useState(null); const [inventory, setInventory] = useState([]); const [isEditOpen, setIsEditOpen] = useState(false); const [newDescription, setNewDescription] = useState(''); const fetchCharacter = async () => { try { console.log(`Fetching character for gameId: ${gameId}, userId: ${userId}`); // Debug output const response = await axios.get(`http://localhost:5000/games/${gameId}/${userId}/character`); console.log('Character data:', response.data); // Debug output setCharacter(response.data); } catch (error) { console.error('Error fetching character:', error); } }; const fetchInventory = async (charId) => { try { const response = await axios.get(`http://localhost:5000/games/${gameId}/${charId}/items`); setInventory(response.data ? [response.data].flat() : []); } catch (error) { console.error('Error fetching inventory:', error); } }; useEffect(() => { if (userId) { fetchCharacter(); } }, [userId, gameId]); useEffect(() => { if (character?.CharID) { fetchInventory(character.CharID); } }, [character]); useEffect(() => { const interval = setInterval(() => { if (character?.CharID) { fetchCharacter(); fetchInventory(character.CharID); } }, 5000); return () => clearInterval(interval); }, [character]); const handleEditOpen = () => { setNewDescription(character.description); setIsEditOpen(true); }; const handleEditClose = () => { setIsEditOpen(false); }; const handleSaveDescription = async () => { try { const response = await axios.put(`http://localhost:5000/games/${gameId}/${userId}/character`, { description: newDescription }); setCharacter({ ...character, description: newDescription }); setIsEditOpen(false); } catch (error) { console.error('Error updating description:', error); } }; if (!character) { return ( No Character Found ); } return ( {/* Character Image and Details */} {character.CharName} Gold: {character.Gold} Age: {character.Age} Race: {character.Race} Sex: {character.Sex} Job: {character.Job} {/* Health and Mana Bars */} Health {character.currentHealth}/{character.maxHealth} Mana {character.currentMana}/{character.maxMana} {/* Character Info Section */} Description {character.description} {/* Inventory */} Inventory {inventory.map((item, index) => ( {item.ItemName} Value: {item.GoldValue} ))} {/* Edit Description Dialog */} Edit Description --------------------------------------------------------------------- setNewDescription(e.target.value)} sx={{ '& .MuiInputBase-root': { color: '#fff' }, '& .MuiInputLabel-root': { color: '#ccc' } }} /> ); }; export default GamesPage;