-
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
Develop #2714
base: master
Are you sure you want to change the base?
Develop #2714
Changes from 2 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,122 @@ | ||
import { useState } from 'react'; | ||
import { TextField } from '../TextField'; | ||
import { Movie } from '../../types/Movie'; | ||
type Props = { | ||
onAdd: (movie: Movie) => void; | ||
}; | ||
|
||
export const NewMovie = () => { | ||
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(''); | ||
// const [hasError, setError] = useState(false); | ||
|
||
const handleTitle = (val: string) => { | ||
setTitle(val); | ||
}; | ||
|
||
const handleDescription = (val: string) => { | ||
setDescription(val); | ||
}; | ||
|
||
const handleImgUrl = (val: string) => { | ||
setImgUrl(val); | ||
}; | ||
|
||
const handleImdbUrl = (val: string) => { | ||
setImdbUrl(val); | ||
}; | ||
|
||
const handleImdbUId = (val: string) => { | ||
setImdbId(val); | ||
}; | ||
|
||
const trimedTitle = title.trim(); | ||
const trimedDescription = description.trim(); | ||
const trimedImgUrl = imgUrl.trim(); | ||
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. There are typos in the variable names |
||
const trimmedImdbUrl = imdbUrl.trim(); | ||
const trimmedImdbId = imdbId.trim(); | ||
|
||
const newMovie = { | ||
title: trimedTitle, | ||
description: trimedDescription, | ||
imgUrl: trimedImgUrl, | ||
imdbUrl: trimmedImdbUrl, | ||
imdbId: trimmedImdbId, | ||
}; | ||
|
||
const handleSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
|
||
if (trimedTitle || trimedImgUrl || trimmedImdbUrl || trimmedImdbId) { | ||
onAdd(newMovie); | ||
} | ||
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 condition in the |
||
|
||
setTitle(''); | ||
setDescription(''); | ||
setImgUrl(''); | ||
setImdbUrl(''); | ||
setImdbId(''); | ||
setCount(prev => prev + 1); | ||
}; | ||
|
||
return ( | ||
<form className="NewMovie" key={count}> | ||
<form className="NewMovie" key={count} onSubmit={handleSubmit}> | ||
<h2 className="title">Add a movie</h2> | ||
|
||
<TextField | ||
name="title" | ||
label="Title" | ||
value="" | ||
onChange={() => {}} | ||
value={title} | ||
onChange={handleTitle} | ||
required | ||
/> | ||
|
||
<TextField name="description" label="Description" value="" /> | ||
<TextField | ||
name="description" | ||
label="Description" | ||
value={description} | ||
onChange={handleDescription} | ||
/> | ||
|
||
<TextField name="imgUrl" label="Image URL" value="" /> | ||
<TextField | ||
name="imgUrl" | ||
label="Image URL" | ||
value={imgUrl} | ||
onChange={handleImgUrl} | ||
required | ||
/> | ||
|
||
<TextField name="imdbUrl" label="Imdb URL" value="" /> | ||
<TextField | ||
name="imdbUrl" | ||
label="Imdb URL" | ||
value={imdbUrl} | ||
onChange={handleImdbUrl} | ||
required | ||
/> | ||
|
||
<TextField name="imdbId" label="Imdb ID" value="" /> | ||
<TextField | ||
name="imdbId" | ||
label="Imdb ID" | ||
value={imdbId} | ||
onChange={handleImdbUId} | ||
required | ||
/> | ||
|
||
<div className="field is-grouped"> | ||
<div className="control"> | ||
<button | ||
type="submit" | ||
data-cy="submit-button" | ||
className="button is-link" | ||
disabled={ | ||
!trimedTitle || !trimedImgUrl || !trimmedImdbUrl || !trimmedImdbId | ||
} | ||
> | ||
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.
There is a typo in the function name
handleImdbUId
. It should behandleImdbId
to maintain consistency with the state variableimdbId
.