-
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 #2111
base: master
Are you sure you want to change the base?
add task solution #2111
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,45 +1,101 @@ | ||
import { useState } from 'react'; | ||
import { TextField } from '../TextField'; | ||
|
||
export const NewMovie = () => { | ||
interface NewMovieProps { | ||
onAdd: ( | ||
movie: { | ||
title: string, | ||
description: string, | ||
imgUrl: string, | ||
imdbUrl: string, | ||
imdbId: string, | ||
} | ||
) => void | ||
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. If you don't understand what Oleksandr meant, then look at this
you already have Movie type dont write
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. i fixed only in App. here I scroll and missed it)) |
||
} | ||
|
||
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 [count, setCount] = useState(0); | ||
const [title, setTitle] = useState(''); | ||
const [description, setDescription] = useState(''); | ||
const [imgUrl, setImgUrl] = useState(''); | ||
const [imdbUrl, setImdbUrl] = useState(''); | ||
const [imdbId, setImdbId] = useState(''); | ||
|
||
const urlValidator = (urlString: string): boolean => { | ||
// eslint-disable-next-line max-len | ||
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. put this function inside |
||
const pattern = /^((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www\.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@,.\w_]*)#?(?:[,.!/\\\w]*))?)$/; | ||
|
||
if (!urlString.match(pattern)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
function addMovie(event: React.FormEvent) { | ||
event.preventDefault(); | ||
onAdd({ | ||
title: title.trim(), | ||
description: description.trim(), | ||
imgUrl: imgUrl.trim(), | ||
imdbUrl: imdbUrl.trim(), | ||
imdbId: imdbId.trim(), | ||
}); | ||
|
||
setTitle(''); | ||
setDescription(''); | ||
setImgUrl(''); | ||
setImdbUrl(''); | ||
setImdbId(''); | ||
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. you can create a |
||
|
||
setCount(prev => prev + 1); | ||
} | ||
|
||
return ( | ||
<form className="NewMovie" key={count}> | ||
<form className="NewMovie" key={count} onSubmit={addMovie}> | ||
<h2 className="title">Add a movie</h2> | ||
|
||
<TextField | ||
name="title" | ||
label="Title" | ||
value="" | ||
onChange={() => {}} | ||
value={title} | ||
onChange={setTitle} | ||
required | ||
/> | ||
|
||
<TextField | ||
name="description" | ||
label="Description" | ||
value="" | ||
value={description} | ||
onChange={setDescription} | ||
/> | ||
|
||
<TextField | ||
name="imgUrl" | ||
label="Image URL" | ||
value="" | ||
value={imgUrl} | ||
onChange={setImgUrl} | ||
checkUrl={urlValidator} | ||
required | ||
/> | ||
|
||
<TextField | ||
name="imdbUrl" | ||
label="Imdb URL" | ||
value="" | ||
value={imdbUrl} | ||
onChange={setImdbUrl} | ||
checkUrl={urlValidator} | ||
required | ||
/> | ||
|
||
<TextField | ||
name="imdbId" | ||
label="Imdb ID" | ||
value="" | ||
value={imdbId} | ||
onChange={setImdbId} | ||
required | ||
/> | ||
|
||
<div className="field is-grouped"> | ||
|
@@ -48,6 +104,10 @@ export const NewMovie = () => { | |
type="submit" | ||
data-cy="submit-button" | ||
className="button is-link" | ||
disabled={!title | ||
|| !urlValidator(imgUrl) | ||
|| !urlValidator(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. maybe put this check in const |
||
> | ||
Add | ||
</button> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ type Props = { | |
placeholder?: string, | ||
required?: boolean, | ||
onChange?: (newValue: string) => void, | ||
checkUrl?: (urlString: string) => boolean, | ||
}; | ||
|
||
function getRandomDigits() { | ||
|
@@ -23,6 +24,7 @@ export const TextField: React.FC<Props> = ({ | |
placeholder = `Enter ${label}`, | ||
required = false, | ||
onChange = () => {}, | ||
checkUrl = undefined, | ||
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. please don't write you have already done this by adding |
||
}) => { | ||
// generage a unique id once on component load | ||
const [id] = useState(() => `${name}-${getRandomDigits()}`); | ||
|
@@ -55,6 +57,8 @@ export const TextField: React.FC<Props> = ({ | |
{hasError && ( | ||
<p className="help is-danger">{`${label} is required`}</p> | ||
)} | ||
|
||
{((touched && value) && (checkUrl && !checkUrl(value))) && (<p className="help is-danger">{`${label} incorrect 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. You can write this condition in constant ( too long ) |
||
</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.
You create type State and can use it as type for movie. Actually we already have the same type in 'types' folder, so you may use it instead of creating you own.
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.
agree)