From b4800e7f3bc913dd0297d1e514f00a1609bd59f9 Mon Sep 17 00:00:00 2001 From: 1788l Date: Sun, 21 Jan 2024 16:23:54 +0200 Subject: [PATCH] Solution --- README.md | 2 +- src/App.tsx | 12 ++- src/components/NewMovie/NewMovie.tsx | 110 ++++++++++++++++++++++----- 3 files changed, 102 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 6001d15be..9f624e7ce 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://AV1788.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..4bc410046 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,16 +1,24 @@ import './App.scss'; +import { useState } from 'react'; +import { Movie } from './types/Movie'; import { MoviesList } from './components/MoviesList'; import { NewMovie } from './components/NewMovie'; import moviesFromServer from './api/movies.json'; export const App = () => { + const [movies, setMovies] = useState(moviesFromServer); + + const addMovie = (movie: Movie) => { + setMovies(currentMovies => [...currentMovies, movie]); + }; + return (
- +
- {}} */ /> +
); diff --git a/src/components/NewMovie/NewMovie.tsx b/src/components/NewMovie/NewMovie.tsx index acf9bed20..f3df0bf81 100644 --- a/src/components/NewMovie/NewMovie.tsx +++ b/src/components/NewMovie/NewMovie.tsx @@ -1,61 +1,133 @@ 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); - const [text, setText] = useState(''); + const [count, setCount] = useState(0); + const [title, setTitle] = useState(''); + const [description, setDescription] = useState(''); + const [imgUrl, setImgUrl] = useState(''); + const [imdbUrl, setImdbUrl] = useState(''); + const [imdbId, setImdbId] = useState(''); + + const addTitle = (textInput: string) => { + setTitle(textInput); + }; + + const addDescription = (textInput: string) => { + setDescription(textInput); + }; + + const addImgUrl = (textInput: string) => { + setImgUrl(textInput); + }; + + const addImdbUrl = (textInput: string) => { + setImdbUrl(textInput); + }; - const addText = (textInput: string) => { - setText(textInput); + const addImdbId = (textInput: string) => { + setImdbId(textInput); + }; + + const reset = () => { + setTitle(''); + setDescription(''); + setImgUrl(''); + setImdbUrl(''); + setImdbId(''); + }; + + const handleSubmit = (event:React.FormEvent) => { + event.preventDefault(); + + onAdd({ + title, + description, + imgUrl, + imdbUrl, + imdbId, + }); + + reset(); + setCount(count + 1); }; return ( -
+

Add a movie

- + {title !== '' && imgUrl !== '' + && imdbUrl !== '' && imdbId !== '' + ? ( + + ) : ( + + )}