-
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 #2090
base: master
Are you sure you want to change the base?
solution #2090
Changes from 4 commits
6a8275a
852fab6
de763bc
57cc9ad
f7b1c65
789d11d
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,56 +1,155 @@ | ||||||||||||
import { useState } from 'react'; | ||||||||||||
import React, { useState } from 'react'; | ||||||||||||
import { TextField } from '../TextField'; | ||||||||||||
import { Movie } from '../../types/Movie'; | ||||||||||||
|
||||||||||||
export const NewMovie = () => { | ||||||||||||
export interface NewMovieProps { | ||||||||||||
onAdd: (movie: Movie) => void; | ||||||||||||
} | ||||||||||||
|
||||||||||||
export const NewMovie: React.FC<NewMovieProps> = ({ onAdd }) => { | ||||||||||||
// Increase the count after successful form submission | ||||||||||||
// to reset touched status of all the `Field`s | ||||||||||||
const [count] = useState(0); | ||||||||||||
const defaultMovie = { | ||||||||||||
title: '', | ||||||||||||
description: '', | ||||||||||||
imgUrl: '', | ||||||||||||
imdbUrl: '', | ||||||||||||
imdbId: '', | ||||||||||||
}; | ||||||||||||
|
||||||||||||
const [count, setCount] = useState(0); | ||||||||||||
const [newMovie, setNewMovie] = useState(defaultMovie); | ||||||||||||
|
||||||||||||
const ableToAddMovie = newMovie.title.trim() | ||||||||||||
&& newMovie.imgUrl.trim() && newMovie.imdbUrl.trim() | ||||||||||||
&& newMovie.imdbId.trim(); | ||||||||||||
|
||||||||||||
const createMovie = (): Movie => { | ||||||||||||
return { | ||||||||||||
title: newMovie.title, | ||||||||||||
description: newMovie.description, | ||||||||||||
imgUrl: newMovie.imgUrl, | ||||||||||||
imdbUrl: newMovie.imdbUrl, | ||||||||||||
imdbId: newMovie.imdbId, | ||||||||||||
}; | ||||||||||||
}; | ||||||||||||
|
||||||||||||
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. are you sure you need this function? |
||||||||||||
const submitAddMovie = () => { | ||||||||||||
const movie = createMovie(); | ||||||||||||
|
||||||||||||
setNewMovie(currentMovie => { | ||||||||||||
return { | ||||||||||||
...currentMovie, | ||||||||||||
title: '', | ||||||||||||
description: '', | ||||||||||||
imgUrl: '', | ||||||||||||
imdbUrl: '', | ||||||||||||
imdbId: '', | ||||||||||||
}; | ||||||||||||
}); | ||||||||||||
|
||||||||||||
setCount((currentCount) => currentCount + 1); | ||||||||||||
|
||||||||||||
onAdd(movie); | ||||||||||||
}; | ||||||||||||
|
||||||||||||
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. you can write more simple
|
||||||||||||
return ( | ||||||||||||
<form className="NewMovie" key={count}> | ||||||||||||
<form | ||||||||||||
className="NewMovie" | ||||||||||||
key={count} | ||||||||||||
onSubmit={event => { | ||||||||||||
event.preventDefault(); | ||||||||||||
submitAddMovie(); | ||||||||||||
}} | ||||||||||||
> | ||||||||||||
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. Maybe you should move your "event.preventDefault();" to your function submitAddMovie.
Suggested change
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. and here write
|
||||||||||||
<h2 className="title">Add a movie</h2> | ||||||||||||
|
||||||||||||
<TextField | ||||||||||||
name="title" | ||||||||||||
label="Title" | ||||||||||||
value="" | ||||||||||||
onChange={() => {}} | ||||||||||||
value={newMovie.title} | ||||||||||||
onChange={(event) => { | ||||||||||||
setNewMovie(currentMovie => { | ||||||||||||
return { | ||||||||||||
...currentMovie, | ||||||||||||
title: event, | ||||||||||||
}; | ||||||||||||
}); | ||||||||||||
}} | ||||||||||||
required | ||||||||||||
/> | ||||||||||||
|
||||||||||||
<TextField | ||||||||||||
name="description" | ||||||||||||
label="Description" | ||||||||||||
value="" | ||||||||||||
value={newMovie.description} | ||||||||||||
onChange={(event) => { | ||||||||||||
setNewMovie(currentMovie => { | ||||||||||||
return { | ||||||||||||
...currentMovie, | ||||||||||||
description: event, | ||||||||||||
}; | ||||||||||||
}); | ||||||||||||
}} | ||||||||||||
/> | ||||||||||||
|
||||||||||||
<TextField | ||||||||||||
name="imgUrl" | ||||||||||||
label="Image URL" | ||||||||||||
value="" | ||||||||||||
value={newMovie.imgUrl} | ||||||||||||
onChange={(event) => { | ||||||||||||
setNewMovie(currentMovie => { | ||||||||||||
return { | ||||||||||||
...currentMovie, | ||||||||||||
imgUrl: event, | ||||||||||||
}; | ||||||||||||
}); | ||||||||||||
}} | ||||||||||||
required | ||||||||||||
/> | ||||||||||||
|
||||||||||||
<TextField | ||||||||||||
name="imdbUrl" | ||||||||||||
label="Imdb URL" | ||||||||||||
value="" | ||||||||||||
value={newMovie.imdbUrl} | ||||||||||||
onChange={(event) => { | ||||||||||||
setNewMovie(currentMovie => { | ||||||||||||
return { | ||||||||||||
...currentMovie, | ||||||||||||
imdbUrl: event, | ||||||||||||
}; | ||||||||||||
}); | ||||||||||||
}} | ||||||||||||
required | ||||||||||||
/> | ||||||||||||
|
||||||||||||
<TextField | ||||||||||||
name="imdbId" | ||||||||||||
label="Imdb ID" | ||||||||||||
value="" | ||||||||||||
value={newMovie.imdbId} | ||||||||||||
onChange={(event) => { | ||||||||||||
setNewMovie(currentMovie => { | ||||||||||||
return { | ||||||||||||
...currentMovie, | ||||||||||||
imdbId: event, | ||||||||||||
}; | ||||||||||||
}); | ||||||||||||
}} | ||||||||||||
required | ||||||||||||
/> | ||||||||||||
|
||||||||||||
<div className="field is-grouped"> | ||||||||||||
<div className="control"> | ||||||||||||
|
||||||||||||
<button | ||||||||||||
type="submit" | ||||||||||||
data-cy="submit-button" | ||||||||||||
className="button is-link" | ||||||||||||
disabled={!ableToAddMovie} | ||||||||||||
> | ||||||||||||
Add | ||||||||||||
</button> | ||||||||||||
|
||||||||||||
</div> | ||||||||||||
</div> | ||||||||||||
</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.
please create a separate handler for this