-
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
solved #2738
base: master
Are you sure you want to change the base?
solved #2738
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,87 @@ | ||
import { useState } from 'react'; | ||
import React, { useState } from 'react'; | ||
import { TextField } from '../TextField'; | ||
import { Movie } from '../../App'; | ||
|
||
export const NewMovie = () => { | ||
// Increase the count after successful form submission | ||
// to reset touched status of all the `Field`s | ||
const [count] = useState(0); | ||
type Props = { | ||
onAdd: (movie: Movie) => void; | ||
}; | ||
|
||
export const NewMovie: React.FC<Props> = ({ onAdd }) => { | ||
const [count, setCount] = useState(0); | ||
const [title, setTitle] = useState(''); | ||
const [descr, setDescr] = useState(''); | ||
const [img, setImg] = useState(''); | ||
const [url, setUrl] = useState(''); | ||
const [id, setId] = useState(''); | ||
const isValid = title.trim() && img.trim() && url.trim() && id.trim(); | ||
|
||
const handleSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
|
||
if (!isValid) { | ||
return; | ||
} | ||
|
||
setCount(count + 1); | ||
onAdd({ | ||
title, | ||
description: descr, | ||
imgUrl: img, | ||
imdbUrl: url, | ||
imdbId: id, | ||
}); | ||
}; | ||
|
||
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} | ||
onChange={setTitle} | ||
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 |
||
/> | ||
|
||
<TextField name="description" label="Description" value="" /> | ||
<TextField | ||
name="description" | ||
label="Description" | ||
value={descr} | ||
onChange={setDescr} | ||
/> | ||
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, the |
||
|
||
<TextField name="imgUrl" label="Image URL" value="" /> | ||
<TextField | ||
name="imgUrl" | ||
label="Image URL" | ||
value={img} | ||
onChange={setImg} | ||
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 |
||
/> | ||
|
||
<TextField name="imdbUrl" label="Imdb URL" value="" /> | ||
<TextField | ||
name="imdbUrl" | ||
label="Imdb URL" | ||
value={url} | ||
onChange={setUrl} | ||
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 |
||
/> | ||
|
||
<TextField name="imdbId" label="Imdb ID" value="" /> | ||
<TextField | ||
name="imdbId" | ||
label="Imdb ID" | ||
value={id} | ||
onChange={setId} | ||
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 |
||
/> | ||
|
||
<div className="field is-grouped"> | ||
<div className="control"> | ||
<button | ||
type="submit" | ||
data-cy="submit-button" | ||
className="button is-link" | ||
disabled={!isValid} | ||
> | ||
Add | ||
</button> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,9 @@ | |
return Math.random().toFixed(16).slice(2); | ||
} | ||
|
||
const pattern = | ||
/^((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www\.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@,.\w_]*)#?(?:[,.!/\\\w]*))?)$/; | ||
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 URL validation pattern seems overly complex and might not cover all valid URL cases. Consider simplifying the regex or using a library for URL validation to ensure comprehensive coverage. |
||
|
||
export const TextField: React.FC<Props> = ({ | ||
name, | ||
value, | ||
|
@@ -28,6 +31,7 @@ | |
// To show errors only if the field was touched (onBlur) | ||
const [touched, setTouched] = useState(false); | ||
const hasError = touched && required && !value; | ||
const [validMessage, setValidMessage] = useState(''); | ||
|
||
return ( | ||
<div className="field"> | ||
|
@@ -46,11 +50,23 @@ | |
placeholder={placeholder} | ||
value={value} | ||
onChange={event => onChange(event.target.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 |
||
onBlur={() => setTouched(true)} | ||
onBlur={() => { | ||
setTouched(true); | ||
if ( | ||
(name === 'imgUrl' || name === 'imdbUrl') && | ||
!pattern.test(value) | ||
) { | ||
console.log(pattern.test(value)); | ||
setValidMessage('Not valid URL'); | ||
} else { | ||
setValidMessage(''); | ||
} | ||
}} | ||
/> | ||
</div> | ||
|
||
{hasError && <p className="help is-danger">{`${label} is required`}</p>} | ||
{validMessage && <p className="help is-danger">{`Not valid URL`}</p>} | ||
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 validation message 'Not valid URL' is hardcoded. Consider using 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.
Consider renaming
setMovie
tosetMovies
to match the plural form of themovies
state variable. This will improve code readability and maintain consistency in naming conventions.