-
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
Showing
6 changed files
with
418 additions
and
258 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 |
---|---|---|
@@ -1,8 +1,97 @@ | ||
import React from 'react'; | ||
import classNames from 'classnames'; | ||
import React, { useState } from 'react'; | ||
import { Comment } from '../types/Comment'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
interface Props { | ||
postId: number; | ||
onAddComment: (newComment: Comment) => void; | ||
} | ||
|
||
export const NewCommentForm: React.FC<Props> = ({ postId, onAddComment }) => { | ||
const [name, setName] = useState(''); | ||
const [email, setEmail] = useState(''); | ||
const [comment, setComment] = useState(''); | ||
const [nameError, setNameError] = useState(false); | ||
const [emailError, setEmailError] = useState(false); | ||
const [commentError, setCommentError] = useState(false); | ||
const [loading, setLoading] = useState(false); | ||
|
||
function handleNameChange(event: React.ChangeEvent<HTMLInputElement>) { | ||
setName(event.target.value); | ||
setNameError(false); | ||
} | ||
|
||
function handleEmailChange(event: React.ChangeEvent<HTMLInputElement>) { | ||
setEmail(event.target.value); | ||
setEmailError(false); | ||
} | ||
|
||
function handleCommentChange(event: React.ChangeEvent<HTMLTextAreaElement>) { | ||
setComment(event.target.value); | ||
setCommentError(false); | ||
} | ||
|
||
const validate = (names: string, emaill: string, coment: string) => { | ||
let isValid = true; | ||
|
||
if (!names.trim()) { | ||
setNameError(true); | ||
isValid = false; | ||
} | ||
|
||
if (!emaill.trim() || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emaill)) { | ||
setEmailError(true); | ||
isValid = false; | ||
} | ||
|
||
if (!coment.trim()) { | ||
setCommentError(true); | ||
isValid = false; | ||
} | ||
|
||
return isValid; | ||
}; | ||
|
||
const reset = () => { | ||
setName(''); | ||
setEmail(''); | ||
setComment(''); | ||
setNameError(false); | ||
setEmailError(false); | ||
setCommentError(false); | ||
}; | ||
|
||
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
||
if (!validate(name, email, comment)) { | ||
return; // Вихід, якщо валідація не пройдена | ||
} | ||
|
||
const newComment: Comment = { | ||
id: Date.now(), | ||
postId, | ||
name, | ||
email, | ||
body: comment, | ||
}; | ||
|
||
try { | ||
setLoading(true); | ||
|
||
const response = await client.post<Comment>('/comments', newComment); | ||
|
||
onAddComment(response); | ||
reset(); | ||
} catch { | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
export const NewCommentForm: React.FC = () => { | ||
return ( | ||
<form data-cy="NewCommentForm"> | ||
<form data-cy="NewCommentForm" noValidate onSubmit={handleSubmit}> | ||
<div className="field" data-cy="NameField"> | ||
<label className="label" htmlFor="comment-author-name"> | ||
Author Name | ||
|
@@ -14,24 +103,30 @@ export const NewCommentForm: React.FC = () => { | |
name="name" | ||
id="comment-author-name" | ||
placeholder="Name Surname" | ||
className="input is-danger" | ||
className={classNames('input', { 'is-danger': nameError })} | ||
value={name} | ||
onChange={handleNameChange} | ||
/> | ||
|
||
<span className="icon is-small is-left"> | ||
<i className="fas fa-user" /> | ||
</span> | ||
|
||
<span | ||
className="icon is-small is-right has-text-danger" | ||
data-cy="ErrorIcon" | ||
> | ||
<i className="fas fa-exclamation-triangle" /> | ||
</span> | ||
{nameError && ( | ||
<span | ||
className="icon is-small is-right has-text-danger" | ||
data-cy="ErrorIcon" | ||
> | ||
<i className="fas fa-exclamation-triangle" /> | ||
</span> | ||
)} | ||
</div> | ||
|
||
<p className="help is-danger" data-cy="ErrorMessage"> | ||
Name is required | ||
</p> | ||
{nameError && ( | ||
<p className="help is-danger" data-cy="ErrorMessage"> | ||
Name is required | ||
</p> | ||
)} | ||
</div> | ||
|
||
<div className="field" data-cy="EmailField"> | ||
|
@@ -45,24 +140,30 @@ export const NewCommentForm: React.FC = () => { | |
name="email" | ||
id="comment-author-email" | ||
placeholder="[email protected]" | ||
className="input is-danger" | ||
className={classNames('input', { 'is-danger': emailError })} | ||
value={email} | ||
onChange={handleEmailChange} | ||
/> | ||
|
||
<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> | ||
{emailError && ( | ||
<span | ||
className="icon is-small is-right has-text-danger" | ||
data-cy="ErrorIcon" | ||
> | ||
<i className="fas fa-exclamation-triangle" /> | ||
</span> | ||
)} | ||
</div> | ||
|
||
<p className="help is-danger" data-cy="ErrorMessage"> | ||
Email is required | ||
</p> | ||
{emailError && ( | ||
<p className="help is-danger" data-cy="ErrorMessage"> | ||
Email is required | ||
</p> | ||
)} | ||
</div> | ||
|
||
<div className="field" data-cy="BodyField"> | ||
|
@@ -75,25 +176,36 @@ export const NewCommentForm: React.FC = () => { | |
id="comment-body" | ||
name="body" | ||
placeholder="Type comment here" | ||
className="textarea is-danger" | ||
className={classNames('textarea', { 'is-danger': commentError })} | ||
value={comment} | ||
onChange={handleCommentChange} | ||
/> | ||
</div> | ||
|
||
<p className="help is-danger" data-cy="ErrorMessage"> | ||
Enter some text | ||
</p> | ||
{commentError && ( | ||
<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" | ||
className={classNames('button is-link', { 'is-loading': loading })} | ||
> | ||
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" | ||
className="button is-link is-light" | ||
onClick={reset} | ||
> | ||
Clear | ||
</button> | ||
</div> | ||
|
Oops, something went wrong.