Skip to content

Commit

Permalink
added updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorivna007 committed Jul 28, 2023
1 parent 3c52498 commit 0b090d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
28 changes: 14 additions & 14 deletions src/components/NewCommentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import { CommentData } from '../types/Comment';
type Props = {
addNewComment: (newComment: CommentData) => Promise<void>
};
const emptyComment = {
name: '',
email: '',
text: '',
};

export const NewCommentForm: React.FC<Props> = ({ addNewComment }) => {
const [name, setName] = useState<string>('');
const [email, setEmail] = useState<string>('');
const [text, setText] = useState<string>('');

const [isNamePresent, setIsNamePresent] = useState<boolean>(true);
const [isEmailPresent, setIsEmailPresent] = useState<boolean>(true);
const [isTextPresent, setIsTextPresent] = useState<boolean>(true);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [comment, setComment] = useState(emptyComment);
const { name, email, text } = comment;

const handleReset = () => {
setName('');
setEmail('');
setText('');
setComment(emptyComment);

setIsNamePresent(true);
setIsEmailPresent(true);
Expand All @@ -37,7 +38,7 @@ export const NewCommentForm: React.FC<Props> = ({ addNewComment }) => {
setIsLoading(true);
addNewComment({ name, email, body: text })
.then(() => setIsLoading(false))
.finally(() => setText(''));
.finally(() => setComment(prevValue => ({ ...prevValue, text: '' })));
}
};

Expand All @@ -59,9 +60,8 @@ export const NewCommentForm: React.FC<Props> = ({ addNewComment }) => {
})}
value={name}
onChange={(e) => {
setName(e.target.value);
setComment(prevValue => ({ ...prevValue, name: e.target.value }));
}}
onBlur={() => setName(name.trim())}
/>

<span className="icon is-small is-left">
Expand Down Expand Up @@ -101,9 +101,10 @@ export const NewCommentForm: React.FC<Props> = ({ addNewComment }) => {
})}
value={email}
onChange={(e) => {
setEmail(e.target.value);
setComment(
prevValue => ({ ...prevValue, email: e.target.value }),
);
}}
onBlur={() => setEmail(email.trim())}
/>

<span className="icon is-small is-left">
Expand Down Expand Up @@ -142,9 +143,8 @@ export const NewCommentForm: React.FC<Props> = ({ addNewComment }) => {
})}
value={text}
onChange={(e) => {
setText(() => e.target.value);
setComment(prevValue => ({ ...prevValue, text: e.target.value }));
}}
onBlur={() => setText(text.trim())}
/>
</div>

Expand Down
2 changes: 1 addition & 1 deletion src/components/PostDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export const PostDetails: React.FC<Props> = ({ post }) => {

{isFormOpened && (
<NewCommentForm
addNewComment={(newComment) => handleAddNewComment(newComment)}
addNewComment={handleAddNewComment}
/>
)}
</div>
Expand Down

0 comments on commit 0b090d7

Please sign in to comment.