Skip to content

Commit

Permalink
added task solution. Also optional validation is done
Browse files Browse the repository at this point in the history
  • Loading branch information
koros-rk committed Oct 9, 2023
1 parent 9bacc9b commit eee5de4
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 19 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://koros-rk.github.io/react_movies-list-add-form/) and add it to the PR description.
11 changes: 9 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { useState } from 'react';
import './App.scss';
import { MoviesList } from './components/MoviesList';
import { NewMovie } from './components/NewMovie';
import moviesFromServer from './api/movies.json';

export const App = () => {
const [movies, setMovies] = useState([...moviesFromServer]);

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={movies} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie
onAdd={(movie) => {
setMovies(prevState => [...prevState, movie]);
}}
/>
</div>
</div>
);
Expand Down
85 changes: 72 additions & 13 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 React, { 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);
// 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]*))?)$/;

type Props = {
onAdd: (movie: Movie) => void
};

export const NewMovie: React.FC<Props> = ({ 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 isReadyToSubmit = [title, imgUrl, imdbUrl, imdbId]
.every(item => item !== '');

const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();
setCount(prevState => prevState + 1);

onAdd({
title: title.trim(),
description: description.trim(),
imdbId: imdbId.trim(),
imdbUrl: imdbUrl.trim(),
imgUrl: imgUrl.trim(),
});

[setTitle, setDescription, setImgUrl, setImdbUrl, setImdbId]
.forEach(func => func(''));
};

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={title}
onChange={(value) => {
setTitle(value);
}}
required
/>

<TextField
name="description"
label="Description"
value=""
value={description}
onChange={(value) => {
setDescription(value);
}}
/>

<TextField
name="imgUrl"
label="Image URL"
value=""
label="Img URL"
value={imgUrl}
onChange={(value) => {
setImgUrl(value);
}}
validate={(value) => {
return pattern.test(value);
}}
required
/>

<TextField
name="imdbUrl"
label="Imdb URL"
value=""
value={imdbUrl}
onChange={(value) => {
setImdbUrl(value);
}}
validate={(value) => {
return pattern.test(value);
}}
required
/>

<TextField
name="imdbId"
label="Imdb ID"
value=""
value={imdbId}
onChange={(value) => {
setImdbId(value);
}}
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={!isReadyToSubmit}
>
Add
</button>
Expand Down
15 changes: 13 additions & 2 deletions src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Props = {
placeholder?: string,
required?: boolean,
onChange?: (newValue: string) => void,
validate?: (value: string) => boolean,
};

function getRandomDigits() {
Expand All @@ -23,12 +24,14 @@ export const TextField: React.FC<Props> = ({
placeholder = `Enter ${label}`,
required = false,
onChange = () => {},
validate = () => true,
}) => {
// 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 [isValid, setIsValid] = useState(true);
const hasError = touched && required && !value;

return (
Expand All @@ -43,18 +46,26 @@ export const TextField: React.FC<Props> = ({
id={id}
data-cy={`movie-${name}`}
className={classNames('input', {
'is-danger': hasError,
'is-danger': hasError || !isValid,
})}
placeholder={placeholder}
value={value}
onChange={event => onChange(event.target.value)}
onChange={event => {
const fieldValue = event.target.value;

setIsValid(validate(fieldValue) || fieldValue === '');
onChange(fieldValue);
}}
onBlur={() => setTouched(true)}
/>
</div>

{hasError && (
<p className="help is-danger">{`${label} is required`}</p>
)}
{!isValid && (
<p className="help is-danger">{`${label} is incorrect`}</p>
)}
</div>
);
};

0 comments on commit eee5de4

Please sign in to comment.