-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
solution #1187
Open
Shevchuchka
wants to merge
7
commits into
mate-academy:master
Choose a base branch
from
Shevchuchka:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
solution #1187
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4aa1f67
solution
Shevchuchka d057951
improve code
Shevchuchka 24a516a
remove default values
Shevchuchka 39c6234
solution
Shevchuchka fd30f85
menuClick error
Shevchuchka 2d206f7
improvedCode
Shevchuchka d54749c
postDetails
Shevchuchka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,8 +1,78 @@ | ||
import React from 'react'; | ||
import classNames from 'classnames'; | ||
import React, { useState } from 'react'; | ||
import { CommentData } from '../types/Comment'; | ||
|
||
type Props = { | ||
buttonLoader: boolean; | ||
addFunction: (newComment: CommentData) => Promise<void>; | ||
}; | ||
|
||
export const NewCommentForm: React.FC<Props> = ({ | ||
buttonLoader, | ||
addFunction, | ||
}) => { | ||
const [name, setName] = useState(''); | ||
const [nameError, setNameError] = 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 onSubmit = (event: React.FormEvent | React.MouseEvent) => { | ||
event.preventDefault(); | ||
|
||
setName(normalizeValue(name)); | ||
setNameError(!name); | ||
|
||
setEmail(normalizeValue(email)); | ||
setEmailError(!email); | ||
|
||
setText(normalizeValue(text)); | ||
setTextError(!text); | ||
|
||
if (!name || !email || !text) { | ||
return; | ||
} | ||
|
||
const newComment = addFunction({ name, email, body: text }); | ||
|
||
if (newComment instanceof Promise) { | ||
newComment.then(() => setText('')); | ||
} | ||
}; | ||
|
||
const clearFunction = () => { | ||
setEmail(''); | ||
setEmailError(false); | ||
setName(''); | ||
setNameError(false); | ||
setText(''); | ||
setTextError(false); | ||
}; | ||
|
||
export const NewCommentForm: React.FC = () => { | ||
return ( | ||
<form data-cy="NewCommentForm"> | ||
<form data-cy="NewCommentForm" onSubmit={event => onSubmit(event)}> | ||
<div className="field" data-cy="NameField"> | ||
<label className="label" htmlFor="comment-author-name"> | ||
Author Name | ||
|
@@ -14,24 +84,29 @@ export const NewCommentForm: React.FC = () => { | |
name="name" | ||
id="comment-author-name" | ||
placeholder="Name Surname" | ||
className="input is-danger" | ||
value={name} | ||
onChange={handleNameChange} | ||
className={classNames('input', { 'is-danger': nameError })} | ||
/> | ||
|
||
<span className="icon is-small is-left"> | ||
<i className="fas fa-user" /> | ||
</span> | ||
{nameError && ( | ||
<> | ||
<span | ||
className="icon is-small is-right has-text-danger" | ||
data-cy="ErrorIcon" | ||
> | ||
<i className="fas fa-exclamation-triangle" /> | ||
</span> | ||
|
||
<span | ||
className="icon is-small is-right has-text-danger" | ||
data-cy="ErrorIcon" | ||
> | ||
<i className="fas fa-exclamation-triangle" /> | ||
</span> | ||
<p className="help is-danger" data-cy="ErrorMessage"> | ||
Name is required | ||
</p> | ||
</> | ||
)} | ||
</div> | ||
|
||
<p className="help is-danger" data-cy="ErrorMessage"> | ||
Name is required | ||
</p> | ||
</div> | ||
|
||
<div className="field" data-cy="EmailField"> | ||
|
@@ -45,24 +120,30 @@ export const NewCommentForm: React.FC = () => { | |
name="email" | ||
id="comment-author-email" | ||
placeholder="[email protected]" | ||
className="input is-danger" | ||
onChange={handleEmailChange} | ||
value={email} | ||
className={classNames('input', { 'is-danger': emailError })} | ||
/> | ||
|
||
<span className="icon is-small is-left"> | ||
<i className="fas fa-envelope" /> | ||
</span> | ||
|
||
<span | ||
className="icon is-small is-right has-text-danger" | ||
data-cy="ErrorIcon" | ||
> | ||
<i className="fas fa-exclamation-triangle" /> | ||
</span> | ||
</div> | ||
{emailError && ( | ||
<> | ||
<span | ||
className="icon is-small is-right has-text-danger" | ||
data-cy="ErrorIcon" | ||
> | ||
<i className="fas fa-exclamation-triangle" /> | ||
</span> | ||
|
||
<p className="help is-danger" data-cy="ErrorMessage"> | ||
Email is required | ||
</p> | ||
<p className="help is-danger" data-cy="ErrorMessage"> | ||
Email is required | ||
</p> | ||
</> | ||
)} | ||
</div> | ||
</div> | ||
|
||
<div className="field" data-cy="BodyField"> | ||
|
@@ -75,25 +156,40 @@ export const NewCommentForm: React.FC = () => { | |
id="comment-body" | ||
name="body" | ||
placeholder="Type comment here" | ||
className="textarea is-danger" | ||
onChange={handleTextChange} | ||
value={text} | ||
className={classNames('input', { 'is-danger': textError })} | ||
/> | ||
</div> | ||
|
||
<p className="help is-danger" data-cy="ErrorMessage"> | ||
Enter some text | ||
</p> | ||
{textError && ( | ||
<p className="help is-danger" data-cy="ErrorMessage"> | ||
Enter some text | ||
</p> | ||
)} | ||
</div> | ||
|
||
<div className="field is-grouped"> | ||
<div className="control"> | ||
<button type="submit" className="button is-link is-loading"> | ||
<button | ||
type="submit" | ||
onClick={event => { | ||
onSubmit(event); | ||
}} | ||
className={classNames('button is-link', { | ||
'is-loading': buttonLoader, | ||
})} | ||
> | ||
Add | ||
</button> | ||
</div> | ||
|
||
<div className="control"> | ||
{/* eslint-disable-next-line react/button-has-type */} | ||
<button type="reset" className="button is-link is-light"> | ||
<button | ||
type="reset" | ||
onClick={clearFunction} | ||
className="button is-link is-light" | ||
> | ||
Clear | ||
</button> | ||
</div> | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
All states must be at the beginning of the component