-
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 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,37 +1,112 @@ | ||
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 ? true : false; | ||
|
||
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={() => {}} | ||
value={title} | ||
required | ||
onChange={setTitle} | ||
/> | ||
Comment on lines
+67
to
+69
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 |
||
|
||
<TextField name="description" label="Description" value="" /> | ||
<TextField | ||
name="description" | ||
label="Description" | ||
value={description} | ||
onChange={setDescription} | ||
/> | ||
|
||
<TextField name="imgUrl" label="Image URL" value="" /> | ||
<TextField | ||
name="imgUrl" | ||
label="Image URL" | ||
value={imgUrl} | ||
required | ||
onChange={setImgUrl} | ||
/> | ||
Comment on lines
+82
to
+84
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 the |
||
|
||
<TextField name="imdbUrl" label="Imdb URL" value="" /> | ||
<TextField | ||
name="imdbUrl" | ||
label="Imdb URL" | ||
value={imdbUrl} | ||
required | ||
onChange={setImdbUrl} | ||
/> | ||
Comment on lines
+90
to
+92
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 the |
||
|
||
<TextField name="imdbId" label="Imdb ID" value="" /> | ||
<TextField | ||
name="imdbId" | ||
label="Imdb ID" | ||
value={imdbId} | ||
required | ||
onChange={setImdbId} | ||
/> | ||
Comment on lines
+98
to
+100
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 the |
||
|
||
<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,7 @@ type Props = { | |
label?: string; | ||
placeholder?: string; | ||
required?: boolean; | ||
onChange?: (newValue: string) => void; | ||
onChange: (newValue: string) => void; | ||
}; | ||
|
||
function getRandomDigits() { | ||
|
@@ -16,11 +16,11 @@ function getRandomDigits() { | |
|
||
export const TextField: React.FC<Props> = ({ | ||
name, | ||
value, | ||
label = name, | ||
value, | ||
placeholder = `Enter ${label}`, | ||
required = false, | ||
onChange = () => {}, | ||
onChange, | ||
}) => { | ||
// generate a unique id once on component load | ||
const [id] = useState(() => `${name}-${getRandomDigits()}`); | ||
|
@@ -29,6 +29,10 @@ export const TextField: React.FC<Props> = ({ | |
const [touched, setTouched] = useState(false); | ||
const hasError = touched && required && !value; | ||
|
||
const handleOnChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
onChange(event.target.value); | ||
}; | ||
|
||
return ( | ||
<div className="field"> | ||
<label className="label" htmlFor={id}> | ||
|
@@ -37,6 +41,7 @@ export const TextField: React.FC<Props> = ({ | |
|
||
<div className="control"> | ||
<input | ||
name={name} | ||
type="text" | ||
id={id} | ||
data-cy={`movie-${name}`} | ||
|
@@ -45,8 +50,9 @@ export const TextField: React.FC<Props> = ({ | |
})} | ||
placeholder={placeholder} | ||
value={value} | ||
onChange={event => onChange(event.target.value)} | ||
onChange={event => handleOnChange(event)} | ||
onBlur={() => setTouched(true)} | ||
required={name !== 'description' && !value} | ||
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 |
||
/> | ||
</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 correctly structured as an array ofMovie
objects. The type assertionas Movie[]
assumes that the data matches theMovie
type, which can lead to runtime errors if the structure is incorrect. Verify the structure ofmoviesFromServer
to prevent potential issues.