From 091250ef65d697ca04100c9cbee663d207c36cbd Mon Sep 17 00:00:00 2001 From: MrWhiteres Date: Sat, 27 Jan 2024 22:24:09 +0200 Subject: [PATCH] add task solutions --- src/App.tsx | 7 ++- src/components/NewMovie/NewMovie.tsx | 70 +++++++++++++++++++++++----- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 34be670b0..4424be42b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,16 +1,19 @@ import './App.scss'; +import { useState } from 'react'; import { MoviesList } from './components/MoviesList'; import { NewMovie } from './components/NewMovie'; import moviesFromServer from './api/movies.json'; export const App = () => { + const [movies, setMovies] = useState(moviesFromServer); + return (
- +
- {}} */ /> + setMovies([...movies, movie])} />
); diff --git a/src/components/NewMovie/NewMovie.tsx b/src/components/NewMovie/NewMovie.tsx index 34f22fb0a..447b13862 100644 --- a/src/components/NewMovie/NewMovie.tsx +++ b/src/components/NewMovie/NewMovie.tsx @@ -1,45 +1,90 @@ -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:(newMovie:Movie) => void; +}; + +const initialMovieState = { + title: '', + description: '', + imgUrl: '', + imdbUrl: '', + imdbId: '', +}; + +export const NewMovie:React.FC = ({ onAdd }) => { + const [newMovie, setNewMovie] = useState(initialMovieState); + const [count, setCount] = useState(0); + + const clearForm = () => { + setNewMovie(initialMovieState); + setCount((prevCount) => prevCount + 1); + }; + + const handleChange = (name:string) => (value:string) => { + setNewMovie((prevMovie) => ({ ...prevMovie, [name]: value })); + }; + + const handleAddForm = (event:React.FormEvent) => { + event.preventDefault(); + onAdd(newMovie); + clearForm(); + }; + + const isDisabled = !( + newMovie.title.trim() + && newMovie.imgUrl.trim() + && newMovie.imdbUrl.trim() + && newMovie.imdbId.trim() + ); return ( -
+

Add a movie

{}} + value={newMovie.title} + onChange={handleChange('title')} required />
@@ -48,6 +93,7 @@ export const NewMovie = () => { type="submit" data-cy="submit-button" className="button is-link" + disabled={isDisabled} > Add