Skip to content
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 #1186

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,35 @@
}
}

.tile {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a comment or documentation for the .tile class to explain its purpose or usage, especially since it contains nested styles and media queries.

align-items: stretch;
min-height: min-content;
flex: 1 1 0;
Comment on lines +21 to +24

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .tile class uses flex: 1 1 0;, which might not behave as expected if the parent container is not a flex container. Ensure that the parent of .tile is a flex container to utilize flex properties effectively.


&.is-ancestor {
margin-left: -0.75rem;
margin-right: -0.75rem;
margin-top: -0.75rem;

&:last-child {
margin-bottom: -0.75rem;
}
}

@media (min-width: 769px) {
&:not(.is-child) {
display: flex;
}
}
}

.message-body {
white-space: pre-line;
}

.container {
flex-grow: 1;
margin: 0 auto;
position: relative;
width: auto;
}
131 changes: 87 additions & 44 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,103 @@
import classNames from 'classnames';

/* eslint-disable @typescript-eslint/indent */
import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';
import cn from 'classnames';
import './App.scss';

import { PostsList } from './components/PostsList';
import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';

export const App = () => (
<main className="section">
<div className="container">
<div className="tile is-ancestor">
<div className="tile is-parent">
<div className="tile is-child box is-success">
<div className="block">
<UserSelector />
</div>
import { PostsList, PostDetails, UserSelector, Loader } from './components';
import { User } from './types';
import { usePosts, useUsers } from './hooks';

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
export const App = () => {
const {
users,
errorMessage: usersErrorMessage,
selectedUser,
setSelectedUser,
} = useUsers();
const {
posts,
selectedPost,
setSelectedPost,
isLoading,
errorMessage: postsErrorMessage,
} = usePosts(selectedUser?.id);

<Loader />
const errorMessage = usersErrorMessage || postsErrorMessage;

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
const handleSelectUserId = (user: User) => {
setSelectedUser(user);
setSelectedPost(null);
};

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
return (
<main className="section">
<div className="container">
<div className="tile is-ancestor">
<div className="tile is-parent">
<div className="tile is-child box is-success">
<div className="block">
<UserSelector
users={users}
selectedUser={selectedUser}
onSelectUser={handleSelectUserId}
/>
</div>

<PostsList />
<div className="block" data-cy="MainContent">
{!selectedUser && (
<p data-cy="NoSelectedUser">No user selected</p>
)}

{selectedUser && isLoading && <Loader />}

{selectedUser && !isLoading && errorMessage && (
<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
{errorMessage}
</div>
)}

{selectedUser &&
!isLoading &&
!errorMessage &&
posts?.length === 0 && (
<div
className="notification is-warning"
data-cy="NoPostsYet"
>
No posts yet
</div>
)}

{selectedUser &&
!isLoading &&
!errorMessage &&
posts?.length !== 0 && (
<PostsList
posts={posts}
selectedPost={selectedPost}
onSelectPost={setSelectedPost}
/>
)}
</div>
</div>
</div>
</div>

<div
data-cy="Sidebar"
className={classNames(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
'Sidebar--open',
)}
>
<div className="tile is-child box is-success ">
<PostDetails />
<div
data-cy="Sidebar"
className={cn('tile is-parent is-8-desktop Sidebar', {
'Sidebar--open': selectedPost,
})}
>
Comment on lines +91 to +93

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The className for the sidebar uses classnames to conditionally apply Sidebar--open. Ensure that selectedPost is the correct condition for opening the sidebar. If there are other conditions or states that should affect this, consider adding them.

Comment on lines +91 to +93

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The classnames library is used correctly to conditionally apply the Sidebar--open class. However, ensure that the selectedPost variable is correctly initialized and managed to avoid unexpected behavior when toggling the sidebar's open state.

<div className="tile is-child box is-success ">
{selectedPost && <PostDetails selectedPost={selectedPost} />}
</div>
</div>
</div>
</div>
</div>
</main>
);
</main>
);
};
14 changes: 14 additions & 0 deletions src/api/comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Comment } from '../types/Comment';
import { client } from '../utils/fetchClient';

export const getComments = (postId: number) => {
return client.get<Comment[]>(`/comments?postId=${postId}`);
};

export const deleteComment = (commentId: number) => {
return client.delete(`/comments/${commentId}`);
};

export const addComment = (newComment: Omit<Comment, 'id'>) => {
return client.post<Comment>('/comments', newComment);
};
6 changes: 6 additions & 0 deletions src/api/posts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Post } from '../types/Post';
import { client } from '../utils/fetchClient';

export const getUserPosts = (selectedUserId: number) => {
return client.get<Post[]>(`/posts?userId=${selectedUserId}`);
};
6 changes: 6 additions & 0 deletions src/api/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { User } from '../types/User';
import { client } from '../utils/fetchClient';

export const getUsers = () => {
return client.get<User[]>('/users');
};
33 changes: 33 additions & 0 deletions src/components/CommentItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { FC } from 'react';

import { Comment } from '../types';

type Props = {
comment: Comment;
onDelete: (commentId: number) => Promise<void>;
};

export const CommentItem: FC<Props> = ({ comment, onDelete }) => {
const { id, name, email, body } = comment;

return (
<article className="message is-small" data-cy="Comment">
<div className="message-header">
<a href={`mailto:${email}`} data-cy="CommentAuthor">
{name}
</a>
<button
data-cy="CommentDelete"
type="button"
className="delete is-small"
aria-label="delete"
onClick={() => onDelete(id)}
></button>
</div>

<div className="message-body" data-cy="CommentBody">
{body}
</div>
</article>
);
};
Loading
Loading