import { useState } from "react"; import { useDispatch, useSelector } from…
- User
import { useState } from "react"; import { useDispatch, useSelector } from "react-redux"; import { addGenreToGlobalAction,resetGenresGlobal } from "../../redux/actions"; import axios from "axios"; import { useEffect } from "react";
const Form = () => {
const globalGenres = useSelector((state)=>state.genres);
const dispatch = useDispatch();
const [form, setForm] = useState({ name:"", description:"", platforms:"", image:"", releaseDate:"", rating:"", genres:[],
})
useEffect(() => { setForm((form) => ({ ...form, genres: globalGenres })); }, [globalGenres]);
const [genres, setGenres] = useState("") const [nameGenres, setNameGenres] = useState([])
const onChangeHandle = (event) => { const value = event.target.value; const property = event.target.name; setForm({...form, [property]:value}) }
const submitHandler = async (event) => { event.preventDefault(); setForm({...form, genres: globalGenres}) try { const response = await axios.post('http://localhost:3001/post/videogames', form); console.log('Formulario Enviado Exitosamente',response.data); } catch (error) { console.error(error); } }
const handleGenres = (event) => { const genre = JSON.parse(event.target.value); setGenres(genre);
};
const addGenresToGlobal = (genres) => { dispatch(addGenreToGlobalAction(genres.value)); setNameGenres([...nameGenres, genres.name]); setGenres("");
}
const resetGenresHandlerClick = () => { const emptyArray = [] setNameGenres([]) dispatch(resetGenresGlobal(emptyArray)) }
return ( Nombre: Descripcion: Platform: Imagen: Fecha de Lanzamiento: Calificacion: Generos: Selecciona los generos <option value={JSON.stringify({ name: 'Action', value: '1' })}>Action <option value={JSON.stringify({ name: 'Indie', value: '2' })}>Indie <option value={JSON.stringify({ name: 'Adventure', value: '3' })}>Adventure <option value={JSON.stringify({ name: 'RPG', value: '4' })}>RPG <option value={JSON.stringify({ name: 'Strategy', value: '5' })}>Strategy <option value={JSON.stringify({ name: 'Shooter', value: '6' })}>Shooter <option value={JSON.stringify({ name: 'Casual', value: '7' })}>Casual <option value={JSON.stringify({ name: 'Simulation', value: '8' })}>Simulation <option value={JSON.stringify({ name: 'Puzzle', value: '9' })}>Puzzle <option value={JSON.stringify({ name: 'Arcade', value: '10' })}>Arcade <option value={JSON.stringify({ name: 'Platformer', value: '11' })}>Platformer <option value={JSON.stringify({ name: 'Massively Multiplayer', value: '12' })}>Massively Multiplayer <option value={JSON.stringify({ name: 'Racing', value: '13' })}>Racing <option value={JSON.stringify({ name: 'Sports', value: '14' })}>Sports <option value={JSON.stringify({ name: 'Fighting', value: '15' })}>Fighting <option value={JSON.stringify({ name: 'Family', value: '16' })}>Family <option value={JSON.stringify({ name: 'Board Game', value: '17' })}>Board Games <option value={JSON.stringify({ name: 'Educational', value: '18' })}>Educational <option value={JSON.stringify({ name: 'Card', value: '19' })}>Card <button type="button" onClick={()=>{addGenresToGlobal(genres)}}>Agregar Genero <button type="button" onClick={()=>{resetGenresHandlerClick()}}>Reset Generos {nameGenres && nameGenres.map((genres,index) =>{ return {genres} })} Enviar ); };
- Assistant
Claro, puedo ayudarte a crear validaciones para tu formulario. Aquí tienes un ejemplo de cómo podrías hacerlo:
Importa la librería Yup para crear las validaciones:
import * as Yup from "yup";Crea un esquema de validación usando Yup:
const validationSchema = Yup.object().shape({ name: Yup.string().required("El nombre es obligatorio"), description: Yup.string().required("La descripción es obligatoria"), platforms: Yup.string().required("La plataforma es obligatoria"), image: Yup.string().url("La imagen debe ser una URL válida").required("La imagen es obligatoria"), releaseDate: Yup.string().required("La fecha de lanzamiento es obligatoria"), rating: Yup.number().required("La calificación es obligatoria").positive("La calificación debe ser un número positivo"), genres: Yup.array().min(1, "Debe seleccionar al menos un género"), });