-
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
aasdasd #2682
base: master
Are you sure you want to change the base?
aasdasd #2682
Changes from 1 commit
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,37 +1,117 @@ | ||
import { useState } from 'react'; | ||
import { FormEvent, useState } from 'react'; | ||
import { TextField } from '../TextField'; | ||
|
||
export const NewMovie = () => { | ||
type Movie = { | ||
title: string; | ||
description: string; | ||
imgUrl: string; | ||
imdbUrl: string; | ||
imdbId: string; | ||
}; | ||
|
||
type NewMovieProps = { | ||
onAdd: (newMovie: 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 [title, setTitle] = useState<string>(''); | ||
const [description, setDescription] = useState<string>(''); | ||
const [imgUrl, setImgUrl] = useState<string>(''); | ||
const [imdbUrl, setImdbUrl] = useState<string>(''); | ||
const [imdbId, setImdbId] = useState<string>(''); | ||
const buttonEnabled = title || imgUrl || imdbUrl || 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. The |
||
const handleSubmit = (event: FormEvent<HTMLFormElement>): void => { | ||
event.preventDefault(); | ||
|
||
const form = event.target as HTMLFormElement; | ||
const formData = new FormData(form); | ||
|
||
const data = Object.fromEntries(formData.entries()) as { | ||
[key: string]: string; | ||
}; | ||
|
||
const newMovie: Movie = { | ||
title: data.title, | ||
description: data.description, | ||
imgUrl: data.imgUrl, | ||
imdbUrl: data.imdbUrl, | ||
imdbId: data.imdbId, | ||
}; | ||
|
||
onAdd(newMovie); | ||
|
||
form.reset(); | ||
|
||
setTitle(''); | ||
setDescription(''); | ||
setImgUrl(''); | ||
setImdbUrl(''); | ||
setImdbId(''); | ||
}; | ||
|
||
return ( | ||
<form className="NewMovie" key={count}> | ||
<form | ||
className="NewMovie" | ||
key={count} | ||
onSubmit={event => handleSubmit(event)} | ||
> | ||
<h2 className="title">Add a movie</h2> | ||
|
||
<TextField | ||
name="title" | ||
label="Title" | ||
value="" | ||
onChange={() => {}} | ||
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. The |
||
required | ||
data={title} | ||
onChange={setTitle} | ||
/> | ||
|
||
<TextField name="description" label="Description" value="" /> | ||
<TextField | ||
name="description" | ||
label="Description" | ||
value="" | ||
data={description} | ||
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. The |
||
onChange={setDescription} | ||
/> | ||
|
||
<TextField name="imgUrl" label="Image URL" value="" /> | ||
<TextField | ||
name="imgUrl" | ||
label="Image URL" | ||
value="" | ||
required | ||
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. The |
||
data={imgUrl} | ||
onChange={setImgUrl} | ||
/> | ||
|
||
<TextField name="imdbUrl" label="Imdb URL" value="" /> | ||
<TextField | ||
name="imdbUrl" | ||
label="Imdb URL" | ||
value="" | ||
required | ||
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. The |
||
data={imdbUrl} | ||
onChange={setImdbUrl} | ||
/> | ||
|
||
<TextField name="imdbId" label="Imdb ID" value="" /> | ||
<TextField | ||
name="imdbId" | ||
label="Imdb ID" | ||
value="" | ||
required | ||
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. The |
||
data={imdbId} | ||
onChange={setImdbId} | ||
/> | ||
|
||
<div className="field is-grouped"> | ||
<div className="control"> | ||
<button | ||
type="submit" | ||
data-cy="submit-button" | ||
className="button is-link" | ||
disabled={!buttonEnabled} | ||
> | ||
Add | ||
</button> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ type Props = { | |
label?: string; | ||
placeholder?: string; | ||
required?: boolean; | ||
onChange?: (newValue: string) => void; | ||
data: string; | ||
onChange: (newValue: string) => void; | ||
}; | ||
|
||
function getRandomDigits() { | ||
|
@@ -16,18 +17,22 @@ function getRandomDigits() { | |
|
||
export const TextField: React.FC<Props> = ({ | ||
name, | ||
value, | ||
label = name, | ||
placeholder = `Enter ${label}`, | ||
required = false, | ||
onChange = () => {}, | ||
data, | ||
onChange, | ||
}) => { | ||
// generate a unique id once on component load | ||
const [id] = useState(() => `${name}-${getRandomDigits()}`); | ||
|
||
// To show errors only if the field was touched (onBlur) | ||
const [touched, setTouched] = useState(false); | ||
const hasError = touched && required && !value; | ||
const hasError = touched && required && !data; | ||
|
||
const handleOnChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
onChange(event.target.value); | ||
}; | ||
|
||
return ( | ||
<div className="field"> | ||
|
@@ -37,16 +42,18 @@ export const TextField: React.FC<Props> = ({ | |
|
||
<div className="control"> | ||
<input | ||
name={name} | ||
type="text" | ||
id={id} | ||
data-cy={`movie-${name}`} | ||
className={classNames('input', { | ||
'is-danger': hasError, | ||
})} | ||
placeholder={placeholder} | ||
value={value} | ||
onChange={event => onChange(event.target.value)} | ||
value={data} | ||
onChange={event => handleOnChange(event)} | ||
onBlur={() => setTouched(true)} | ||
required={name === 'description' ? false : data ? false : true} | ||
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. The logic for the |
||
/> | ||
</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.
Ensure that
moviesFromServer
is an array of objects that conform to theMovie
type. If the structure ofmoviesFromServer
does not match theMovie
type, it could lead to runtime errors.