-
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
added logic for add form #1804
base: master
Are you sure you want to change the base?
added logic for add form #1804
Changes from 1 commit
7524374
de84850
b95207a
b51e977
0873bd8
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,58 +1,93 @@ | ||||||
import { 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); | ||||||
|
||||||
return ( | ||||||
<form className="NewMovie" key={count}> | ||||||
<h2 className="title">Add a movie</h2> | ||||||
|
||||||
<TextField | ||||||
name="title" | ||||||
label="Title" | ||||||
value="" | ||||||
onChange={() => {}} | ||||||
required | ||||||
/> | ||||||
|
||||||
<TextField | ||||||
name="description" | ||||||
label="Description" | ||||||
value="" | ||||||
/> | ||||||
|
||||||
<TextField | ||||||
name="imgUrl" | ||||||
label="Image URL" | ||||||
value="" | ||||||
/> | ||||||
|
||||||
<TextField | ||||||
name="imdbUrl" | ||||||
label="Imdb URL" | ||||||
value="" | ||||||
/> | ||||||
|
||||||
<TextField | ||||||
name="imdbId" | ||||||
label="Imdb ID" | ||||||
value="" | ||||||
/> | ||||||
|
||||||
<div className="field is-grouped"> | ||||||
<div className="control"> | ||||||
<button | ||||||
type="submit" | ||||||
data-cy="submit-button" | ||||||
className="button is-link" | ||||||
> | ||||||
Add | ||||||
</button> | ||||||
export const NewMovie | ||||||
= ({ onAdd }: { onAdd: (movie: Movie) => void }) => { | ||||||
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 formReset = () => { | ||||||
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 function name must begin with a verb.
Suggested change
|
||||||
setTitle(''); | ||||||
setDescription(''); | ||||||
setImgUrl(''); | ||||||
setImdbUrl(''); | ||||||
setImdbId(''); | ||||||
}; | ||||||
|
||||||
const isDisabled = () => { | ||||||
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.
Suggested change
|
||||||
return !(title && imgUrl && imdbUrl && imdbId); | ||||||
}; | ||||||
|
||||||
const submit = (): void => { | ||||||
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.
Suggested change
|
||||||
onAdd({ | ||||||
title, description, imgUrl, imdbUrl, imdbId, | ||||||
}); | ||||||
|
||||||
formReset(); | ||||||
|
||||||
setCount((prevCount) => prevCount + 1); | ||||||
}; | ||||||
|
||||||
return ( | ||||||
<form className="NewMovie" key={count} onSubmit={submit}> | ||||||
<h2 className="title">Add a movie</h2> | ||||||
|
||||||
<TextField | ||||||
name="title" | ||||||
label="Title" | ||||||
value={title} | ||||||
onChange={setTitle} | ||||||
required | ||||||
/> | ||||||
|
||||||
<TextField | ||||||
name="description" | ||||||
label="Description" | ||||||
onChange={setDescription} | ||||||
value={description} | ||||||
/> | ||||||
|
||||||
<TextField | ||||||
name="imgUrl" | ||||||
label="Image URL" | ||||||
value={imgUrl} | ||||||
onChange={setImgUrl} | ||||||
required | ||||||
/> | ||||||
|
||||||
<TextField | ||||||
name="imdbUrl" | ||||||
label="Imdb URL" | ||||||
value={imdbUrl} | ||||||
onChange={setImdbUrl} | ||||||
required | ||||||
/> | ||||||
|
||||||
<TextField | ||||||
name="imdbId" | ||||||
label="Imdb ID" | ||||||
value={imdbId} | ||||||
onChange={setImdbId} | ||||||
required | ||||||
/> | ||||||
|
||||||
<div className="field is-grouped"> | ||||||
<div className="control"> | ||||||
<button | ||||||
type="submit" | ||||||
data-cy="submit-button" | ||||||
className="button is-link" | ||||||
disabled={isDisabled()} | ||||||
> | ||||||
Add | ||||||
</button> | ||||||
</div> | ||||||
</div> | ||||||
</div> | ||||||
</form> | ||||||
); | ||||||
}; | ||||||
</form> | ||||||
); | ||||||
}; |
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.
All this should be kept in one state.
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.
You still have a state for each input, let's create one state for all of them. and one handler to update the state when
onChange
event is triggered.Your default state can looks like: