Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ This task is similar to [Static List of TODOs](https://github.com/mate-academy/r
- Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Open one more terminal and run tests with `npm test` to ensure your solution is correct.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_static-list-of-posts/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://MateuszCieplak.github.io/react_static-list-of-posts/) and add it to the PR description.
21 changes: 0 additions & 21 deletions src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,6 @@ iframe {
text-align: center;
}

.PostInfo {
margin: 10px auto;
width: 90%;
border: 1px solid #000;
border-radius: 8px;
background-color: lightgray;
padding: 1em;

&__title {
margin: 0;
}

&__header {
margin-bottom: 1em;
}
}

.UserInfo {
font-weight: bold;
}

.CommentList {
display: flex;
flex-direction: column;
Expand Down
112 changes: 19 additions & 93 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,105 +2,31 @@ import React from 'react';

import './App.scss';

// import postsFromServer from './api/posts';
// import commentsFromServer from './api/comments';
// import usersFromServer from './api/users';
import postsFromServer from './api/posts';
import commentsFromServer from './api/comments';
import usersFromServer from './api/users';
import { Post } from './components/Types/Posts';
import { User } from './components/Types/Users';
import { PostList } from './components/PostList';

function getUser(userId: number): User {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The function getUser should be renamed to getUserById to clearly indicate that it retrieves a user by their ID. This aligns with the checklist item: "If you are searching some entity by some filter, specify it in function/method name".

const foundUser = usersFromServer.find(user => user.id === userId);

return foundUser || null;
}

export const posts: Post[] = postsFromServer.map(post => ({
...post,
user: getUser(post.userId),
comments: commentsFromServer.filter(comment => comment.postId === post.id),
}));

export const App: React.FC = () => (
<section className="App">
<h1 className="App__title">Static list of posts</h1>

<div className="PostList">
<div className="PostInfo">
<div className="PostInfo__header">
<h3 className="PostInfo__title">qui est esse</h3>

<p>
{' Posted by '}

<a className="UserInfo" href="mailto:Sincere@april.biz">
Leanne Graham
</a>
</p>
</div>

<p className="PostInfo__body">
est rerum tempore vitae sequi sint nihil reprehenderit dolor beatae ea
dolores neque fugiat blanditiis voluptate porro vel nihil molestiae ut
reiciendis qui aperiam non debitis possimus qui neque nisi nulla
</p>

<hr />

<b data-cy="NoCommentsMessage">No comments yet</b>
</div>

<div className="PostInfo">
<div className="PostInfo__header">
<h3 className="PostInfo__title">doloremque illum aliquid sunt</h3>

<p>
{' Posted by '}

<a className="UserInfo" href="mailto:Julianne.OConner@kory.org">
Patricia Lebsack
</a>
</p>
</div>

<p className="PostInfo__body">
deserunt eos nobis asperiores et hic est debitis repellat molestiae
optio nihil ratione ut eos beatae quibusdam distinctio maiores earum
voluptates et aut adipisci ea maiores voluptas maxime
</p>

<div className="CommentList">
<div className="CommentInfo">
<div className="CommentInfo__title">
<strong className="CommentInfo__name">pariatur omnis in</strong>

{' by '}

<a
className="CommentInfo__email"
href="mailto:Telly_Lynch@karl.co.uk"
>
Telly_Lynch@karl.co.uk
</a>
</div>

<div className="CommentInfo__body">
dolorum voluptas laboriosam quisquam ab totam beatae et aut
aliquid optio assumenda voluptas velit itaque quidem voluptatem
tempore cupiditate in itaque sit molestiae minus dolores magni
</div>
</div>

<div className="CommentInfo">
<div className="CommentInfo__title">
<strong className="CommentInfo__name">
odio adipisci rerum aut animi
</strong>

{' by '}

<a
className="CommentInfo__email"
href="mailto:Nikita@garfield.biz"
>
Nikita@garfield.biz
</a>
</div>

<div className="CommentInfo__body">
quia molestiae reprehenderit quasi aspernatur aut expedita
occaecati aliquam eveniet laudantium omnis quibusdam delectus
saepe quia accusamus maiores nam est cum et ducimus et vero
voluptates excepturi deleniti ratione
</div>
</div>
</div>
</div>
<PostList posts={posts} />
</div>
</section>
);
19 changes: 18 additions & 1 deletion src/components/CommentInfo/CommentInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
import React from 'react';
import { Comment } from '../Types/Comments';

export const CommentInfo: React.FC = () => <>Put the comment here</>;
export const CommentInfo: React.FC<{ comment: Comment }> = ({ comment }) => {
return (
<div className="CommentInfo">
<div className="CommentInfo__title">
<strong className="CommentInfo__name">{comment.name}</strong>

{' by '}

<a className="CommentInfo__email" href={`mailto:${comment.email}`}>
{comment.email}
</a>
</div>

<div className="CommentInfo__body">{comment.body}</div>
</div>
);
};
7 changes: 7 additions & 0 deletions src/components/CommentList/CommentList.scss
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
// add styles here
.CommentList {
display: flex;
flex-direction: column;
gap: 0.7em;
background-color: #eee;
padding: 1em;
}
25 changes: 24 additions & 1 deletion src/components/CommentList/CommentList.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/* eslint-disable max-len */
import React from 'react';
import '../CommentList/CommentList.scss';

export const CommentList: React.FC = () => <>Put the list here</>;
import { Comment } from '../Types/Comments';
import { CommentInfo } from '../CommentInfo';

export const CommentList: React.FC<{ comments: Comment[] }> = ({
comments,
}) => {
return (
<div className="CommentList">
{comments.length > 0 ? (
comments.map(comment => (
<CommentInfo key={comment.id} comment={comment} />
))
) : (
<>
<hr />

<b data-cy="NoCommentsMessage">No comments yet</b>
</>
Comment on lines +18 to +22

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Avoid using a React fragment (<>...</>) when there is already a wrapper element present, such as the <div> with the class CommentList. This aligns with the checklist item: "Don't use React fragment, if you already have a wrapper".

)}
</div>
);
};
16 changes: 16 additions & 0 deletions src/components/PostInfo/PostInfo.scss
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
// add styles here
.PostInfo {
margin: 10px auto;
width: 90%;
border: 1px solid #000;
border-radius: 8px;
background-color: lightgray;
padding: 1em;

&__title {
margin: 0;
}

&__header {
margin-bottom: 1em;
}
}
23 changes: 22 additions & 1 deletion src/components/PostInfo/PostInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
import React from 'react';
import '../PostInfo/PostInfo.scss';

export const PostInfo: React.FC = () => <>Put the post here</>;
import { Post } from '../Types/Posts';
import { CommentList } from '../CommentList';
import { UserInfo } from '../UserInfo';

export const PostInfo: React.FC<{ post: Post }> = ({ post }) => {
return (
<>
<div className="PostInfo">
<div className="PostInfo__header">
<h3 className="PostInfo__title">{post.title}</h3>
<p>
{' Posted by '}
<UserInfo user={post.user} />
</p>
</div>
<p className="PostInfo__body">{post.body}</p>
<CommentList comments={post.comments} />
</div>
</>
Comment on lines +10 to +22

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Avoid using a React fragment (<>...</>) when there is already a wrapper element present, such as the <div> with the class PostInfo. This aligns with the checklist item: "Don't use React fragment, if you already have a wrapper".

);
};
12 changes: 11 additions & 1 deletion src/components/PostList/PostList.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import React from 'react';
import { Post } from '../Types/Posts';
import { PostInfo } from '../PostInfo';

export const PostList: React.FC = () => <>Put the list here</>;
export const PostList: React.FC<{ posts: Post[] }> = ({ posts }) => {
return (
<>
{posts.map(post => (
<PostInfo key={post.id} post={post} />
))}
</>
Comment on lines +7 to +11

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Avoid using a React fragment (<>...</>) when there is already a wrapper element present, such as the <div> with the class PostList. This aligns with the checklist item: "Don't use React fragment, if you already have a wrapper" .

);
};
7 changes: 7 additions & 0 deletions src/components/Types/Comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface Comment {
postId: number;
id: number;
name: string;
email: string;
body: string;
}
11 changes: 11 additions & 0 deletions src/components/Types/Posts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { User } from './Users';
import { Comment } from './Comments';

export interface Post {
userId: number;
id: number;
title: string;
body: string;
user: User;
comments: Comment[];
}
5 changes: 5 additions & 0 deletions src/components/Types/Users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface User {
id: number;
name: string;
email: string;
}
3 changes: 3 additions & 0 deletions src/components/UserInfo/UserInfo.scss
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
// add styles here
.UserInfo {
font-weight: bold;
}
10 changes: 9 additions & 1 deletion src/components/UserInfo/UserInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import React from 'react';
import '../UserInfo/UserInfo.scss';
import { User } from '../Types/Users';

export const UserInfo: React.FC = () => <>Put the user here</>;
export const UserInfo: React.FC<{ user: User }> = ({ user }) => {
return (
<a className="UserInfo" href={`mailto:${user.email}`}>
{user.name}
</a>
);
};