-
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 task react_movies-list-add-form #2120
base: master
Are you sure you want to change the base?
Conversation
src/components/NewMovie/NewMovie.tsx
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
we can combine it in one useState
const [newMovie, setNewMovie] = useState({
title: '',
...
})
now we can also create only one handle function for all fields
const handleChange = (e: ...) => {
const {name, value} = e.target;
setMovie((prevMovie) => ({...prevMovie, [name]: value}))
}
don't forget to specify name attr to input in TextField
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.
Almost done.
Please check a few comments.
src/components/NewMovie/NewMovie.tsx
Outdated
label="Image URL" | ||
value="" | ||
value={newMovie.imgUrl} | ||
onChange={e => handleChange(e)} |
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.
onChange={e => handleChange(e)} | |
onChange={handleChange} |
it will be the same
src/components/NewMovie/NewMovie.tsx
Outdated
label="Imdb URL" | ||
value="" | ||
value={newMovie.imdbUrl} | ||
onChange={e => handleChange(e)} |
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.
onChange={e => handleChange(e)} | |
onChange={handleChange} |
it will be the same
src/components/NewMovie/NewMovie.tsx
Outdated
disabled={!newMovie.title | ||
|| !urlValidator(newMovie.imgUrl) | ||
|| !urlValidator(newMovie.imdbUrl) | ||
|| !newMovie.imdbId} |
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.
better to move in constant.
const isDisabled = !newMovie.title.trim() || !urlValidator(newMovie.imgUrl) || !urlValidator(newMovie.imdbUrl) || !newMovie.imdbId.trim()
disabled={!newMovie.title | |
|| !urlValidator(newMovie.imgUrl) | |
|| !urlValidator(newMovie.imdbUrl) | |
|| !newMovie.imdbId} | |
disabled={isDisabled} |
trim for other required values will solve such problem as on screencast
src/App.tsx
Outdated
|
||
export const App = () => { | ||
const [movieList, setMovieList] = useState<Movie[]>([...moviesFromServer]); |
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.
const [movieList, setMovieList] = useState<Movie[]>([...moviesFromServer]); | |
const [movieList, setMovieList] = useState<Movie[]>(moviesFromServer); |
redundant to destructure
src/components/NewMovie/NewMovie.tsx
Outdated
// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
do not keep commented code
@@ -47,14 +50,16 @@ export const TextField: React.FC<Props> = ({ | |||
})} | |||
placeholder={placeholder} | |||
value={value} | |||
onChange={event => onChange(event.target.value)} | |||
onChange={event => onChange(event)} | |||
onBlur={() => setTouched(true)} | |||
/> | |||
</div> | |||
|
|||
{hasError && ( |
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.
trim value in hasError counting for correct show error message when fill input only with empty spaces.
const hasError = touched && required && !value.trim();
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.
Nice!
Some minor changes needed
src/components/NewMovie/NewMovie.tsx
Outdated
imdbId: '', | ||
}); | ||
|
||
const urlValidator = (urlString: string): boolean => { |
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.
move this helper outside the component
src/components/NewMovie/NewMovie.tsx
Outdated
onAdd: ( | ||
movie: { | ||
title: string, | ||
description: string, | ||
imgUrl: string, | ||
imdbUrl: string, | ||
imdbId: string, | ||
} | ||
) => void |
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 have a type for a movie
onAdd: ( | |
movie: { | |
title: string, | |
description: string, | |
imgUrl: string, | |
imdbUrl: string, | |
imdbId: string, | |
} | |
) => void | |
onAdd: ( | |
movie: (movie: Movie) => void |
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.
isn't fixed
src/components/NewMovie/NewMovie.tsx
Outdated
const [newMovie, setNewMovie] = useState({ | ||
title: '', | ||
description: '', | ||
imgUrl: '', | ||
imdbUrl: '', | ||
imdbId: '', | ||
}); |
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.
move initial state to a variable initialMovieState
(outside the component)
const [newMovie, setNewMovie] = useState({ | |
title: '', | |
description: '', | |
imgUrl: '', | |
imdbUrl: '', | |
imdbId: '', | |
}); | |
const [newMovie, setNewMovie] = useState<Movie>(initialMovieState); |
src/components/NewMovie/NewMovie.tsx
Outdated
setNewMovie({ | ||
title: '', | ||
description: '', | ||
imgUrl: '', | ||
imdbUrl: '', | ||
imdbId: '', | ||
}); |
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.
setNewMovie({ | |
title: '', | |
description: '', | |
imgUrl: '', | |
imdbUrl: '', | |
imdbId: '', | |
}); | |
setNewMovie(initialMovie); |
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.
Almost done!
Yet you've missed this comment
https://github.com/mate-academy/react_movies-list-add-form/pull/2120/files#r1462388794
src/components/NewMovie/NewMovie.tsx
Outdated
onAdd: ( | ||
movie: { | ||
title: string, | ||
description: string, | ||
imgUrl: string, | ||
imdbUrl: string, | ||
imdbId: string, | ||
} | ||
) => void |
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.
isn't fixed
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.
lgtm
DEMO LINK