diff --git a/src/App.tsx b/src/App.tsx index 34be670b0..a5fc386cf 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,16 +1,23 @@ import './App.scss'; +import { useState } from 'react'; import { MoviesList } from './components/MoviesList'; import { NewMovie } from './components/NewMovie'; import moviesFromServer from './api/movies.json'; +import { Movie } from './types/Movie'; export const App = () => { + const [movies, setMovies] = useState(moviesFromServer); + const addMovie = (newMovie: Movie) => { + setMovies(currentMovies => [...currentMovies, newMovie]); + }; + return (
- +
- {}} */ /> +
); diff --git a/src/components/NewMovie/NewMovie.tsx b/src/components/NewMovie/NewMovie.tsx index 34f22fb0a..a8f487b1f 100644 --- a/src/components/NewMovie/NewMovie.tsx +++ b/src/components/NewMovie/NewMovie.tsx @@ -1,45 +1,89 @@ -import { useState } from 'react'; +import React, { useState } from 'react'; import { TextField } from '../TextField'; +import { Movie } from '../../types/Movie'; -export const NewMovie = () => { - // Increase the count after successful form submission - // to reset touched status of all the `Field`s - const [count] = useState(0); +type Props = { + onAdd: (movie: Movie) => void, +}; + +const initialMovieState = { + title: '', + description: '', + imgUrl: '', + imdbUrl: '', + imdbId: '', +}; + +export const NewMovie: React.FC = ({ onAdd }) => { + const [count, setCount] = useState(0); + const [movie, setMovie] = useState(initialMovieState); + + const resetForm = () => { + setMovie(initialMovieState); + }; + + const handleInputChange = (key: string, value: string) => { + setMovie(prevInputs => ({ ...prevInputs, [key]: value })); + }; + + const handleSubmit = (event: React.FormEvent) => { + event.preventDefault(); + + onAdd(movie); + + setCount(count + 1); + + resetForm(); + }; + + const isSubmitDisabled = !movie.title.trim() + || !movie.imgUrl.trim() || !movie.imdbUrl.trim() || !movie.imdbId.trim(); return ( -
+

Add a movie

{}} + value={movie.title} + onChange={(value) => handleInputChange('title', value)} required /> handleInputChange('description', value)} /> handleInputChange('imgUrl', value)} + required /> handleInputChange('imdbUrl', value)} + required /> handleInputChange('imdbId', value)} + required />
@@ -48,6 +92,7 @@ export const NewMovie = () => { type="submit" data-cy="submit-button" className="button is-link" + disabled={isSubmitDisabled} > Add