-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
base: master
Are you sure you want to change the base?
Solution #2673
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
event!.preventDefault(); | ||
const newMovie: Movie = { | ||
title: title, | ||
description: description, | ||
imgUrl: imageUrl, | ||
imdbUrl: imdbUrl, | ||
imdbId: imdbId, | ||
}; | ||
|
||
if (!Object.values(newMovie).some(value => !value)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider implementing validation logic for required fields on |
||
onAdd(newMovie); | ||
handleResetForm(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider implementing advanced validation for the |
||
/> | ||
|
||
<TextField name="imdbUrl" label="Imdb URL" value="" /> | ||
<TextField | ||
name="imdbUrl" | ||
label="Imdb URL" | ||
value={imdbUrl} | ||
onChange={(newValue: string) => setImdbUrl(newValue)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider implementing advanced validation for the |
||
/> | ||
|
||
<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> | ||
|
There was a problem hiding this comment.
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.