From 1e7cb4d6af8a3a867fdc73bacf7bd1345f77e97e Mon Sep 17 00:00:00 2001 From: Dominik Dudek Date: Sun, 19 Jan 2025 22:54:46 +0100 Subject: [PATCH 1/2] Task solution --- src/App.scss | 29 ----- src/App.tsx | 124 +++++--------------- src/components/CommentInfo/CommentInfo.tsx | 25 +++- src/components/CommentList/CommentList.scss | 8 +- src/components/CommentList/CommentList.tsx | 17 ++- src/components/PostInfo/PostInfo.scss | 17 ++- src/components/PostInfo/PostInfo.tsx | 33 +++++- src/components/PostList/PostList.tsx | 16 ++- src/components/UserInfo/UserInfo.scss | 4 +- src/components/UserInfo/UserInfo.tsx | 18 ++- src/types/Comment..ts | 7 ++ src/types/Post.ts | 11 ++ src/types/User.ts | 6 + 13 files changed, 182 insertions(+), 133 deletions(-) create mode 100644 src/types/Comment..ts create mode 100644 src/types/Post.ts create mode 100644 src/types/User.ts 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 6ccd7807c2..9306cf5d3b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,105 +2,37 @@ 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 { User } from './types/User'; +import { Comment } from './types/Comment.'; +import { Post } from './types/Post'; +import { PostList } from './components/PostList'; + +function getUser(userId: number): User | null { + const foundUser = usersFromServer.find(user => user.id === userId); + + return foundUser || null; +} + +function getComments(postId: number): Comment[] | null { + const comments = commentsFromServer.filter(comment => { + return comment.postId === postId; + }); + + return comments.length > 0 ? comments : null; +} + +export const posts: Post[] = postsFromServer.map(post => ({ + ...post, + user: getUser(post.userId), + comments: getComments(post.id), +})); 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 -

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

doloremque illum aliquid sunt

- -

- {' Posted by '} - - - Patricia Lebsack - -

-
- -

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

