From 62be060203e6f0b55782e74d0af03243c207adf8 Mon Sep 17 00:00:00 2001 From: Vitaliy Zviriuk Date: Thu, 19 Oct 2023 20:27:56 +0200 Subject: [PATCH] solution --- README.md | 2 +- src/App.tsx | 12 +++- src/components/NewMovie/NewMovie.tsx | 85 +++++++++++++++++++++++--- src/components/TextField/TextField.tsx | 4 +- 4 files changed, 89 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 6001d15be..a8e908d9b 100644 --- a/README.md +++ b/README.md @@ -30,4 +30,4 @@ const pattern = /^((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|( - Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline). - Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript). - Open one more terminal and run tests with `npm test` to ensure your solution is correct. -- Replace `` with your Github username in the [DEMO LINK](https://.github.io/react_movies-list-add-form/) and add it to the PR description. +- Replace `` with your Github username in the [DEMO LINK](https://zvir91.github.io/react_movies-list-add-form/) and add it to the PR description. diff --git a/src/App.tsx b/src/App.tsx index 34be670b0..5444c0976 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,16 +1,24 @@ +import { useState } from 'react'; + import './App.scss'; 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..67f640e82 100644 --- a/src/components/NewMovie/NewMovie.tsx +++ b/src/components/NewMovie/NewMovie.tsx @@ -1,45 +1,109 @@ import { useState } from 'react'; import { TextField } from '../TextField'; +import { Movie } from '../../types/Movie'; -export const NewMovie = () => { +type Props = { + onAdd: (movie:Movie) => void +}; + +export const NewMovie: React.FC = ({ onAdd }) => { // Increase the count after successful form submission - // to reset touched status of all the `Field`s - const [count] = useState(0); + // to reset touched status of all the `Field` + const [count, setCount] = useState(0); + const [newTitle, setNewTitle] = useState(''); + const [newDescription, setNewDescription] = useState(''); + const [newImageURL, setNewImageURL] = useState(''); + const [newURL, setNewURL] = useState(''); + const [newId, setNewId] = useState(''); + + const reset = () => { + setNewTitle(''); + setNewDescription(''); + setNewImageURL(''); + setNewURL(''); + setNewId(''); + }; + + const disable = !newTitle || !newImageURL || !newId || !newURL || !newId; + + const handlerSubmit = (event: React.FormEvent) => { + event.preventDefault(); + + let updCaunt = count; + + setCount(updCaunt += 1); + + if (disable) { + return; + } + + onAdd({ + title: newTitle, + description: newDescription, + imgUrl: newImageURL, + imdbUrl: newURL, + imdbId: newId, + }); + + reset(); + }; return ( -
+

Add a movie

{}} + value={newTitle} + onChange={(value) => { + setNewTitle(value); + }} required /> { + setNewDescription(value); + }} /> { + setNewImageURL(value); + }} + required /> { + setNewURL(value); + }} + required /> { + setNewId(value); + }} + required />
@@ -48,6 +112,7 @@ export const NewMovie = () => { type="submit" data-cy="submit-button" className="button is-link" + disabled={disable} > Add diff --git a/src/components/TextField/TextField.tsx b/src/components/TextField/TextField.tsx index 307b19865..bc5cfaed8 100644 --- a/src/components/TextField/TextField.tsx +++ b/src/components/TextField/TextField.tsx @@ -47,7 +47,9 @@ export const TextField: React.FC = ({ })} placeholder={placeholder} value={value} - onChange={event => onChange(event.target.value)} + onChange={(event) => { + onChange(event.target.value); + }} onBlur={() => setTouched(true)} />