Skip to content

Commit

Permalink
add task solution
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaSnegireva committed Oct 13, 2023
1 parent 9bacc9b commit f1ccda6
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand All @@ -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 `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_movies-list-add-form/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://MariaSnegireva.github.io/react_movies-list-add-form/) and add it to the PR description.
10 changes: 8 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
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 [allMovies, setAllMovies] = useState(moviesFromServer);

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={allMovies} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie
onAdd={(movie: Movie) => setAllMovies([...allMovies, movie])}
/>
</div>
</div>
);
Expand Down
77 changes: 68 additions & 9 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,103 @@
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<Props> = ({ 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 [name, setName] = useState('');
const [description, setDescription] = useState('');
const [imageUrl, setImageUrl] = useState('');
const [imdbUrl, setImdbUrl] = useState('');
const [imdbId, setImdbId] = useState('');

const checkInput = () => {
// 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]*))?)$/;

const allInputReceived
= name.trim() && imageUrl.trim() && imdbUrl.trim() && imdbId.trim();

const regexMatched = imdbUrl.match(pattern) && imageUrl.match(pattern);

return allInputReceived && regexMatched;
};

const reset = () => {
setName('');
setDescription('');
setImageUrl('');
setImdbUrl('');
setImdbId('');
};

const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();

if (checkInput()) {
onAdd({
title: name,
description,
imgUrl: imageUrl,
imdbUrl,
imdbId,
});
reset();
setCount(count + 1);
}
};

return (
<form className="NewMovie" key={count}>
<form
className="NewMovie"
key={count}
onSubmit={handleSubmit}
>
<h2 className="title">Add a movie</h2>

<TextField
name="title"
label="Title"
value=""
onChange={() => {}}
value={name}
onChange={setName}
required
/>

<TextField
name="description"
label="Description"
value=""
value={description}
onChange={setDescription}
required
/>

<TextField
name="imgUrl"
label="Image URL"
value=""
value={imageUrl}
onChange={setImageUrl}
required
/>

<TextField
name="imdbUrl"
label="Imdb URL"
value=""
value={imdbUrl}
onChange={setImdbUrl}
required
/>

<TextField
name="imdbId"
label="Imdb ID"
value=""
value={imdbId}
onChange={setImdbId}
required
/>

<div className="field is-grouped">
Expand All @@ -48,6 +106,7 @@ export const NewMovie = () => {
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={!checkInput()}
>
Add
</button>
Expand Down
31 changes: 26 additions & 5 deletions src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,31 @@ export const TextField: React.FC<Props> = ({
}) => {
// generage a unique id once on component load
const [id] = useState(() => `${name}-${getRandomDigits()}`);
// 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]*))?)$/;

// To show errors only if the field was touched (onBlur)
const [touched, setTouched] = useState(false);
const hasError = touched && required && !value;
const [errorMessage, setErrorMessage] = useState(`${label} is required`);

const hasError = () => {
if ((name === 'imgUrl' || name === 'imdbUrl') && value) {
return touched && required && !value.match(pattern);
}

return touched && required && !value;
};

const handleOnBlur = () => {
if ((name === 'imgUrl' || name === 'imdbUrl')
&& value && !value.match(pattern)) {
setErrorMessage('Wrong URL input');
} else {
setErrorMessage(`${label} is required`);
}

setTouched(true);
};

return (
<div className="field">
Expand All @@ -43,17 +64,17 @@ export const TextField: React.FC<Props> = ({
id={id}
data-cy={`movie-${name}`}
className={classNames('input', {
'is-danger': hasError,
'is-danger': hasError(),
})}
placeholder={placeholder}
value={value}
onChange={event => onChange(event.target.value)}
onBlur={() => setTouched(true)}
onBlur={handleOnBlur}
/>
</div>

{hasError && (
<p className="help is-danger">{`${label} is required`}</p>
{hasError() && (
<p className="help is-danger">{errorMessage}</p>
)}
</div>
);
Expand Down

0 comments on commit f1ccda6

Please sign in to comment.