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-add-form #2085

Open
wants to merge 4 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
16 changes: 12 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import './App.scss';
import React, { useState } from 'react';
import { MoviesList } from './components/MoviesList';
import { NewMovie } from './components/NewMovie';
import { Movie } from './types/Movie';
import moviesFromServer from './api/movies.json';
import { NewMovie } from './components/NewMovie/NewMovie';

export const App: React.FC = () => {
const [movies, setMovies] = useState<Movie[]>(moviesFromServer);

const addMovie = (newMovie: Movie) => {
setMovies(currentMovies => [...currentMovies, newMovie]);
};

export const App = () => {
return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={movies} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie onAdd={addMovie} />
</div>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/MoviesList/MoviesList.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';

import './MoviesList.scss';
import { MovieCard } from '../MovieCard';
import { Movie } from '../../types/Movie';
import './MoviesList.scss';

interface Props {
movies: Movie[];
Expand Down
17 changes: 16 additions & 1 deletion src/components/NewMovie/NewMovie.scss
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
// not empty
.new-movie-form {
display: flex;
flex-direction: column;
align-items: flex-end; /* Align items to the right */
}

.form-field {
margin-bottom: 10px; /* Add some spacing between fields */
display: flex;
flex-direction: column;
align-items: flex-end; /* Align items to the right */
}

label {
margin-bottom: 5px;
}
99 changes: 88 additions & 11 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,120 @@
import { useState } from 'react';
import { TextField } from '../TextField';
import { Movie } from '../../types/Movie';
import { urlValidate } from '../utsils/UrlValidate';

export const NewMovie = () => {
// Increase the count after successful form submission
// to reset touched status of all the `Field`s
const [count] = useState(0);
interface Props {
onAdd: (movie: Movie) => void
}

export const NewMovie: React.FC<Props> = ({ onAdd }) => {
const [count, setCount] = useState(0);

const [newMovie, setNewMovie] = useState({
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
});
Comment on lines +9 to +19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move an initial state to a variable

Suggested change
export const NewMovie: React.FC<Props> = ({ onAdd }) => {
const [count, setCount] = useState(0);
const [newMovie, setNewMovie] = useState({
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
});
const initialMovieState = {
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
}
export const NewMovie: React.FC<Props> = ({ onAdd }) => {
const [count, setCount] = useState(0);
const [newMovie, setNewMovie] = useState(initialMovieState);


const {
title,
description,
imgUrl,
imdbUrl,
imdbId,
} = newMovie;

const isSubmitDisabled = !title.trim()
|| !imgUrl.trim()
|| !imdbId.trim()
|| !imdbUrl.trim();

const [hasImgUrlError, setHasImgUrlError] = useState(false);
const [hasImdbUrlError, setHasImdbUrlError] = useState(false);

const handleInput = (event: React.ChangeEvent<HTMLInputElement>): void => {
const { name, value } = event.target;

setNewMovie(prev => ({
...prev,
[name]: value,
}));
};

const onUrlCheck = () => {
setHasImdbUrlError(urlValidate(imdbUrl));
setHasImgUrlError(urlValidate(imgUrl));
};

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

if (hasImdbUrlError || hasImgUrlError) {
return;
}

onAdd(newMovie);

setNewMovie({
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
});
Comment on lines +60 to +66

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
setNewMovie({
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
});
setNewMovie(initialMovieState);


setCount(prevCount => prevCount + 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={title}
onChange={handleInput}
required
/>

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

<TextField
name="imgUrl"
label="Image URL"
value=""
value={imgUrl}
onChange={handleInput}
required
hasUrlError={hasImgUrlError}
/>

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

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

<div className="field is-grouped">
Expand All @@ -48,6 +123,8 @@ export const NewMovie = () => {
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={isSubmitDisabled}
onClick={onUrlCheck}
>
Add
</button>
Expand Down
1 change: 0 additions & 1 deletion src/components/NewMovie/index.ts

This file was deleted.

17 changes: 11 additions & 6 deletions src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import classNames from 'classnames';
import React, { useState } from 'react';
import classNames from 'classnames';

type Props = {
name: string,
value: string,
label?: string,
placeholder?: string,
required?: boolean,
onChange?: (newValue: string) => void,
onChange: (newValue: React.ChangeEvent<HTMLInputElement>) => void,
hasUrlError?: boolean,
};

function getRandomDigits() {
Expand All @@ -23,11 +24,10 @@ export const TextField: React.FC<Props> = ({
placeholder = `Enter ${label}`,
required = false,
onChange = () => {},
hasUrlError,
}) => {
// 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;

Expand All @@ -39,22 +39,27 @@ export const TextField: React.FC<Props> = ({

<div className="control">
<input
name={name}
type="text"
id={id}
data-cy={`movie-${name}`}
className={classNames('input', {
'is-danger': hasError,
'is-danger': hasError || hasUrlError,
})}
placeholder={placeholder}
value={value}
onChange={event => onChange(event.target.value)}
onChange={event => onChange(event)}
onBlur={() => setTouched(true)}
/>
</div>

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

{hasUrlError && value && (
<p className="help is-danger">{`${label} is incorrect`}</p>
)}
</div>
);
};
6 changes: 6 additions & 0 deletions src/components/utsils/UrlValidate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// 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]*))?)$/;

export const urlValidate = (url: string): boolean => {
return !pattern.test(url);
};
Loading