-
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
solution #2720
base: master
Are you sure you want to change the base?
solution #2720
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,105 @@ | ||||||
import { useState } from 'react'; | ||||||
import { FC, useState } from 'react'; | ||||||
import { Movie } from '../../types/Movie'; | ||||||
import { TextField } from '../TextField'; | ||||||
|
||||||
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; | ||||||
} | ||||||
|
||||||
type InputTypes = 'title' | 'description' | 'imgUrl' | 'imdbUrl' | 'imdbId'; | ||||||
|
||||||
export const NewMovie: FC<Props> = ({ onAdd }) => { | ||||||
const [count, setCount] = useState(0); | ||||||
const [movies, setMovies] = 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. The |
||||||
title: '', | ||||||
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
|
||||||
description: '', | ||||||
imgUrl: '', | ||||||
imdbUrl: '', | ||||||
imdbId: '', | ||||||
}); | ||||||
|
||||||
Comment on lines
+13
to
+19
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 renaming the |
||||||
const validInputs = () => { | ||||||
const { title, imgUrl, imdbUrl, imdbId } = movies; | ||||||
|
||||||
return title.trim() && imgUrl.trim() && imdbUrl.trim() && imdbId.trim(); | ||||||
}; | ||||||
|
||||||
function handleInputChange(name: InputTypes, value: string) { | ||||||
setMovies(prevMovie => ({ | ||||||
...prevMovie, | ||||||
[name]: value, | ||||||
})); | ||||||
} | ||||||
|
||||||
const reset = () => { | ||||||
setMovies({ | ||||||
title: '', | ||||||
description: '', | ||||||
imgUrl: '', | ||||||
imdbUrl: '', | ||||||
imdbId: '', | ||||||
}); | ||||||
}; | ||||||
|
||||||
const handleSubmit = (event: React.FormEvent) => { | ||||||
event.preventDefault(); | ||||||
onAdd(movies); | ||||||
reset(); | ||||||
setCount(count + 1); | ||||||
}; | ||||||
|
||||||
return ( | ||||||
<form className="NewMovie" key={count}> | ||||||
<form onSubmit={handleSubmit} className="NewMovie" key={count}> | ||||||
<h2 className="title">Add a movie</h2> | ||||||
|
||||||
<TextField | ||||||
name="title" | ||||||
label="Title" | ||||||
value="" | ||||||
onChange={() => {}} | ||||||
value={movies.title} | ||||||
onChange={value => { | ||||||
handleInputChange('title', value); | ||||||
}} | ||||||
required | ||||||
/> | ||||||
|
||||||
<TextField name="description" label="Description" value="" /> | ||||||
<TextField | ||||||
name="description" | ||||||
label="Description" | ||||||
value={movies.description} | ||||||
onChange={value => handleInputChange('description', value)} | ||||||
/> | ||||||
|
||||||
<TextField name="imgUrl" label="Image URL" value="" /> | ||||||
<TextField | ||||||
name="imgUrl" | ||||||
label="Image URL" | ||||||
required | ||||||
value={movies.imgUrl} | ||||||
onChange={value => handleInputChange('imgUrl', value)} | ||||||
/> | ||||||
|
||||||
<TextField name="imdbUrl" label="Imdb URL" value="" /> | ||||||
<TextField | ||||||
name="imdbUrl" | ||||||
label="Imdb URL" | ||||||
required | ||||||
value={movies.imdbUrl} | ||||||
onChange={value => handleInputChange('imdbUrl', value)} | ||||||
/> | ||||||
|
||||||
<TextField name="imdbId" label="Imdb ID" value="" /> | ||||||
<TextField | ||||||
name="imdbId" | ||||||
label="Imdb ID" | ||||||
value={movies.imdbId} | ||||||
required | ||||||
onChange={value => handleInputChange('imdbId', value)} | ||||||
/> | ||||||
|
||||||
<div className="field is-grouped"> | ||||||
<div className="control"> | ||||||
<button | ||||||
type="submit" | ||||||
data-cy="submit-button" | ||||||
className="button is-link" | ||||||
disabled={!validInputs()} | ||||||
> | ||||||
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 are using the spread operator to create a copy of
moviesFromServer
when initializing themovies
state. According to the checklist, if you are using a non-mutating array method, you don't need to create a copy of the array. Consider removing the spread operator:useState<Movie[]>(moviesFromServer);
.