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 #2673

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
11 changes: 9 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ 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);

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={movies} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie

Choose a reason for hiding this comment

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

Ensure that the NewMovie component is properly handling form submissions and validations. Refer to the previous review comments regarding form submission handling, state update logic, and validation.

onAdd={newMovie => {
setMovies(prevMovies => [...prevMovies, newMovie]);
}}
/>
</div>
</div>
);
Expand Down
80 changes: 71 additions & 9 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,99 @@
import { useState } from 'react';
import { TextField } from '../TextField';
import { Movie } from '../../types/Movie';

export const NewMovie = () => {
type Props = {
onAdd: (newMovie: 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 [title, setTitle] = useState('');
const [description, setDescription] = useState('');
const [imageUrl, setImageUrl] = useState('');
const [imdbUrl, setImdbUrl] = useState('');
const [imdbId, setImdbId] = useState('');

const handleResetForm = () => {
setCount(prevKey => prevKey + 1); // оновлюємо ключ для реініціалізації
setTitle('');
setDescription('');
setImageUrl('');
setImdbUrl('');
setImdbId('');
};

const isFormValid = [title, description, imageUrl, imdbUrl, imdbId].every(
value => value.trim() !== '',
);

return (
<form className="NewMovie" key={count}>
<form
className="NewMovie"
key={count}
onSubmit={event => {

Choose a reason for hiding this comment

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

The onSubmit function correctly receives the event parameter and calls preventDefault(), which is a good practice to prevent the default form submission behavior.

event!.preventDefault();
const newMovie: Movie = {
title: title,
description: description,
imgUrl: imageUrl,
imdbUrl: imdbUrl,
imdbId: imdbId,
};

if (!Object.values(newMovie).some(value => !value)) {

Choose a reason for hiding this comment

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

Consider implementing validation logic for required fields on onBlur to provide immediate feedback to users when fields are left empty. This will improve the user experience by ensuring that users are aware of any missing information before attempting to submit the form.

onAdd(newMovie);
handleResetForm();

Choose a reason for hiding this comment

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

Ensure that errors are cleared when the form is reset. This will improve the user experience by providing a clean slate after each submission.

}
}}
>
<h2 className="title">Add a movie</h2>

<TextField
name="title"
label="Title"
value=""
onChange={() => {}}
value={title}
onChange={(newValue: string) => setTitle(newValue)}
required
/>

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

<TextField name="imgUrl" label="Image URL" value="" />
<TextField
name="imgUrl"
label="Image URL"
value={imageUrl}
onChange={(newValue: string) => setImageUrl(newValue)}

Choose a reason for hiding this comment

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

Consider implementing advanced validation for the imgUrl field to ensure that the input is a valid URL. This will enhance the robustness of your form.

/>

<TextField name="imdbUrl" label="Imdb URL" value="" />
<TextField
name="imdbUrl"
label="Imdb URL"
value={imdbUrl}
onChange={(newValue: string) => setImdbUrl(newValue)}

Choose a reason for hiding this comment

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

Consider implementing advanced validation for the imdbUrl field to ensure that the input is a valid URL. This will enhance the robustness of your form.

/>

<TextField name="imdbId" label="Imdb ID" value="" />
<TextField
name="imdbId"
label="Imdb ID"
value={imdbId}
onChange={(newValue: string) => setImdbId(newValue)}
/>

<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
Loading