Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first commit #529

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
17 changes: 16 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
*/

// Import the state hook
import React from 'react';
import React, { useState } from 'react';
// Import the Posts (plural!) and SearchBar components, since they are used inside App component
import Posts from './components/Posts/Posts'
import SearchBar from './components/SearchBar/SearchBar'
// Import the dummyData
import dummyData from './dummy-data'
import './App.css';

const App = () => {
// Create a state called `posts` to hold the array of post objects, **initializing to dummyData**.
// This state is the source of truth for the data inside the app. You won't be needing dummyData anymore.
// To make the search bar work (which is stretch) we'd need another state to hold the search term.
const [posts, setPosts] = useState(dummyData);

const likePost = postId => {
/*
Expand All @@ -27,11 +31,22 @@ const App = () => {
- if the `id` of the post matches `postId`, return a new post object with the desired values (use the spread operator).
- otherwise just return the post object unchanged.
*/
const updatedPosts = posts.map(post => {
if (post.id === postId){
return { ...post, likes: post.likes + 1}
}else {
return post;
}
})

setPosts(updatedPosts);
};

return (
<div className='App'>
{/* Add SearchBar and Posts here to render them */}
<SearchBar />
<Posts posts={posts} likePost={likePost} />
{/* Check the implementation of each component, to see what props they require, if any! */}
</div>
);
Expand Down
3 changes: 3 additions & 0 deletions src/components/Comments/Comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const Comments = props => {
return (
<div>
{/* map through the comments prop and render a Comment for every piece of data */}
{comments.map(comment => {
return <Comment comment={comment} key={comment.id}/>
})}
</div>
);
};
Expand Down
4 changes: 2 additions & 2 deletions src/components/Posts/LikeSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ const LikeSection = props => {
className='like-section'
key='likes-icons-container'
>
<div className='like-section-wrapper'>
<div className='like-section-wrapper' onClick={likePost}>
<FontAwesomeIcon icon={faHeart} />
</div>
<div className='like-section-wrapper'>
<FontAwesomeIcon icon={faComment} />
</div>
</div>
<p className='like-number'>100 likes</p>
<p className='like-number'>{numberOfLikes}</p>
</div>
);
};
Expand Down
4 changes: 2 additions & 2 deletions src/components/Posts/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const Post = props => {
/>
</div>
{/* Is LikeSection getting all the props it needs to work correctly? */}
<LikeSection likePost={() => likePost(post.id)} />
<LikeSection likePost={() => likePost(post.id)} numberOfLikes={post.likes}/>
{/* Comments also wants its props! */}
<Comments />
<Comments comments={post.comments}/>
</div>
);
};
Expand Down
3 changes: 3 additions & 0 deletions src/components/Posts/Posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const Posts = (props) => {
<div className='posts-container-wrapper'>
{/* Map through the posts array returning a Post component at each iteration */}
{/* Check the implementation of Post to see what props it requires! */}
{ posts.map(post => {
return <Post post={posts} likePost={likePost} key={post.id}/>
})}
</div>
);
};
Expand Down