From 6e0a95c1fa8f280366ca1930c6b3957d59f8fdcb Mon Sep 17 00:00:00 2001 From: Mykola Bilenkiy Date: Mon, 13 Jan 2025 02:04:40 +0200 Subject: [PATCH] make task --- README.md | 2 +- src/App.tsx | 19 ++++- src/components/NewMovie/NewMovie.tsx | 105 +++++++++++++++++++++++++-- 3 files changed, 115 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index abe3db5d7..838d704bc 100644 --- a/README.md +++ b/README.md @@ -27,4 +27,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://cyberte4.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..4e459f187 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,15 +2,30 @@ import './App.scss'; import { MoviesList } from './components/MoviesList'; import { NewMovie } from './components/NewMovie'; import moviesFromServer from './api/movies.json'; +import { useState } from 'react'; + +type Movie = { + title: string; + description: string; + imgUrl: string; + imdbUrl: string; + imdbId: string; +}; export const App = () => { + const [movies, setMovies] = useState(moviesFromServer); + + const handleAddMovie = (newMovie: Movie) => { + setMovies(prevMovies => [...prevMovies, newMovie]); + }; + return (
- +
- {}} */ /> +
); diff --git a/src/components/NewMovie/NewMovie.tsx b/src/components/NewMovie/NewMovie.tsx index 85bace9dd..cbf54f7f5 100644 --- a/src/components/NewMovie/NewMovie.tsx +++ b/src/components/NewMovie/NewMovie.tsx @@ -1,30 +1,118 @@ import { useState } from 'react'; import { TextField } from '../TextField'; -export const NewMovie = () => { +type Movie = { + title: string; + description: string; + imgUrl: string; + imdbUrl: string; + imdbId: string; +}; + +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 [title, setTitle] = useState(''); + const [description, setDescription] = useState(''); + const [image, setImage] = useState(''); + const [imdbUrl, setImdbUrl] = useState(''); + const [imdbId, setImdbId] = useState(''); + + const ifFormValid = () => { + return ( + title.trim() !== '' && + image.trim() !== '' && + imdbUrl.trim() !== '' && + imdbId.trim() !== '' + ); + }; + + const handleTitleChange = (value: string) => { + setTitle(value); // Тепер отримуємо тільки значення, а не event + }; + + const handleDescriptionChange = (value: string) => { + setDescription(value); + }; + + const handleImageChange = (value: string) => { + setImage(value); + }; + + const handleImdbUrlChange = (value: string) => { + setImdbUrl(value); + }; + + const handleImdbIdChange = (value: string) => { + setImdbId(value); + }; + + const handleSubmit = (event: React.FormEvent) => { + event.preventDefault(); + + onAdd({ + title, + description, + imgUrl: image, + imdbUrl, + imdbId, + }); + + setTitle(''); + setDescription(''); + setImage(''); + setImdbUrl(''); + setImdbId(''); + }; + return ( -
+

Add a movie

{}} + value={title} + onChange={handleTitleChange} required /> - + - + - + - +
@@ -32,6 +120,7 @@ export const NewMovie = () => { type="submit" data-cy="submit-button" className="button is-link" + disabled={!ifFormValid()} > Add