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

solution #2709

Open
wants to merge 1 commit 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 @@ -27,4 +27,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://pasha28091997.github.io/react_movies-list-add-form/) and add it to the PR description.
17 changes: 15 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@ import './App.scss';
import { MoviesList } from './components/MoviesList';
import { NewMovie } from './components/NewMovie';
import moviesFromServer from './api/movies.json';
import { useState } from 'react';

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

const handleAddMovie = (movie: {
title: string;
description: string;
imgUrl: string;
imdbUrl: string;
imdbId: string;
}) => {
setMovies(prevMovies => [...prevMovies, movie]);
};

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={movies} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie onAdd={handleAddMovie} />
</div>
</div>
);
Expand Down
155 changes: 144 additions & 11 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,170 @@
/* eslint-disable prettier/prettier */
import { useState } from 'react';
import { TextField } from '../TextField';

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: (formData: {
title: string;
description: string;
imgUrl: string;
imdbUrl: string;
imdbId: string;
}) => void;
}

export const NewMovie = ({ onAdd }: Props) => {
const [formData, setFormData] = useState({
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
});

const [errors, setErrors] = useState<
// eslint-disable-next-line prettier/prettier
// eslint-disable-next-line prettier/prettier, @typescript-eslint/indent
Partial<Record<keyof typeof formData, string>>
>({});

const handleChange = (
name: keyof typeof formData,
newValue: string,
): void => {
setFormData(prevData => ({
...prevData,
[name]: newValue,
}));

if (errors[name]) {
setErrors(prevErrors => {
const updatedErrors = { ...prevErrors };

delete updatedErrors[name];

return updatedErrors;
});
}
};

const handleBlur = (name: keyof typeof formData): void => {
if (['title', 'imgUrl', 'imdbUrl', 'imdbId'].includes(name)) {
const value = formData[name].trim();

if (!value) {
setErrors(prevErrors => ({
...prevErrors,
[name]: `${name} is required`,
}));
} else {
setErrors(prevErrors => {
const updatedErrors = { ...prevErrors };

delete updatedErrors[name];

return updatedErrors;
});
}
}
};

const isFormValid = (): boolean => {
return ['title', 'imgUrl', 'imdbUrl', 'imdbId'].every(
field => formData[field as keyof typeof formData].trim() !== '',
);
};

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

const newErrors: Partial<Record<keyof typeof formData, string>> = {};

['title', 'imgUrl', 'imdbUrl', 'imdbId'].forEach(field => {
if (!formData[field as keyof typeof formData].trim()) {
newErrors[field as keyof typeof formData] = `${field} is required`;
}
});

if (Object.keys(newErrors).length > 0) {
setErrors(newErrors);

return;
}

onAdd(formData);

setFormData({
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
});

setErrors({});
};

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

<TextField
name="title"
label="Title"
value=""
onChange={() => {}}
value={formData.title}
onChange={value => handleChange('title', value)}
onBlur={() => handleBlur('title')}
error={errors.title}
required
/>

<TextField name="description" label="Description" value="" />
<TextField
name="description"
label="Description"
value={formData.description}
onChange={value => handleChange('description', value)}
onBlur={() => handleBlur('description')}
error={errors.description}
required
/>

<TextField name="imgUrl" label="Image URL" value="" />
<TextField
name="imgUrl"
label="Image URL"
value={formData.imgUrl}
onChange={value => handleChange('imgUrl', value)}
onBlur={() => handleBlur('imgUrl')}
error={errors.imgUrl}
required
/>

<TextField name="imdbUrl" label="Imdb URL" value="" />
<TextField
name="imdbUrl"
label="Imdb URL"
value={formData.imdbUrl}
onChange={value => handleChange('imdbUrl', value)}
onBlur={() => handleBlur('imdbUrl')}
error={errors.imdbUrl}
required
/>

<TextField name="imdbId" label="Imdb ID" value="" />
<TextField
name="imdbId"
label="Imdb ID"
value={formData.imdbId}
onChange={value => handleChange('imdbId', value)}
onBlur={() => handleBlur('imdbId')}
error={errors.imdbId}
required
/>

<div className="field is-grouped">
<div className="control">
<button
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={!isFormValid()}
>
Add
</button>
Expand Down
4 changes: 3 additions & 1 deletion src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ type Props = {
label?: string;
placeholder?: string;
required?: boolean;
onChange?: (newValue: string) => void;
onChange: (newValue: string) => void;
onBlur: () => void;
error?: string;
};

function getRandomDigits() {
Expand Down
Loading