From afac38ce78ec859d614189e68687ecc9ba1ae36a Mon Sep 17 00:00:00 2001 From: Timi2424 Date: Fri, 1 Dec 2023 17:05:49 +0100 Subject: [PATCH] solution --- README.md | 2 +- src/App.scss | 29 ----- src/App.tsx | 122 +++++--------------- src/components/CommentInfo/CommentInfo.tsx | 22 +++- src/components/CommentList/CommentList.scss | 8 +- src/components/CommentList/CommentList.tsx | 22 +++- src/components/PostInfo/PostInfo.scss | 17 ++- src/components/PostInfo/PostInfo.tsx | 35 +++++- src/components/PostList/PostList.tsx | 18 ++- src/components/UserInfo/UserInfo.scss | 4 +- src/components/UserInfo/UserInfo.tsx | 18 ++- src/types/CommentsTypes.ts | 7 ++ src/types/PostsTypes.ts | 11 ++ src/types/UserTypes.ts | 6 + 14 files changed, 181 insertions(+), 140 deletions(-) create mode 100644 src/types/CommentsTypes.ts create mode 100644 src/types/PostsTypes.ts create mode 100644 src/types/UserTypes.ts diff --git a/README.md b/README.md index fde130de80..00102c58b5 100644 --- a/README.md +++ b/README.md @@ -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 `` with your Github username in the [DEMO LINK](https://.github.io/react_static-list-of-posts/) and add it to the PR description. +- Replace `` with your Github username in the [DEMO LINK](https://Timi2424.github.io/react_static-list-of-posts/) and add it to the PR description. diff --git a/src/App.scss b/src/App.scss index 98bb3956c3..6436509521 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/App.tsx b/src/App.tsx index a068b02fb5..3c8c444982 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,109 +1,39 @@ -import React from 'react'; - import './App.scss'; -// import postsFromServer from './api/posts'; -// import commentsFromServer from './api/comments'; -// import usersFromServer from './api/users'; - -export const App: React.FC = () => ( -
-

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 -

+import postsFromServer from './api/posts'; +import commentsFromServer from './api/comments'; +import usersFromServer from './api/users'; -
+import { PostList } from './components/PostList'; - No comments yet -
+import { Post } from './types/PostsTypes'; +import { User } from './types/UserTypes'; +import { Comment } from './types/CommentsTypes'; -
-
-

- doloremque illum aliquid sunt -

+function getUser(userId: number): User | null { + const foundUser = usersFromServer.find(user => user.id === userId); -

- {' Posted by '} + return foundUser || null; +} - - Patricia Lebsack - -

-
+function getComment(postId: number): Comment[] { + const foundComment = commentsFromServer.filter( + comment => comment.postId === postId, + ); -

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

+ return foundComment; +} -
-
-
- pariatur omnis in +export const posts: Post[] = postsFromServer.map(post => ({ + ...post, + user: getUser(post.userId), + comments: getComment(post.id), +})); - {' 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 - -
+export const App: React.FC = () => ( +
+

Static list of posts

-
- 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 -
-
-
-
-
+
); diff --git a/src/components/CommentInfo/CommentInfo.tsx b/src/components/CommentInfo/CommentInfo.tsx index 4343b8410c..4a1e8d0815 100644 --- a/src/components/CommentInfo/CommentInfo.tsx +++ b/src/components/CommentInfo/CommentInfo.tsx @@ -1,5 +1,21 @@ -import React from 'react'; +import { Comment } from '../../types/CommentsTypes'; -export const CommentInfo: React.FC = () => ( - <>Put the comment here +type Props = { + comment: Comment; +}; + +export const CommentInfo: React.FC = ({ comment }) => ( +
+
+ {comment.name} + + {' by '} + + + {comment.email} + +
+ +
{comment.body}
+
); diff --git a/src/components/CommentList/CommentList.scss b/src/components/CommentList/CommentList.scss index f1b40ee00e..91c2ed5fbb 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/CommentList/CommentList.tsx b/src/components/CommentList/CommentList.tsx index ead6a64da7..fee6d16a6e 100644 --- a/src/components/CommentList/CommentList.tsx +++ b/src/components/CommentList/CommentList.tsx @@ -1,5 +1,23 @@ import React from 'react'; -export const CommentList: React.FC = () => ( - <>Put the list here +import './CommentList.scss'; + +import { Comment } from '../../types/CommentsTypes'; +import { CommentInfo } from '../CommentInfo'; + +type CommentsProps = { + comments: Comment[]; +}; + +export const CommentList: React.FC = ({ comments }) => ( + <> +
+ {comments.map(comment => ( + + ))} +
+ ); diff --git a/src/components/PostInfo/PostInfo.scss b/src/components/PostInfo/PostInfo.scss index f1b40ee00e..1ad49fd344 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/PostInfo/PostInfo.tsx b/src/components/PostInfo/PostInfo.tsx index 421b94cb7b..7c6379a935 100644 --- a/src/components/PostInfo/PostInfo.tsx +++ b/src/components/PostInfo/PostInfo.tsx @@ -1,5 +1,36 @@ import React from 'react'; -export const PostInfo: React.FC = () => ( - <>Put the post here +import './PostInfo.scss'; + +import { Post } from '../../types/PostsTypes'; +import { UserInfo } from '../UserInfo'; +import { CommentList } from '../CommentList'; + +type Props = { + post: Post; +}; + +export const PostInfo: React.FC = ({ post }) => ( +
+
+

+ {post.title} +

+ + {post.user && } +
+ +

+ {post.body} +

+ + {post.comments.length !== 0 + ? + : ( + <> +
+ No comments yet + + )} +
); diff --git a/src/components/PostList/PostList.tsx b/src/components/PostList/PostList.tsx index b5318a2735..8842370094 100644 --- a/src/components/PostList/PostList.tsx +++ b/src/components/PostList/PostList.tsx @@ -1,5 +1,19 @@ import React from 'react'; -export const PostList: React.FC = () => ( - <>Put the list here +import { Post } from '../../types/PostsTypes'; +import { PostInfo } from '../PostInfo'; + +type Props = { + posts: Post[]; +}; + +export const PostList: React.FC = ({ posts }) => ( +
+ {posts.map(post => ( + + ))} +
); diff --git a/src/components/UserInfo/UserInfo.scss b/src/components/UserInfo/UserInfo.scss index f1b40ee00e..5080de7cd7 100644 --- a/src/components/UserInfo/UserInfo.scss +++ b/src/components/UserInfo/UserInfo.scss @@ -1 +1,3 @@ -// add styles here +.UserInfo { + font-weight: bold; +} diff --git a/src/components/UserInfo/UserInfo.tsx b/src/components/UserInfo/UserInfo.tsx index 89faeae3c9..70167b80df 100644 --- a/src/components/UserInfo/UserInfo.tsx +++ b/src/components/UserInfo/UserInfo.tsx @@ -1,5 +1,19 @@ import React from 'react'; -export const UserInfo: React.FC = () => ( - <>Put the user here +import './UserInfo.scss'; + +import { User } from '../../types/UserTypes'; + +type Props = { + user: User; +}; + +export const UserInfo: React.FC = ({ user }) => ( +

+ {' Posted by '} + + + {user.name} + +

); diff --git a/src/types/CommentsTypes.ts b/src/types/CommentsTypes.ts new file mode 100644 index 0000000000..14df6303f3 --- /dev/null +++ b/src/types/CommentsTypes.ts @@ -0,0 +1,7 @@ +export interface Comment { + postId: number; + id: number; + name: string; + email: string; + body: string; +} diff --git a/src/types/PostsTypes.ts b/src/types/PostsTypes.ts new file mode 100644 index 0000000000..1141fb9834 --- /dev/null +++ b/src/types/PostsTypes.ts @@ -0,0 +1,11 @@ +import { User } from './UserTypes'; +import { Comment } from './CommentsTypes'; + +export interface Post { + userId: number; + id: number; + title: string; + body: string; + user: User | null; + comments: Comment[]; +} diff --git a/src/types/UserTypes.ts b/src/types/UserTypes.ts new file mode 100644 index 0000000000..1f6908b55a --- /dev/null +++ b/src/types/UserTypes.ts @@ -0,0 +1,6 @@ +export interface User { + id: number; + name: string; + username: string; + email: string; +}