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
29 changes: 0 additions & 29 deletions src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
129 changes: 34 additions & 95 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,105 +2,44 @@ 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 { Post } from './types/Post';
import { IDGenericFunc } from './types/IDGenericFunc';
import { PostList } from './components/PostList';

function getById<T extends IDGenericFunc>(array: T[], id: number) {
return array.find(element => element.id === id) || null;
}

function getPostList() {
const posts = postsFromServer.map(post => ({
...post,
user: getById<User>(usersFromServer, post.userId),
comments: [],
}));

commentsFromServer.forEach(comment => {
const post = getById<Post>(posts, comment.postId);
const resultComment = {
...comment,
post,
};

post?.comments.push(resultComment);
});

return posts;
}

const postList: Post[] = getPostList();

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>
</div>
<PostList posts={postList} />
</section>
);
26 changes: 24 additions & 2 deletions src/components/CommentInfo/CommentInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
import React from 'react';
type Props = {
comment: {
name: string;
email: string;
body: string;
};
};

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

{' by '}

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

<div className="CommentInfo__body">{body}</div>
</div>
);
};
8 changes: 7 additions & 1 deletion src/components/CommentList/CommentList.scss
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
// add styles here
.CommentList {
display: flex;
flex-direction: column;
gap: 0.7em;
background-color: #eee;
padding: 1em;
}
17 changes: 16 additions & 1 deletion src/components/CommentList/CommentList.tsx
Original file line number Diff line number Diff line change
@@ -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<Props> = ({ comments }) => {
return (
<div className="CommentList">
{comments.map(comment => (
<CommentInfo comment={comment} key={comment.id} />
))}
</div>
);
};
17 changes: 16 additions & 1 deletion src/components/PostInfo/PostInfo.scss
Original file line number Diff line number Diff line change
@@ -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;
}
}
37 changes: 35 additions & 2 deletions src/components/PostInfo/PostInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
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 = ({ post: { title, body, user, comments } }: Props) => {
const commentsElement = comments.length ? (
<CommentList comments={comments} />
) : (
<b data-cy="NoCommentsMessage">No comments yet</b>
);

return (
<div className="PostInfo">
<div className="PostInfo__header">
<h3 className="PostInfo__title">{title}</h3>

<p>
{' Posted by '}

{user && <UserInfo user={user} />}
</p>
</div>

<p className="PostInfo__body">{body}</p>

<hr />

{commentsElement}
</div>
);
};
16 changes: 15 additions & 1 deletion src/components/PostList/PostList.tsx
Original file line number Diff line number Diff line change
@@ -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<Props> = ({ posts }) => {
return (
<div className="PostList">
{posts.map(post => (
<PostInfo post={post} key={post.id} />
))}
</div>
);
};
4 changes: 3 additions & 1 deletion src/components/UserInfo/UserInfo.scss
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// add styles here
.UserInfo {
font-weight: bold;
}
14 changes: 13 additions & 1 deletion src/components/UserInfo/UserInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
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;
};

export const UserInfo: React.FC<Props> = ({ user: { email, name } }) => {
return (
<a className="UserInfo" href={`mailto:${email}`}>
{name}
</a>
);
};
7 changes: 7 additions & 0 deletions src/types/Comment.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;
}
3 changes: 3 additions & 0 deletions src/types/IDGenericFunc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IDGenericFunc {
id: number;
}
11 changes: 11 additions & 0 deletions src/types/Post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { User } from './User';
import { Comment } from './Comment';

export interface Post {
userId: number;
id: number;
title: string;
body: string;
user: User | null;
comments: Comment[];
}
6 changes: 6 additions & 0 deletions src/types/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface User {
id: number;
name: string;
username: string;
email: string;
}