diff --git a/README.md b/README.md index 6001d15be..54f4d1efa 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://P-Nazar.github.io/react_movies-list-add-form/) and add it to the PR description. diff --git a/src/App.scss b/src/App.scss index 89b6ef5fb..2a0875434 100644 --- a/src/App.scss +++ b/src/App.scss @@ -1,3 +1,7 @@ +iframe { + display: none; +} + .page { display: grid; grid-template: auto / 1fr 400px; diff --git a/src/App.tsx b/src/App.tsx index 34be670b0..4eb41b39f 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,16 +1,26 @@ import './App.scss'; +import { useState } from 'react'; 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 [movieList, setMovieList] = useState(moviesFromServer); + + const handleAddMovie = (newMovie: Movie) => { + setMovieList((prevMovies) => [...prevMovies, newMovie]); + }; + return (
- +
- {}} */ /> +
); diff --git a/src/components/NewMovie/NewMovie.tsx b/src/components/NewMovie/NewMovie.tsx index 34f22fb0a..928efff5f 100644 --- a/src/components/NewMovie/NewMovie.tsx +++ b/src/components/NewMovie/NewMovie.tsx @@ -1,45 +1,106 @@ import { useState } from 'react'; import { TextField } from '../TextField'; +import { Movie } from '../../types/Movie'; -export const NewMovie = () => { +interface Props { + onAdd: (movie: Movie) => void + +} + +const urlValidator = (urlString: string): boolean => { + // eslint-disable-next-line max-len + const pattern = /^((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www\.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@,.\w_]*)#?(?:[,.!/\\\w]*))?)$/; + + if (!urlString.match(pattern)) { + return false; + } + + return true; +}; + +const initialMovieState = { + title: '', + description: '', + imgUrl: '', + imdbUrl: '', + imdbId: '', +}; + +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 [count, setCount] = useState(0); + const [newMovie, setNewMovie] = useState(initialMovieState); + + const isDisabled = !newMovie.title.trim() + || !urlValidator(newMovie.imgUrl) || !urlValidator(newMovie.imdbUrl) + || !newMovie.imdbId.trim(); + + function addMovie(event: React.FormEvent) { + event.preventDefault(); + + onAdd({ + title: newMovie.title.trim(), + description: newMovie.description.trim(), + imgUrl: newMovie.imgUrl.trim(), + imdbUrl: newMovie.imdbUrl.trim(), + imdbId: newMovie.imdbId.trim(), + }); + + setNewMovie(initialMovieState); + + setCount(prev => prev + 1); + } + + const handleChange = (e: React.ChangeEvent) => { + const { name, value } = e.target; + + setNewMovie((prevMovie) => ({ ...prevMovie, [name]: value })); + }; return ( -
+

Add a movie

{}} + value={newMovie.title} + onChange={handleChange} required />
@@ -48,6 +109,7 @@ export const NewMovie = () => { type="submit" data-cy="submit-button" className="button is-link" + disabled={isDisabled} > Add diff --git a/src/components/TextField/TextField.tsx b/src/components/TextField/TextField.tsx index 307b19865..538a47da1 100644 --- a/src/components/TextField/TextField.tsx +++ b/src/components/TextField/TextField.tsx @@ -7,7 +7,8 @@ type Props = { label?: string, placeholder?: string, required?: boolean, - onChange?: (newValue: string) => void, + onChange?: (e: React.ChangeEvent) => void, + checkUrl?: (urlString: string) => boolean, }; function getRandomDigits() { @@ -23,13 +24,14 @@ export const TextField: React.FC = ({ placeholder = `Enter ${label}`, required = false, onChange = () => {}, + checkUrl = undefined, }) => { // generage a unique id once on component load const [id] = useState(() => `${name}-${getRandomDigits()}`); // To show errors only if the field was touched (onBlur) const [touched, setTouched] = useState(false); - const hasError = touched && required && !value; + const hasError = touched && required && !value.trim(); return (
@@ -39,6 +41,7 @@ export const TextField: React.FC = ({
= ({ })} placeholder={placeholder} value={value} - onChange={event => onChange(event.target.value)} + onChange={event => onChange(event)} onBlur={() => setTouched(true)} />
@@ -55,6 +58,8 @@ export const TextField: React.FC = ({ {hasError && (

{`${label} is required`}

)} + + {((touched && value) && (checkUrl && !checkUrl(value))) && (

{`${label} incorrect URL`}

)}
); };