- -
-
-
- 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 -
-
-
-
-
+
); diff --git a/src/components/CommentInfo/CommentInfo.tsx b/src/components/CommentInfo/CommentInfo.tsx index 4471a7b9a3..16947399ce 100644 --- a/src/components/CommentInfo/CommentInfo.tsx +++ b/src/components/CommentInfo/CommentInfo.tsx @@ -1,3 +1,26 @@ import React from 'react'; +import { Comment } from '../../types/Comment.'; -export const CommentInfo: React.FC = () => <>Put the comment here; +type Props = { + comment: Comment; +}; + +export const CommentInfo: React.FC = ({ comment }) => { + const { name, email, body } = comment; + + return ( +
+
+ {name} + + {' by '} + + + {email} + +
+ +
{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 8cc7d72e14..cd8c24bcda 100644 --- a/src/components/CommentList/CommentList.tsx +++ b/src/components/CommentList/CommentList.tsx @@ -1,3 +1,18 @@ import React from 'react'; +import { Comment } from '../../types/Comment.'; +import { CommentInfo } from '../CommentInfo'; +import './CommentList.scss'; -export const CommentList: React.FC = () => <>Put the list here; +type Props = { + comments: Comment[]; +}; + +export const CommentList: React.FC = ({ comments }) => { + return ( +
+ {comments.map(comment => { + return ; + })} +
+ ); +}; 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 90220ae5e1..be5d57c274 100644 --- a/src/components/PostInfo/PostInfo.tsx +++ b/src/components/PostInfo/PostInfo.tsx @@ -1,3 +1,34 @@ import React from 'react'; +import { Post } from '../../types/Post'; +import { UserInfo } from '../UserInfo'; +import { CommentList } from '../CommentList'; +import './PostInfo.scss'; -export const PostInfo: React.FC = () => <>Put the post here; +type Props = { + post: Post; +}; + +export const PostInfo: React.FC = ({ post }) => { + const { title, user, body, comments } = post; + const commentsInfo = + !comments || comments.length === 0 ? ( + No comments yet + ) : ( + + ); + + return ( +
+
+

{title}

+ +
+ +

{body}

+ +
+ + {commentsInfo} +
+ ); +}; diff --git a/src/components/PostList/PostList.tsx b/src/components/PostList/PostList.tsx index d7bc7aa5fa..d2e469858e 100644 --- a/src/components/PostList/PostList.tsx +++ b/src/components/PostList/PostList.tsx @@ -1,3 +1,17 @@ import React from 'react'; +import { Post } from '../../types/Post'; +import { PostInfo } from '../PostInfo'; -export const PostList: React.FC = () => <>Put the list here; +type Props = { + posts: Post[]; +}; + +export const PostList: React.FC = ({ posts }) => { + return ( +
+ {posts.map(post => { + return ; + })} +
+ ); +}; 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 0435cbfa67..5cafe8387a 100644 --- a/src/components/UserInfo/UserInfo.tsx +++ b/src/components/UserInfo/UserInfo.tsx @@ -1,3 +1,19 @@ import React from 'react'; +import { User } from '../../types/User'; +import './UserInfo.scss'; -export const UserInfo: React.FC = () => <>Put the user here; +type Props = { + user: User | null; +}; + +export const UserInfo: React.FC = ({ user }) => { + return ( +

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

+ ); +}; diff --git a/src/types/Comment..ts b/src/types/Comment..ts new file mode 100644 index 0000000000..14df6303f3 --- /dev/null +++ b/src/types/Comment..ts @@ -0,0 +1,7 @@ +export interface Comment { + postId: number; + id: number; + name: string; + email: string; + body: string; +} diff --git a/src/types/Post.ts b/src/types/Post.ts new file mode 100644 index 0000000000..0fac161d5e --- /dev/null +++ b/src/types/Post.ts @@ -0,0 +1,11 @@ +import { Comment } from './Comment.'; +import { User } from './User'; + +export interface Post { + userId: number; + id: number; + title: string; + body: string; + user: User | null; + comments: Comment[] | null; +} diff --git a/src/types/User.ts b/src/types/User.ts new file mode 100644 index 0000000000..1f6908b55a --- /dev/null +++ b/src/types/User.ts @@ -0,0 +1,6 @@ +export interface User { + id: number; + name: string; + username: string; + email: string; +} From bff9c64b8712ac35a6759905d1fc9c68f4c615f3 Mon Sep 17 00:00:00 2001 From: Dominik Dudek Date: Sun, 19 Jan 2025 23:11:12 +0100 Subject: [PATCH 2/2] Solved typo and changed function names --- src/App.tsx | 2 +- src/components/CommentInfo/CommentInfo.tsx | 2 +- src/components/CommentList/CommentList.tsx | 2 +- src/types/{Comment..ts => Comment.ts} | 0 src/types/Post.ts | 2 +- 5 files changed, 4 insertions(+), 4 deletions(-) rename src/types/{Comment..ts => Comment.ts} (100%) diff --git a/src/App.tsx b/src/App.tsx index 9306cf5d3b..ee07d4aa70 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -6,7 +6,7 @@ import postsFromServer from './api/posts'; import commentsFromServer from './api/comments'; import usersFromServer from './api/users'; import { User } from './types/User'; -import { Comment } from './types/Comment.'; +import { Comment } from './types/Comment'; import { Post } from './types/Post'; import { PostList } from './components/PostList'; diff --git a/src/components/CommentInfo/CommentInfo.tsx b/src/components/CommentInfo/CommentInfo.tsx index 16947399ce..43c9893d82 100644 --- a/src/components/CommentInfo/CommentInfo.tsx +++ b/src/components/CommentInfo/CommentInfo.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { Comment } from '../../types/Comment.'; +import { Comment } from '../../types/Comment'; type Props = { comment: Comment; diff --git a/src/components/CommentList/CommentList.tsx b/src/components/CommentList/CommentList.tsx index cd8c24bcda..0cef8ac4e5 100644 --- a/src/components/CommentList/CommentList.tsx +++ b/src/components/CommentList/CommentList.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { Comment } from '../../types/Comment.'; +import { Comment } from '../../types/Comment'; import { CommentInfo } from '../CommentInfo'; import './CommentList.scss'; diff --git a/src/types/Comment..ts b/src/types/Comment.ts similarity index 100% rename from src/types/Comment..ts rename to src/types/Comment.ts diff --git a/src/types/Post.ts b/src/types/Post.ts index 0fac161d5e..176e47e648 100644 --- a/src/types/Post.ts +++ b/src/types/Post.ts @@ -1,4 +1,4 @@ -import { Comment } from './Comment.'; +import { Comment } from './Comment'; import { User } from './User'; export interface Post {