From 1af4484fb7198723471996e974385aa03fbaf03f Mon Sep 17 00:00:00 2001 From: Ovramenko Andrii Date: Sun, 2 Mar 2025 21:00:58 +0200 Subject: [PATCH] add task solution --- README.md | 2 +- src/App.jsx | 117 ++++---------------- src/App.scss | 29 ----- src/components/CommentInfo/CommentInfo.jsx | 16 ++- src/components/CommentList/CommentList.jsx | 14 ++- src/components/CommentList/CommentList.scss | 8 +- src/components/PostInfo/PostInfo.jsx | 23 +++- src/components/PostInfo/PostInfo.scss | 17 ++- src/components/PostList/PostList.jsx | 10 +- src/components/UserInfo/UserInfo.jsx | 8 +- src/components/UserInfo/UserInfo.scss | 4 +- 11 files changed, 115 insertions(+), 133 deletions(-) diff --git a/README.md b/README.md index ab3abde5..b3c8a62a 100644 --- a/README.md +++ b/README.md @@ -13,4 +13,4 @@ This task is similar to [Static List of TODOs](https://github.com/mate-academy/r - Install Prettier Extention and use this [VSCode settings](https://mate-academy.github.io/fe-program/tools/vscode/settings.json) to enable format on save. - Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline). - Open one more terminal and run tests with `npm test` to ensure your solution is correct. -- Replace `` with your Github username in the [DEMO LINK](https://.github.io/react_static-list-of-posts-js/) and add it to the PR description. +- Replace `` with your Github username in the [DEMO LINK](https://AnOvramenko.github.io/react_static-list-of-posts-js/) and add it to the PR description. diff --git a/src/App.jsx b/src/App.jsx index 22ae1b9c..62f84821 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,104 +1,31 @@ import './App.scss'; -// import postsFromServer from './api/posts.json'; -// import commentsFromServer from './api/comments.json'; -// import usersFromServer from './api/users.json'; +import postsFromServer from './api/posts.json'; +import commentsFromServer from './api/comments.json'; +import usersFromServer from './api/users.json'; +import { PostList } from './components/PostList/PostList'; -export const App = () => ( -
-

Static list of posts

- -
-
-
-

qui est esse

- -

- {' Posted by '} - - - Leanne Graham - -

-
- -

- 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 -

- -
- - No comments yet -
- -
-
-

doloremque illum aliquid sunt

+function getUserById(userId) { + return usersFromServer.find(user => user.id === userId); +} -

- {' Posted by '} +function getPostComments(postId) { + const comments = commentsFromServer.filter( + comment => comment.postId === postId, + ); - - Patricia Lebsack - -

-
+ return comments; +} -

- 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 -

+const posts = postsFromServer.map(post => ({ + ...post, + user: getUserById(post.userId), + comments: getPostComments(post.id), +})); -
-
-
- pariatur omnis in - - {' by '} - - - Telly_Lynch@karl.co.uk - -
- -
- 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 -
-
- -
-
- - odio adipisci rerum aut animi - - - {' by '} - - - Nikita@garfield.biz - -
- -
- 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 -
-
-
-
-
+export const App = () => ( +
+

Static list of posts

+
); diff --git a/src/App.scss b/src/App.scss index 98bb3956..64365095 100644 --- a/src/App.scss +++ b/src/App.scss @@ -5,32 +5,3 @@ iframe { .App__title { 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; - gap: 0.7em; - background-color: #eee; - padding: 1em; -} diff --git a/src/components/CommentInfo/CommentInfo.jsx b/src/components/CommentInfo/CommentInfo.jsx index f99e2778..1b34fe52 100644 --- a/src/components/CommentInfo/CommentInfo.jsx +++ b/src/components/CommentInfo/CommentInfo.jsx @@ -1 +1,15 @@ -export const CommentInfo = () => <>Put the comment here; +export const CommentInfo = ({ comment: { name, email, body } }) => ( +
+
+ {name} + + {' by '} + + + {email} + +
+ +
{body}
+
+); diff --git a/src/components/CommentList/CommentList.jsx b/src/components/CommentList/CommentList.jsx index 263dfbc7..d9634236 100644 --- a/src/components/CommentList/CommentList.jsx +++ b/src/components/CommentList/CommentList.jsx @@ -1 +1,13 @@ -export const CommentList = () => <>Put the list here; +import { CommentInfo } from '../CommentInfo/CommentInfo'; +import './CommentList.scss'; + +export const CommentList = ({ comments }) => + comments.length === 0 ? ( + No comments yet + ) : ( +
+ {comments.map(comment => ( + + ))} +
+ ); diff --git a/src/components/CommentList/CommentList.scss b/src/components/CommentList/CommentList.scss index f1b40ee0..91c2ed5f 100644 --- a/src/components/CommentList/CommentList.scss +++ b/src/components/CommentList/CommentList.scss @@ -1 +1,7 @@ -// add styles here +.CommentList { + display: flex; + flex-direction: column; + gap: 0.7em; + background-color: #eee; + padding: 1em; +} diff --git a/src/components/PostInfo/PostInfo.jsx b/src/components/PostInfo/PostInfo.jsx index eb3efb78..f5dc11dc 100644 --- a/src/components/PostInfo/PostInfo.jsx +++ b/src/components/PostInfo/PostInfo.jsx @@ -1 +1,22 @@ -export const PostInfo = () => <>Put the post here; +import { CommentList } from '../CommentList/CommentList'; +import { UserInfo } from '../UserInfo/UserInfo'; +import './PostInfo.scss'; + +export const PostInfo = ({ post: { title, body, comments, user } }) => ( +
+
+

{title}

+ +

+ {' Posted by '} + +

+
+ +

{body}

+ +
+ + +
+); diff --git a/src/components/PostInfo/PostInfo.scss b/src/components/PostInfo/PostInfo.scss index f1b40ee0..1ad49fd3 100644 --- a/src/components/PostInfo/PostInfo.scss +++ b/src/components/PostInfo/PostInfo.scss @@ -1 +1,16 @@ -// 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; + } +} diff --git a/src/components/PostList/PostList.jsx b/src/components/PostList/PostList.jsx index 4487a96c..c81a8339 100644 --- a/src/components/PostList/PostList.jsx +++ b/src/components/PostList/PostList.jsx @@ -1 +1,9 @@ -export const PostList = () => <>Put the list here; +import { PostInfo } from '../PostInfo/PostInfo'; + +export const PostList = ({ posts }) => ( +
+ {posts.map(post => ( + + ))} +
+); diff --git a/src/components/UserInfo/UserInfo.jsx b/src/components/UserInfo/UserInfo.jsx index 6264e2f7..08b7791a 100644 --- a/src/components/UserInfo/UserInfo.jsx +++ b/src/components/UserInfo/UserInfo.jsx @@ -1 +1,7 @@ -export const UserInfo = () => <>Put the user here; +import './UserInfo.scss'; + +export const UserInfo = ({ user: { name, email } }) => ( + + {name} + +); diff --git a/src/components/UserInfo/UserInfo.scss b/src/components/UserInfo/UserInfo.scss index f1b40ee0..5080de7c 100644 --- a/src/components/UserInfo/UserInfo.scss +++ b/src/components/UserInfo/UserInfo.scss @@ -1 +1,3 @@ -// add styles here +.UserInfo { + font-weight: bold; +}