diff --git a/README.md b/README.md index 6001d15be..cbcf97d0c 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ You have the `App` with the `MoviesList` and `NewMovie` form containing ready to use `TextField` components. Learn how it works and implement an ability to add movies from [IMDB](https://www.imdb.com/). -If you want to test your page you can get first image from a [movie page](https://www.imdb.com/title/tt1312171) using `DevTools` -> `Network` -> `Img` +If you want to test your page you can get first image from a [movie page](https://www.imdb.com/title/tt1312171) using `DevTools` -> `Network` -> `Img` > Here is [the demo page](https://mate-academy.github.io/react_movies-list-add-form/) @@ -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://pivkopa.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..c7e51c1aa 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,16 +1,22 @@ +import { useState } from 'react'; import './App.scss'; import { MoviesList } from './components/MoviesList'; import { NewMovie } from './components/NewMovie'; import moviesFromServer from './api/movies.json'; export const App = () => { + const [prepMovies, setPrepMovies] = useState(moviesFromServer); + return (
- +
- {}} */ /> + { + setPrepMovies(currentMovies => [...currentMovies, movie]); + }} + />
); diff --git a/src/components/NewMovie/NewMovie.tsx b/src/components/NewMovie/NewMovie.tsx index 34f22fb0a..736cd59bf 100644 --- a/src/components/NewMovie/NewMovie.tsx +++ b/src/components/NewMovie/NewMovie.tsx @@ -1,56 +1,133 @@ -import { useState } from 'react'; +import React, { useState } from 'react'; import { TextField } from '../TextField'; +import { Movie } from '../../types/Movie'; -export const NewMovie = () => { +export interface NewMovieProps { + 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); + const defaultMovie = { + title: '', + description: '', + imgUrl: '', + imdbUrl: '', + imdbId: '', + }; + + const [count, setCount] = useState(0); + const [newMovie, setNewMovie] = useState(defaultMovie); + + const ableToAddMovie = newMovie.title.trim() + && newMovie.imgUrl.trim() && newMovie.imdbUrl.trim() + && newMovie.imdbId.trim(); + + const submitAddMovie = (event: React.FormEvent) => { + event.preventDefault(); + + setNewMovie(defaultMovie); + + setCount((currentCount) => currentCount + 1); + + onAdd(newMovie); + }; return ( -
+

Add a movie

{}} + value={newMovie.title} + onChange={(event) => { + setNewMovie(currentMovie => { + return { + ...currentMovie, + title: event, + }; + }); + }} required /> { + setNewMovie(currentMovie => { + return { + ...currentMovie, + description: event, + }; + }); + }} /> { + setNewMovie(currentMovie => { + return { + ...currentMovie, + imgUrl: event, + }; + }); + }} + required /> { + setNewMovie(currentMovie => { + return { + ...currentMovie, + imdbUrl: event, + }; + }); + }} + required /> { + setNewMovie(currentMovie => { + return { + ...currentMovie, + imdbId: event, + }; + }); + }} + required />
+ +