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

dynamic list of posts completed #1188

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
278 changes: 190 additions & 88 deletions cypress/integration/page.spec.js

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@mate-academy/scripts": "^1.8.5",
"@mate-academy/scripts": "^1.9.12",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
29 changes: 29 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,32 @@
.message-body {
white-space: pre-line;
}

@media print, screen and (min-width: 769px) {

Choose a reason for hiding this comment

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

The media query combines print and screen and (min-width: 769px). Ensure this is intentional, as it affects both print and screen media types.

.tile:not(.is-child) {
display: flex;
}
}

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

.tile.is-ancestor:last-child {
margin-bottom: -0.75rem;
}

.tile {
align-items: stretch;
min-height: min-content;
flex: 1 1 0;
}

.container {
flex-grow: 1;
margin: 0 auto;
position: relative;
width: auto;
}
127 changes: 90 additions & 37 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,110 @@ import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';
import './App.scss';

import * as userService from './api/users';
import * as postService from './api/posts';
import { PostsList } from './components/PostsList';
import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';
import { useEffect, useState } from 'react';
import { User } from './types/User';
import { Post } from './types/Post';

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>
export const App = () => {
const [users, setUsers] = useState<User[]>([]);
const [selectedUser, setSelectedUser] = useState<User | null>(null);
const [isPostsLoading, setIsPostsLoading] = useState(false);
const [userPosts, setUserPosts] = useState<Post[]>([]);
const [selectedPost, setSelectedPost] = useState<Post | null>(null);
const [hasError, setHasError] = useState(false);

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
useEffect(() => {
userService
.getUsers()
.then(setUsers)
.catch(() => setHasError(true));
}, []);

<Loader />
useEffect(() => {
setIsPostsLoading(true);
postService
.getUserPosts(selectedUser?.id || 0)

Choose a reason for hiding this comment

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

The getUserPosts function is called with selectedUser?.id || 0. This could lead to unintended behavior if selectedUser is null. Consider handling this case more explicitly.

.then(setUserPosts)
.catch(() => setHasError(true))
.finally(() => setIsPostsLoading(false));
}, [selectedUser]);

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
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}
setSelectedUser={setSelectedUser}
/>
</div>

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
</div>
{isPostsLoading ? (
<Loader />
) : (
<div className="block" data-cy="MainContent">
{!selectedUser && (
<p data-cy="NoSelectedUser">No user selected</p>
)}

{hasError && !isPostsLoading && (
<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
)}

<PostsList />
{selectedUser &&
!hasError &&
(!userPosts.length ? (
<div
className="notification is-warning"
data-cy="NoPostsYet"
>
No posts yet
</div>
) : (
<PostsList
userPosts={userPosts}
selectedPost={selectedPost}
setSelectedPost={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={classNames(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
{ 'Sidebar--open': selectedPost !== null },
)}
>
{selectedPost && (
<div className="tile is-child box is-success ">
<PostDetails selestedPost={selectedPost} />

Choose a reason for hiding this comment

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

There is a typo in the PostDetails component prop. It should be selectedPost instead of selestedPost.

</div>
)}
</div>
</div>
</div>
</div>
</main>
);
</main>
);
};
20 changes: 20 additions & 0 deletions src/api/comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Comment } from '../types/Comment';
import { client } from '../utils/fetchClient';

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

export const createComment = ({
postId,
name,
email,
body,
}: Omit<Comment, 'id'>) => {
return client.post<Comment>(`/comments`, { postId, name, email, body });
};

export const deleteComment = (commId: Comment['id']) => {

Choose a reason for hiding this comment

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

The parameter name commId is inconsistent with the rest of the codebase where commentId might be more appropriate for clarity and consistency.

return client.delete(`/comments/${commId}`);
};
7 changes: 7 additions & 0 deletions src/api/posts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Post } from '../types/Post';
import { client } from '../utils/fetchClient';

export const getUserPosts = (userId: Post['userId']) => {
return client.get<Post[]>(`
/posts?userId=${userId}`);
};
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');
};
36 changes: 36 additions & 0 deletions src/components/CommentList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import { Comment } from '../types/Comment';

type Props = {
comments: Comment[];
onDelete: (id: number) => void;
};

export const CommentList: React.FC<Props> = ({ comments, onDelete }) => {
return (
<>
<p className="title is-4">Comments:</p>

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

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