-
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
task done #2101
base: master
Are you sure you want to change the base?
task done #2101
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,45 +1,95 @@ | ||
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); | ||
export interface Props { | ||
onAdd: (movie: Movie) => void; | ||
} | ||
|
||
export const NewMovie = ({ onAdd }: Props) => { | ||
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 isAbleToAdd = title.trim() && imgUrl.trim() && imdbUrl.trim() && imdbId; | ||
|
||
const createNewMovie = (): Movie => { | ||
return { | ||
title: title.trim(), | ||
description: description.trim(), | ||
imgUrl: imgUrl.trim(), | ||
imdbUrl: imdbUrl.trim(), | ||
imdbId: imdbId.trim(), | ||
}; | ||
}; | ||
|
||
function reset() { | ||
setTitle(''); | ||
setDescription(''); | ||
setImgUrl(''); | ||
setImdbUrl(''); | ||
setImdbId(''); | ||
Comment on lines
+30
to
+34
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 function reset for it |
||
} | ||
|
||
function addNewMovie(event: React.FormEvent<HTMLFormElement>) { | ||
event.preventDefault(); | ||
const newMovie = createNewMovie(); | ||
|
||
reset(); | ||
|
||
setCount(current => current + 1); | ||
|
||
onAdd(newMovie); | ||
} | ||
|
||
return ( | ||
<form className="NewMovie" key={count}> | ||
<form | ||
className="NewMovie" | ||
key={count} | ||
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. Why you need to add this key attribute? Why you need to reset "touched status"? Can you explain please? 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. We use this key to rerender component with clear field |
||
onSubmit={addNewMovie} | ||
> | ||
<h2 className="title">Add a movie</h2> | ||
|
||
<TextField | ||
name="title" | ||
label="Title" | ||
value="" | ||
onChange={() => {}} | ||
value={title} | ||
onChange={setTitle} | ||
required | ||
/> | ||
|
||
<TextField | ||
name="description" | ||
label="Description" | ||
value="" | ||
value={description} | ||
onChange={setDescription} | ||
/> | ||
|
||
<TextField | ||
name="imgUrl" | ||
label="Image URL" | ||
value="" | ||
value={imgUrl} | ||
onChange={setImgUrl} | ||
required | ||
/> | ||
|
||
<TextField | ||
name="imdbUrl" | ||
label="Imdb URL" | ||
value="" | ||
value={imdbUrl} | ||
onChange={setImdbUrl} | ||
required | ||
/> | ||
|
||
<TextField | ||
name="imdbId" | ||
label="Imdb ID" | ||
value="" | ||
value={imdbId} | ||
onChange={setImdbId} | ||
required | ||
/> | ||
|
||
<div className="field is-grouped"> | ||
|
@@ -48,6 +98,7 @@ export const NewMovie = () => { | |
type="submit" | ||
data-cy="submit-button" | ||
className="button is-link" | ||
disabled={!isAbleToAdd} | ||
> | ||
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 can use object with type Movie and create only one state for all fields.
I think it's easier to use because you don't have to create a lot of states for each field, but your solution is also suitable (It is not necessary to change)
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.
We ask about this our teacher Misha and he said that it is not better.