-
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 solution #2401
base: master
Are you sure you want to change the base?
add task solution #2401
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 { Movie } from '../../types/Movie'; | ||||||||||||||||||
import { TextField } from '../TextField'; | ||||||||||||||||||
|
||||||||||||||||||
export const NewMovie = () => { | ||||||||||||||||||
// Increase the count after successful form submission | ||||||||||||||||||
// to reset touched status of all the `Field`s | ||||||||||||||||||
const [count] = useState(0); | ||||||||||||||||||
interface MovieForm { | ||||||||||||||||||
title: string; | ||||||||||||||||||
imgUrl: string; | ||||||||||||||||||
imdbUrl: string; | ||||||||||||||||||
imdbId: string; | ||||||||||||||||||
description: string; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
export const NewMovie = ({ | ||||||||||||||||||
onAddMovie, | ||||||||||||||||||
}: { | ||||||||||||||||||
onAddMovie: (movie: Movie) => void; | ||||||||||||||||||
}) => { | ||||||||||||||||||
const [form, setForm] = useState<Movie>({ | ||||||||||||||||||
title: '', | ||||||||||||||||||
imgUrl: '', | ||||||||||||||||||
imdbUrl: '', | ||||||||||||||||||
imdbId: '', | ||||||||||||||||||
description: '', | ||||||||||||||||||
}); | ||||||||||||||||||
|
||||||||||||||||||
const [touched, setTouched] = useState<{ [key: string]: boolean }>({}); | ||||||||||||||||||
|
||||||||||||||||||
const handleBlur = (name: string) => { | ||||||||||||||||||
setTouched(prev => ({ ...prev, [name]: true })); | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
const handleChange = (name: string, value: string) => { | ||||||||||||||||||
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.
Suggested change
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. IT DOESN'T WORK THAT WAY( 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. it should work, you can always ask for some help in the chat but you also can leave your version, just specify types more precisely
Suggested change
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. |
||||||||||||||||||
setForm(prevForm => ({ | ||||||||||||||||||
...prevForm, | ||||||||||||||||||
[name]: 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.
Suggested change
|
||||||||||||||||||
})); | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
const isFormValid = () => { | ||||||||||||||||||
const requiredFields = ['title', 'imgUrl', 'imdbUrl', 'imdbId']; | ||||||||||||||||||
return requiredFields.every(field => !!form[field as keyof MovieForm]); | ||||||||||||||||||
}; | ||||||||||||||||||
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.
Suggested change
to fix my prev comment |
||||||||||||||||||
|
||||||||||||||||||
const handleSubmit = (e: React.FormEvent) => { | ||||||||||||||||||
e.preventDefault(); | ||||||||||||||||||
onAddMovie(form); | ||||||||||||||||||
setForm({ | ||||||||||||||||||
title: '', | ||||||||||||||||||
imgUrl: '', | ||||||||||||||||||
imdbUrl: '', | ||||||||||||||||||
imdbId: '', | ||||||||||||||||||
description: '', | ||||||||||||||||||
}); | ||||||||||||||||||
setTouched({}); | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
return ( | ||||||||||||||||||
<form className="NewMovie" key={count}> | ||||||||||||||||||
<form className="NewMovie" onSubmit={handleSubmit}> | ||||||||||||||||||
<h2 className="title">Add a movie</h2> | ||||||||||||||||||
|
||||||||||||||||||
<TextField | ||||||||||||||||||
name="title" | ||||||||||||||||||
label="Title" | ||||||||||||||||||
value="" | ||||||||||||||||||
onChange={() => {}} | ||||||||||||||||||
value={form.title} | ||||||||||||||||||
onChange={value => handleChange('title', 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.
Suggested change
You can create one event handler for all fields |
||||||||||||||||||
onBlur={() => handleBlur('title')} | ||||||||||||||||||
required | ||||||||||||||||||
touched={!!touched.title} | ||||||||||||||||||
/> | ||||||||||||||||||
|
||||||||||||||||||
<TextField name="description" label="Description" value="" /> | ||||||||||||||||||
<TextField | ||||||||||||||||||
name="description" | ||||||||||||||||||
label="Description" | ||||||||||||||||||
value={form.description} | ||||||||||||||||||
onChange={value => handleChange('description', value)} | ||||||||||||||||||
/> | ||||||||||||||||||
|
||||||||||||||||||
<TextField name="imgUrl" label="Image URL" value="" /> | ||||||||||||||||||
<TextField | ||||||||||||||||||
name="imgUrl" | ||||||||||||||||||
label="Image URL" | ||||||||||||||||||
value={form.imgUrl} | ||||||||||||||||||
onChange={value => handleChange('imgUrl', value)} | ||||||||||||||||||
onBlur={() => handleBlur('imgUrl')} | ||||||||||||||||||
required | ||||||||||||||||||
touched={!!touched.imgUrl} | ||||||||||||||||||
/> | ||||||||||||||||||
|
||||||||||||||||||
<TextField name="imdbUrl" label="Imdb URL" value="" /> | ||||||||||||||||||
<TextField | ||||||||||||||||||
name="imdbUrl" | ||||||||||||||||||
label="Imdb URL" | ||||||||||||||||||
value={form.imdbUrl} | ||||||||||||||||||
onChange={value => handleChange('imdbUrl', value)} | ||||||||||||||||||
onBlur={() => handleBlur('imdbUrl')} | ||||||||||||||||||
required | ||||||||||||||||||
touched={!!touched.imdbUrl} | ||||||||||||||||||
/> | ||||||||||||||||||
|
||||||||||||||||||
<TextField name="imdbId" label="Imdb ID" value="" /> | ||||||||||||||||||
<TextField | ||||||||||||||||||
name="imdbId" | ||||||||||||||||||
label="Imdb ID" | ||||||||||||||||||
value={form.imdbId} | ||||||||||||||||||
onChange={value => handleChange('imdbId', value)} | ||||||||||||||||||
onBlur={() => handleBlur('imdbId')} | ||||||||||||||||||
required | ||||||||||||||||||
touched={!!touched.imdbId} | ||||||||||||||||||
/> | ||||||||||||||||||
|
||||||||||||||||||
<div className="field is-grouped"> | ||||||||||||||||||
<div className="control"> | ||||||||||||||||||
<button | ||||||||||||||||||
type="submit" | ||||||||||||||||||
data-cy="submit-button" | ||||||||||||||||||
className="button is-link" | ||||||||||||||||||
disabled={!isFormValid()} | ||||||||||||||||||
> | ||||||||||||||||||
Add | ||||||||||||||||||
</button> | ||||||||||||||||||
|
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.