-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
add react form #2125
base: master
Are you sure you want to change the base?
add react form #2125
Changes from 1 commit
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,45 +1,140 @@ | ||
import { useState } from 'react'; | ||
import React, { useState } from 'react'; | ||
import { TextField } from '../TextField'; | ||
import { Movie } from '../../types/Movie'; | ||
|
||
export const NewMovie = () => { | ||
type Props = { | ||
onAdd: (movie: 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 [imgUrl, setImgUrl] = useState(''); | ||
// const [imdbUrl, setImdbUrl] = useState(''); | ||
// const [imdbId, setImdbId] = useState(''); | ||
|
||
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. remove comments |
||
const [movieData, setMovieData] = useState({ | ||
count: 0, | ||
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. move default state object to a variable |
||
title: '', | ||
description: '', | ||
imgUrl: '', | ||
imdbUrl: '', | ||
imdbId: '', | ||
}); | ||
|
||
const { | ||
count, | ||
title, | ||
description, | ||
imgUrl, | ||
imdbUrl, | ||
imdbId, | ||
} = movieData; | ||
|
||
const reset = () => { | ||
setMovieData((prevData) => ({ | ||
...prevData, | ||
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. just pass default state object |
||
count: prevData.count + 1, | ||
title: '', | ||
description: '', | ||
imgUrl: '', | ||
imdbUrl: '', | ||
imdbId: '', | ||
})); | ||
}; | ||
|
||
const handleAdd = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
onAdd({ | ||
title, | ||
description, | ||
imgUrl, | ||
imdbUrl, | ||
imdbId, | ||
}); | ||
reset(); | ||
}; | ||
|
||
const checkCorrect = !title.trim() | ||
|| !imgUrl.trim() | ||
|| !imdbUrl.trim() | ||
|| !imdbId.trim(); | ||
|
||
return ( | ||
<form className="NewMovie" key={count}> | ||
<form | ||
className="NewMovie" | ||
key={count} | ||
onSubmit={handleAdd} | ||
> | ||
<h2 className="title">Add a movie</h2> | ||
|
||
<TextField | ||
name="title" | ||
label="Title" | ||
value="" | ||
onChange={() => {}} | ||
value={title} | ||
onChange={(newValue) => setMovieData( | ||
{ | ||
...movieData, | ||
title: newValue, | ||
}, | ||
)} | ||
required | ||
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. move this onClick handler to an own function so you'll reuse it for cases below as well |
||
/> | ||
|
||
<TextField | ||
name="description" | ||
label="Description" | ||
value="" | ||
value={description} | ||
onChange={(newValue) => setMovieData( | ||
{ | ||
...movieData, | ||
description: newValue, | ||
}, | ||
)} | ||
/> | ||
|
||
<TextField | ||
name="imgUrl" | ||
label="Image URL" | ||
value="" | ||
value={imgUrl} | ||
onChange={(newValue) => setMovieData( | ||
{ | ||
...movieData, | ||
imgUrl: newValue, | ||
}, | ||
)} | ||
required | ||
/> | ||
|
||
<TextField | ||
name="imdbUrl" | ||
label="Imdb URL" | ||
value="" | ||
value={imdbUrl} | ||
onChange={(newValue) => setMovieData( | ||
{ | ||
...movieData, | ||
imdbUrl: newValue, | ||
}, | ||
)} | ||
required | ||
/> | ||
|
||
<TextField | ||
name="imdbId" | ||
label="Imdb ID" | ||
value="" | ||
value={imdbId} | ||
onChange={(newValue) => setMovieData( | ||
{ | ||
...movieData, | ||
imdbId: newValue, | ||
}, | ||
)} | ||
required | ||
/> | ||
|
||
<div className="field is-grouped"> | ||
|
@@ -48,6 +143,7 @@ export const NewMovie = () => { | |
type="submit" | ||
data-cy="submit-button" | ||
className="button is-link" | ||
disabled={checkCorrect} | ||
> | ||
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.
you should have this state to increase counter on submit