Skip to content

Commit

Permalink
added fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sheva10barca committed Jul 18, 2023
1 parent 01b8f11 commit 8405779
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const App: React.FC = () => {
const [selectedPost, setSelectedPost] = useState<Post | null>(null);
const [comments, setComments] = useState<Comment[]>([]);

const [isCommentsLoading, setIsCommentsLoading] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);
const [isCommentError, setIsCommentError] = useState(false);
Expand All @@ -49,13 +50,18 @@ export const App: React.FC = () => {
};

const getCommentsFromServer = (postId: number) => {
setIsCommentsLoading(true);

getComments(postId)
.then((data) => {
setComments(data);
setIsCommentError(false);
})
.catch(() => setIsCommentError(true))
.finally(() => setIsLoading(false));
.finally(() => {
setIsLoading(false);
setIsCommentsLoading(false);
});
};

const handleUserSelect = (
Expand All @@ -66,6 +72,7 @@ export const App: React.FC = () => {

getUserPostsFromServer(user.id);
setSelectedUser(user);
setSelectedPost(null);
};

const handleSelectPost = (post: Post) => {
Expand Down Expand Up @@ -113,6 +120,7 @@ export const App: React.FC = () => {
<UserSelector
users={users}
handleUserSelect={handleUserSelect}
selectedUser={selectedUser}
/>
</div>

Expand Down Expand Up @@ -172,6 +180,7 @@ export const App: React.FC = () => {
setCanWriteComment={setCanWriteComment}
handleAddNewComment={handleAddNewComment}
handleDeleteComment={handleDeleteComment}
isCommentsLoading={isCommentsLoading}
/>
)}
</div>
Expand Down
6 changes: 4 additions & 2 deletions src/components/PostDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Props = {
body: string,
) => void;
handleDeleteComment: (commentId: number) => void;
isCommentsLoading: boolean;
};

export const PostDetails: React.FC<Props> = ({
Expand All @@ -27,6 +28,7 @@ export const PostDetails: React.FC<Props> = ({
setCanWriteComment,
handleAddNewComment,
handleDeleteComment,
isCommentsLoading,
}) => {
return (
<div className="content" data-cy="PostDetails">
Expand All @@ -40,7 +42,7 @@ export const PostDetails: React.FC<Props> = ({
</div>

<div className="block">
{!comments && !isCommentError && <Loader />}
{isCommentsLoading && !isCommentError && <Loader />}

{isCommentError && (
<div className="notification is-danger" data-cy="CommentsError">
Expand All @@ -54,7 +56,7 @@ export const PostDetails: React.FC<Props> = ({
</p>
)}

{comments && comments?.length > 0 && (
{comments && !isCommentsLoading && (
<>
<p className="title is-4">Comments:</p>
{comments.map((comment) => (
Expand Down
9 changes: 7 additions & 2 deletions src/components/UserSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ type Props = {
event: React.MouseEvent<HTMLAnchorElement, MouseEvent>,
user: User,
) => void;
selectedUser: User | null;
};

export const UserSelector: React.FC<Props> = ({ users, handleUserSelect }) => {
export const UserSelector: React.FC<Props> = ({
users,
handleUserSelect,
selectedUser,
}) => {
const [isDropDownOpened, setIsDropDownOpened] = useState(false);

return (
Expand All @@ -22,7 +27,7 @@ export const UserSelector: React.FC<Props> = ({ users, handleUserSelect }) => {
aria-controls="dropdown-menu"
onClick={() => setIsDropDownOpened(!isDropDownOpened)}
>
<span>Choose a user</span>
<span>{selectedUser ? selectedUser.name : 'Choose a user'}</span>

<span className="icon is-small">
<i className="fas fa-angle-down" aria-hidden="true" />
Expand Down

0 comments on commit 8405779

Please sign in to comment.