-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
movies list add form v1 #1889
base: master
Are you sure you want to change the base?
movies list add form v1 #1889
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,45 +1,91 @@ | ||
import { useState } from 'react'; | ||
import React, { useState } from 'react'; | ||
import { TextField } from '../TextField'; | ||
import { Movie } from '../../types/Movie'; | ||
|
||
export const NewMovie = () => { | ||
type Props = { | ||
onAdd: (movie: Movie) => void | ||
}; | ||
|
||
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(''); | ||
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. Combine it into 1 state |
||
|
||
const readyToSubmit = [title, imgUrl, imdbUrl, imdbId] | ||
.every(item => item !== ''); | ||
|
||
const handleSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
setCount(prevState => prevState + 1); | ||
|
||
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. before adding a movie you should validate values. Right now I can just press |
||
onAdd({ | ||
title: title.trim(), | ||
description: description.trim(), | ||
imdbId: imdbId.trim(), | ||
imdbUrl: imdbUrl.trim(), | ||
imgUrl: imgUrl.trim(), | ||
}); | ||
|
||
[setTitle, setDescription, setImgUrl, setImdbUrl, setImdbId] | ||
.forEach(func => func('')); | ||
}; | ||
|
||
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.trim()} | ||
onChange={(value) => { | ||
setTitle(value); | ||
}} | ||
required | ||
/> | ||
|
||
<TextField | ||
name="description" | ||
label="Description" | ||
value="" | ||
value={description} | ||
onChange={(value) => { | ||
setDescription(value); | ||
}} | ||
/> | ||
|
||
<TextField | ||
name="imgUrl" | ||
label="Image URL" | ||
value="" | ||
value={imgUrl} | ||
onChange={(value) => { | ||
setImgUrl(value); | ||
}} | ||
required | ||
/> | ||
|
||
<TextField | ||
name="imdbUrl" | ||
label="Imdb URL" | ||
value="" | ||
value={imdbUrl} | ||
onChange={(value) => { | ||
setImdbUrl(value); | ||
}} | ||
required | ||
/> | ||
|
||
<TextField | ||
name="imdbId" | ||
label="Imdb ID" | ||
value="" | ||
value={imdbId} | ||
onChange={(value) => { | ||
setImdbId(value); | ||
}} | ||
/> | ||
|
||
<div className="field is-grouped"> | ||
|
@@ -48,6 +94,7 @@ export const NewMovie = () => { | |
type="submit" | ||
data-cy="submit-button" | ||
className="button is-link" | ||
disabled={!readyToSubmit} | ||
> | ||
Add | ||
</button> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import classNames from 'classnames'; | ||
import React, { useState } from 'react'; | ||
// import { Movie } from '../../types/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. Remove comment |
||
|
||
type Props = { | ||
name: string, | ||
|
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.
For what this spread operator, react created new array each render, and first value it's just default value which don't change moviesFromServer variable