-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd30f85
commit 2d206f7
Showing
2 changed files
with
27 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,45 +13,48 @@ export const NewCommentForm: React.FC<Props> = ({ | |
}) => { | ||
const [name, setName] = useState(''); | ||
const [nameError, setNameError] = useState(false); | ||
const [email, setEmail] = useState(''); | ||
const [emailError, setEmailError] = useState(false); | ||
const [text, setText] = useState(''); | ||
const [textError, setTextError] = useState(false); | ||
|
||
const handleNameChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setName(event.target.value); | ||
setNameError(false); | ||
}; | ||
|
||
const [email, setEmail] = useState(''); | ||
const [emailError, setEmailError] = useState(false); | ||
|
||
const handleEmailChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setEmail(event.target.value); | ||
setEmailError(false); | ||
}; | ||
|
||
const [text, setText] = useState(''); | ||
const [textError, setTextError] = useState(false); | ||
|
||
const handleTextChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => { | ||
setText(event.target.value); | ||
setTextError(false); | ||
}; | ||
|
||
const normalizeValue = (value: string) => { | ||
return value.trim(); | ||
const checkQuery = ( | ||
query: string, | ||
setQuery: (query: string) => void, | ||
error: (value: boolean) => void, | ||
) => { | ||
const normalizedQuery = query.trim(); | ||
const isError = normalizedQuery.length === 0 || !query; | ||
|
||
error(isError); | ||
setQuery(normalizedQuery); | ||
|
||
return isError; | ||
}; | ||
|
||
const onSubmit = (event: React.FormEvent | React.MouseEvent) => { | ||
event.preventDefault(); | ||
|
||
setName(normalizeValue(name)); | ||
setNameError(!name); | ||
const isNameError = checkQuery(name, setName, setNameError); | ||
const isEmailError = checkQuery(email, setEmail, setEmailError); | ||
const isTextError = checkQuery(text, setText, setTextError); | ||
|
||
setEmail(normalizeValue(email)); | ||
setEmailError(!email); | ||
|
||
setText(normalizeValue(text)); | ||
setTextError(!text); | ||
|
||
if (!name || !email || !text) { | ||
if (isNameError || isEmailError || isTextError) { | ||
return; | ||
} | ||
|
||
|
@@ -116,7 +119,7 @@ export const NewCommentForm: React.FC<Props> = ({ | |
|
||
<div className="control has-icons-left has-icons-right"> | ||
<input | ||
type="text" | ||
type="email" | ||
name="email" | ||
id="comment-author-email" | ||
placeholder="[email protected]" | ||
|