-
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 #2683
base: master
Are you sure you want to change the base?
Solution #2683
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,117 @@ | ||
import { useState } from 'react'; | ||
import React, { useState } from 'react'; | ||
import { TextField } from '../TextField'; | ||
import { Movie } from '../../types/Movie'; | ||
|
||
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; | ||
} | ||
|
||
enum Fields { | ||
title = 'Title', | ||
description = 'Description', | ||
imgUrl = 'Image URL', | ||
imdbUrl = 'Imdb URL', | ||
imdbId = 'Imdb ID', | ||
} | ||
|
||
const isValid = (value: string): boolean => { | ||
return value.trim().length > 0; | ||
}; | ||
|
||
export const NewMovie: React.FC<Props> = ({ onAdd }) => { | ||
const [count, setCount] = useState(0); | ||
const [title, setTitle] = useState(''); | ||
const [description, setDescription] = useState(''); | ||
const [imgUrl, setImgUrl] = useState(''); | ||
const [imdbUrl, setImdbUrl] = useState(''); | ||
const [imdbId, setImdbId] = useState(''); | ||
|
||
const reset = () => { | ||
setCount(0); | ||
setTitle(''); | ||
setDescription(''); | ||
setImgUrl(''); | ||
setImdbUrl(''); | ||
setImdbId(''); | ||
}; | ||
|
||
const handleFormSubmit = (event: React.FormEvent): void => { | ||
event.preventDefault(); | ||
|
||
const newMovie = { | ||
title: title.trim(), | ||
description: description.trim(), | ||
imgUrl: imgUrl.trim(), | ||
imdbUrl: imdbUrl.trim(), | ||
imdbId: imdbId.trim(), | ||
}; | ||
|
||
onAdd(newMovie); | ||
|
||
setCount(count + 1); | ||
reset(); | ||
}; | ||
|
||
return ( | ||
<form className="NewMovie" key={count}> | ||
<form | ||
className="NewMovie" | ||
key={count} | ||
onSubmit={handleFormSubmit} | ||
onReset={reset} | ||
> | ||
<h2 className="title">Add a movie</h2> | ||
Comment on lines
+56
to
+61
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 |
||
|
||
<TextField | ||
name="title" | ||
label="Title" | ||
value="" | ||
onChange={() => {}} | ||
label={Fields.title} | ||
value={title} | ||
onChange={event => setTitle(event.target.value)} | ||
required | ||
/> | ||
|
||
<TextField name="description" label="Description" value="" /> | ||
<TextField | ||
name="description" | ||
label={Fields.description} | ||
value={description} | ||
onChange={event => setDescription(event.target.value)} | ||
/> | ||
|
||
<TextField name="imgUrl" label="Image URL" value="" /> | ||
<TextField | ||
name="imgUrl" | ||
label={Fields.imgUrl} | ||
value={imgUrl} | ||
onChange={event => setImgUrl(event.target.value)} | ||
required | ||
/> | ||
|
||
<TextField name="imdbUrl" label="Imdb URL" value="" /> | ||
<TextField | ||
name="imdbUrl" | ||
label={Fields.imdbUrl} | ||
value={imdbUrl} | ||
onChange={event => setImdbUrl(event.target.value)} | ||
required | ||
/> | ||
|
||
<TextField name="imdbId" label="Imdb ID" value="" /> | ||
<TextField | ||
name="imdbId" | ||
label={Fields.imdbId} | ||
value={imdbId} | ||
onChange={event => setImdbId(event.target.value)} | ||
required | ||
/> | ||
|
||
<div className="field is-grouped"> | ||
<div className="control"> | ||
<button | ||
type="submit" | ||
data-cy="submit-button" | ||
className="button is-link" | ||
disabled={ | ||
!isValid(title) || | ||
!isValid(imgUrl) || | ||
!isValid(imdbUrl) || | ||
!isValid(imdbId) | ||
} | ||
> | ||
Comment on lines
+109
to
+114
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 the submit button is disabled until all required fields are filled with valid, trimmed values. This is a task requirement to prevent submission of incomplete data. |
||
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.
Change the form's key to reinitialize it after resetting to clear errors. This is necessary to ensure errors are not shown after clearing the form, as per the task requirements.