diff --git a/src/App.tsx b/src/App.tsx index 34be670b0..9e7a86ddb 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,16 +1,24 @@ 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..94b57f195 100644 --- a/src/components/NewMovie/NewMovie.tsx +++ b/src/components/NewMovie/NewMovie.tsx @@ -1,45 +1,105 @@ -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; +}; + +export const NewMovie: React.FC = ({ onAdd }) => { + const defaultMovieData = { + title: '', + description: '', + imgUrl: '', + imdbUrl: '', + imdbId: '', + }; + + const [movieData, setMovieData] = useState(defaultMovieData); + const [count, setCount] = useState(0); + + const { + title, + description, + imgUrl, + imdbUrl, + imdbId, + } = movieData; + + const reset = () => { + setMovieData(defaultMovieData); + }; + + const handleChange = (field: string, newValue: string) => { + setMovieData((prevData) => ({ + ...prevData, + [field]: newValue, + })); + }; + + const handleAdd = (event: React.FormEvent) => { + event.preventDefault(); + onAdd({ + title, + description, + imgUrl, + imdbUrl, + imdbId, + }); + reset(); + setCount((prevCount) => prevCount + 1); + }; + + const checkCorrect = !title.trim() + || !imgUrl.trim() + || !imdbUrl.trim() + || !imdbId.trim(); return ( -
+

Add a movie

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