Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Movies list done #2147

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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://hyrych-max.github.io/react_movies-list-add-form/) and add it to the PR description.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@mate-academy/cypress-tools": "^1.0.4",
"@mate-academy/eslint-config-react": "^0.0.11",
"@mate-academy/eslint-config-react-typescript": "^1.0.12",
"@mate-academy/scripts": "^1.2.12",
"@mate-academy/scripts": "^1.7.0",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "^0.0.11",
"@types/node": "^17.0.23",
Expand Down
16 changes: 13 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -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';
import './App.scss';

export const App = () => {
const [films, setFilms] = useState(moviesFromServer);

const addFilms = (newFilms: Movie) => {
setFilms(currentFilms => [...currentFilms, newFilms]);
};

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={films} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie onAdd={addFilms} />
</div>
</div>
);
Expand Down
93 changes: 83 additions & 10 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,117 @@
import { useState } from 'react';
import React, { useState } from 'react';

import { TextField } from '../TextField';
import { Movie } from '../../types/Movie';

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

function isValidUrl(url: string) {
const pattern = new RegExp(
'^((([A-Za-z]{3,9}:(?:\\/\\/)?)(?:[-;:&=+$,\\w]+@)?[A-Za-z0-9.-]+|'
+ '(?:www\\.|[-;:&=+$,\\w]+@)[A-Za-z0-9.-]+)((?:\\/[+~%/\\.\\w-_]*)?'
+ '\\??(?:[-+=&;%@,\\.\\w_]*)#?(?:[,.!/\\\\\\w]*))?)$',
);

return pattern.test(url);
}

export const NewMovie = () => {
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 [newMovie, setNewMovie] = useState({
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
});

let isDisabled = true;

if (newMovie.title.trim()
&& newMovie.imgUrl.trim()
&& newMovie.imdbUrl.trim()
&& newMovie.imdbId.trim()
&& isValidUrl(newMovie.imgUrl)
&& isValidUrl(newMovie.imdbUrl)) {
isDisabled = false;
}

const reset = () => {
setNewMovie({
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
});
};

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

if (!newMovie.title || !newMovie.imgUrl
|| !newMovie.imdbUrl || !newMovie.imdbId) {
return;
}

onAdd(newMovie);
setCount(currentCount => currentCount + 1);

reset();
};

return (
<form className="NewMovie" key={count}>
<form
className="NewMovie"
key={count}
onSubmit={(el) => {
setCount(count + 1);
handleSumbit(el);
}}
>
<h2 className="title">Add a movie</h2>

<TextField
name="title"
label="Title"
value=""
onChange={() => {}}
value={newMovie.title}
onChange={(value) => setNewMovie({ ...newMovie, title: value })}
required
/>

<TextField
name="description"
label="Description"
value=""
value={newMovie.description}
onChange={(value) => setNewMovie({ ...newMovie, description: value })}
/>

<TextField
name="imgUrl"
label="Image URL"
value=""
value={newMovie.imgUrl}
onChange={(value) => setNewMovie({ ...newMovie, imgUrl: value })}
required
/>

<TextField
name="imdbUrl"
label="Imdb URL"
value=""
value={newMovie.imdbUrl}
onChange={(value) => setNewMovie({ ...newMovie, imdbUrl: value })}
required
/>

<TextField
name="imdbId"
label="Imdb ID"
value=""
value={newMovie.imdbId}
onChange={(value) => setNewMovie({ ...newMovie, imdbId: value })}
required
/>

<div className="field is-grouped">
Expand All @@ -48,6 +120,7 @@ export const NewMovie = () => {
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={isDisabled}
>
Add
</button>
Expand Down
14 changes: 13 additions & 1 deletion src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@ export const TextField: React.FC<Props> = ({
required = false,
onChange = () => {},
}) => {
const pattern = new RegExp(
'^((([A-Za-z]{3,9}:(?:\\/\\/)?)(?:[-;:&=+$,\\w]+@)?[A-Za-z0-9.-]+|'
+ '(?:www\\.|[-;:&=+$,\\w]+@)[A-Za-z0-9.-]+)((?:\\/[+~%/\\.\\w-_]*)?'
+ '\\??(?:[-+=&;%@,\\.\\w_]*)#?(?:[,.!/\\\\\\w]*))?)$',
);
// 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 validation = pattern.test(value);
const invalidData = !validation && (name === 'imgUrl' || name === 'imdbUrl')
&& touched;

return (
<div className="field">
Expand All @@ -43,7 +51,7 @@ export const TextField: React.FC<Props> = ({
id={id}
data-cy={`movie-${name}`}
className={classNames('input', {
'is-danger': hasError,
'is-danger': hasError || invalidData,
})}
placeholder={placeholder}
value={value}
Expand All @@ -55,6 +63,10 @@ export const TextField: React.FC<Props> = ({
{hasError && (
<p className="help is-danger">{`${label} is required`}</p>
)}

{invalidData && (
<p className="help is-danger">{`${label} is not valid`}</p>
)}
</div>
);
};
15 changes: 9 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"extends": "@mate-academy/students-ts-config",
"include": [
"src"
]
}
{
"compilerOptions": {
"jsx": "react-jsx"
},
"extends": "@mate-academy/students-ts-config",
"include": [
"src"
]
}
Loading