-
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
init commit #2740
base: master
Are you sure you want to change the base?
init commit #2740
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,34 +1,98 @@ | ||
import { useState } from 'react'; | ||
import { FormEventHandler, useState } from 'react'; | ||
import { TextField } from '../TextField'; | ||
import { Movie } from '../../types/Movie'; | ||
|
||
export const NewMovie = () => { | ||
type Props = { | ||
onAdd: (movie: Movie) => void; | ||
}; | ||
|
||
export const NewMovie = ({ onAdd }: Props) => { | ||
// 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 [titleInput, setTitleInput] = useState(''); | ||
const [descriptionInput, setDescriptionInput] = useState(''); | ||
const [imgUrlInput, setImgUrlInput] = useState(''); | ||
const [imdbUrlInput, setImdbUrlInput] = useState(''); | ||
const [imdbIdInput, setimdbIdInput] = useState(''); | ||
const increaseCount = () => { | ||
setCount(count + 1); | ||
}; | ||
|
||
const successfulSubmission = | ||
titleInput !== '' && | ||
imgUrlInput !== '' && | ||
imdbUrlInput !== '' && | ||
imdbIdInput !== ''; | ||
|
||
const handleSubmit: FormEventHandler = event => { | ||
event.preventDefault(); | ||
increaseCount(); | ||
const movie: Movie = { | ||
title: titleInput.trimEnd(), | ||
description: descriptionInput.trimEnd(), | ||
imgUrl: imgUrlInput.trimEnd(), | ||
imdbUrl: imdbUrlInput.trimEnd(), | ||
imdbId: imdbIdInput.trimEnd(), | ||
}; | ||
|
||
if (successfulSubmission) { | ||
onAdd(movie); | ||
setTitleInput(''); | ||
setDescriptionInput(''); | ||
setImgUrlInput(''); | ||
setImdbUrlInput(''); | ||
setimdbIdInput(''); | ||
} | ||
}; | ||
|
||
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={titleInput} | ||
onChange={setTitleInput} | ||
required | ||
/> | ||
|
||
<TextField name="description" label="Description" value="" /> | ||
<TextField | ||
onChange={setDescriptionInput} | ||
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. Similar to the previous comment, ensure that |
||
name="description" | ||
label="Description" | ||
value={descriptionInput} | ||
/> | ||
|
||
<TextField name="imgUrl" label="Image URL" value="" /> | ||
<TextField | ||
onChange={setImgUrlInput} | ||
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. Ensure that |
||
name="imgUrl" | ||
label="Image URL" | ||
value={imgUrlInput} | ||
required | ||
/> | ||
|
||
<TextField name="imdbUrl" label="Imdb URL" value="" /> | ||
<TextField | ||
onChange={setImdbUrlInput} | ||
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. Ensure that |
||
name="imdbUrl" | ||
label="Imdb URL" | ||
value={imdbUrlInput} | ||
required | ||
/> | ||
|
||
<TextField name="imdbId" label="Imdb ID" value="" /> | ||
<TextField | ||
onChange={setimdbIdInput} | ||
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. Ensure that |
||
name="imdbId" | ||
label="Imdb ID" | ||
value={imdbIdInput} | ||
required | ||
/> | ||
|
||
<div className="field is-grouped"> | ||
<div className="control"> | ||
<button | ||
disabled={!successfulSubmission} | ||
type="submit" | ||
data-cy="submit-button" | ||
className="button is-link" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ type Props = { | |
label?: string; | ||
placeholder?: string; | ||
required?: boolean; | ||
onChange?: (newValue: string) => void; | ||
onChange: (input: string) => void; | ||
}; | ||
|
||
function getRandomDigits() { | ||
|
@@ -20,7 +20,7 @@ export const TextField: React.FC<Props> = ({ | |
label = name, | ||
placeholder = `Enter ${label}`, | ||
required = false, | ||
onChange = () => {}, | ||
onChange, | ||
}) => { | ||
// generate a unique id once on component load | ||
const [id] = useState(() => `${name}-${getRandomDigits()}`); | ||
|
@@ -45,7 +45,7 @@ export const TextField: React.FC<Props> = ({ | |
})} | ||
placeholder={placeholder} | ||
value={value} | ||
onChange={event => onChange(event.target.value)} | ||
onChange={event => onChange(event.target.value.trimStart())} | ||
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. Use trim method, to trim from both sides |
||
onBlur={() => setTouched(true)} | ||
/> | ||
</div> | ||
|
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.
The
onChange
prop should be a function that takes an event and updates the state. Make sure thatsetTitleInput
and other similar functions are correctly handling the event parameter. Typically, it should be something likeonChange={(e) => setTitleInput(e.target.value)}
.