From bf461b31b0e61770df3fe536003da980d2429311 Mon Sep 17 00:00:00 2001 From: Serhii-Khobotov Date: Thu, 16 Jan 2025 18:30:59 +0200 Subject: [PATCH] my solution --- README.md | 2 +- src/App.tsx | 12 +++- src/components/NewMovie/NewMovie.tsx | 77 ++++++++++++++++++++++---- src/components/TextField/TextField.tsx | 3 +- 4 files changed, 79 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index abe3db5d7..c55a04321 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://Serhii-Khobotov.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..b97066c07 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 handleAddMovie = (movie: Movie) => { + setMovies(currentMovies => [...currentMovies, movie]); + }; + return (
- +
- {}} */ /> +
); diff --git a/src/components/NewMovie/NewMovie.tsx b/src/components/NewMovie/NewMovie.tsx index 85bace9dd..33e7dcc0a 100644 --- a/src/components/NewMovie/NewMovie.tsx +++ b/src/components/NewMovie/NewMovie.tsx @@ -1,30 +1,84 @@ import { 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: (movie: Movie) => void; +}; + +export const NewMovie: React.FC = ({ onAdd }) => { + 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 reset = () => { + setTitle(''); + setDescription(''); + setImgUrl(''); + setImdbUrl(''); + setImdbId(''); + }; + + const handleSubmit = (e: React.FormEvent) => { + e.preventDefault(); + onAdd({ + title, + description, + imgUrl, + imdbUrl, + imdbId, + }); + + setCount(currentCount => currentCount + 1); + reset(); + }; return ( -
+

Add a movie

{}} + value={title} + onChange={setTitle} required /> - + - + - + - +
@@ -32,6 +86,7 @@ export const NewMovie = () => { type="submit" data-cy="submit-button" className="button is-link" + disabled={!title || !description || !imgUrl || !imdbUrl || !imdbId} > Add diff --git a/src/components/TextField/TextField.tsx b/src/components/TextField/TextField.tsx index e24856c4b..f281a7d08 100644 --- a/src/components/TextField/TextField.tsx +++ b/src/components/TextField/TextField.tsx @@ -4,10 +4,11 @@ import React, { useState } from 'react'; type Props = { name: string; value: string; - label?: string; + label: string; placeholder?: string; required?: boolean; onChange?: (newValue: string) => void; + validate?: (value: string) => string; }; function getRandomDigits() {