Skip to content

Commit

Permalink
newCommentForm logic done
Browse files Browse the repository at this point in the history
  • Loading branch information
Iryna Mariiko authored and Iryna Mariiko committed Dec 29, 2024
1 parent f17da9f commit a0cdbb3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
46 changes: 25 additions & 21 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export const App = () => {
<UserSelector
users={users}
activeUser={activeUser}
setActiveUser={setActiveUser}
setActiveUser={user => {
setActiveUser(user);
setActivePost(null);
}}
/>
</div>

Expand All @@ -73,32 +76,33 @@ export const App = () => {
No posts yet
</div>
)}

<PostsList
isPostsList={isPostsList}
posts={posts}
setActivePost={setActivePost}
activePost={activePost}
/>
{!isLoading && posts.length > 0 && (
<PostsList
isPostsList={isPostsList}
posts={posts}
setActivePost={setActivePost}
activePost={activePost}
/>
)}
</div>
</div>
</div>
{activePost && (
<div
data-cy="Sidebar"
className={classNames(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
'Sidebar--open',
)}
>
<div
data-cy="Sidebar"
className={classNames(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
{ 'Sidebar--open': activePost !== null },
)}
>
{activePost && (
<div className="tile is-child box is-success ">
<PostDetails activePost={activePost} />
</div>
</div>
)}
)}
</div>
</div>
</div>
</main>
Expand Down
6 changes: 5 additions & 1 deletion src/components/NewCommentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ export const NewCommentForm: React.FC<Props> = ({

onAddComment(formData);

setFormData({ name: '', email: '', body: '' });
setFormData(prevFormData => ({
...prevFormData,
body: '',
}));
};

const handleClearComment = (evnt: React.FormEvent<HTMLFormElement>) => {
evnt.preventDefault();
setIsError({ name: false, email: false, body: false });

setFormData({ name: '', email: '', body: '' });
};
Expand Down
3 changes: 1 addition & 2 deletions src/components/PostDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,11 @@ export const PostDetails: React.FC<Props> = ({ activePost }) => {
))}
</>
)}
{!isLoadingComments && (
{!isLoadingComments && !isCommentFormVisible && !isError && (
<button
data-cy="WriteCommentButton"
type="button"
className="button is-link"
style={{ display: isCommentFormVisible ? 'none' : 'block' }}
onClick={() => setIsCommentFormVisible(true)}
>
Write a comment
Expand Down
21 changes: 20 additions & 1 deletion src/components/UserSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useRef, useEffect } from 'react';
import { User } from '../types/User';
import classNames from 'classnames';

Expand All @@ -14,6 +14,24 @@ export const UserSelector: React.FC<Props> = ({
setActiveUser,
}) => {
const [isDropDownVisible, setIsDropDownVisible] = useState(false);
const dropdownRef = useRef(null);

const onOutsideClick = (event: MouseEvent) => {
if (
dropdownRef.current &&
!(dropdownRef.current as HTMLElement).contains(event.target as Node)
) {
setIsDropDownVisible(false);
}
};

useEffect(() => {
document.addEventListener('mousedown', onOutsideClick);

return () => {
document.removeEventListener('mousedown', onOutsideClick);
};
}, []);

const toggleDropDown = () => {
setIsDropDownVisible(!isDropDownVisible);
Expand All @@ -22,6 +40,7 @@ export const UserSelector: React.FC<Props> = ({
return (
<div
data-cy="UserSelector"
ref={dropdownRef}
className={classNames('dropdown', {
'is-active': users.length > 0 && isDropDownVisible,
})}
Expand Down

0 comments on commit a0cdbb3

Please sign in to comment.