import React, { useState, useContext } from 'react'; import { useNavigate, useLocation } from 'react-router-dom'; import { UserContext } from '../context/UserContext'; import axios from 'axios'; import { Box, TextField, Button, Typography, Select, MenuItem, FormControl, InputLabel, Grid2, Card, CardContent, CardMedia, Menu } from '@mui/material'; // Import the same icons as in games.jsx 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 HeartIcon from '@mui/icons-material/Favorite'; import WaterDropIcon from '@mui/icons-material/WaterDrop'; import KeyboardDoubleArrowUpIcon from '@mui/icons-material/KeyboardDoubleArrowUp'; import defaultCharacterImage from '../assets/default-character.png'; const CreateCharacter = () => { const navigate = useNavigate(); const location = useLocation(); const { userId } = useContext(UserContext); const gameId = new URLSearchParams(location.search).get('gameId'); const [formData, setFormData] = useState({ charName: '', race: '', sex: '', age: '', job: '', description: '', maxHealth: 100, maxMana: 100, strength: 10, dexterity: 10, agility: 10, endurance: 10 }); const [selectedImage, setSelectedImage] = useState(null); const [imagePreview, setImagePreview] = useState(null); const handleChange = (e) => { setFormData({ ...formData, [e.target.name]: e.target.value }); }; const handleImageChange = (e) => { const file = e.target.files[0]; if (file) { setSelectedImage(file); setImagePreview(URL.createObjectURL(file)); } }; const handleSubmit = async (e) => { e.preventDefault(); const formDataToSend = new FormData(); // Add all form data Object.keys(formData).forEach(key => { formDataToSend.append(key, formData[key]); }); // Add image if selected if (selectedImage) { formDataToSend.append('image', selectedImage); } // Add gameId and playerId formDataToSend.append('gameId', gameId); formDataToSend.append('playerId', userId); try { const response = await axios.post( 'http://localhost:5000/games/character/create', formDataToSend, { headers: { 'Content-Type': 'multipart/form-data' } } ); if (response.status === 201) { // Redirect to the game page after successful character creation navigate(`/games/${gameId}`); } } catch (error) { console.error('Error creating character:', error); // You might want to show an error message to the user here } }; const inputStyles = { '& .MuiOutlinedInput-root': { '& fieldset': { borderColor: '#444' }, '&:hover fieldset': { borderColor: '#764ACB' }, '&.Mui-focused fieldset': { borderColor: '#764ACB' }, }, '& .MuiInputLabel-root': { color: '#fff' }, '& .MuiInputBase-input': { color: '#fff' } }; return (
{/* Character Image and Details */} {imagePreview ? ( ) : ( )} Sex {/* Health/Mana Bars - Keep existing */} Max Health Max Mana {/* Description */} Description {/* Stats Grid */} Character Stats {/* Submit Button */}
); }; export default CreateCharacter;