Skip to content

Commit 9c8765e

Browse files
committed
Add last upvoted function to PostList
1 parent 865b1be commit 9c8765e

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/components/Post.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import React, { useState } from "react";
22
import PropTypes from "prop-types";
33
import "../styles/post.css";
44

5-
const Post = ({postData}) => {
5+
const Post = ({postData, handleUpvote}) => {
66
const {title, author, date, isPublished, body, tags} = postData;
77
const [count, setCount] = useState(0);
88

9-
const handleClick = () => {
9+
const handleClick = (event) => {
1010
setCount(prev => prev + 1)
11+
handleUpvote(event.target.value)
1112
}
1213

1314
return (
@@ -18,7 +19,7 @@ const Post = ({postData}) => {
1819
</div>
1920
<div className="post-counter">
2021
<span>Upvotes: {count}</span>
21-
<button onClick={handleClick} type="button">Upvote this</button>
22+
<button onClick={handleClick} value={title} type="button">Upvote this</button>
2223
</div>
2324
<div className="post-author">Author: {author}</div>
2425
<div className="post-date">Published: {date}</div>
@@ -40,7 +41,8 @@ Post.propTypes = {
4041
isPublished: PropTypes.bool,
4142
tags: PropTypes.arrayOf(PropTypes.string),
4243
title: PropTypes.string,
43-
}).isRequired
44+
}).isRequired,
45+
handleUpvote: PropTypes.func.isRequired
4446
}
4547

4648
export default Post;

src/components/Postlist.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
import React from "react";
1+
import React, { useState } from "react";
22
import PropTypes from "prop-types";
33

44
import "../styles/postlist.css"
55

66
import Post from "./Post";
77

88
const PostList = ({ posts }) => {
9+
const [lastUpvoted, setLastUpvoted] = useState("");
10+
11+
const handleUpvote = (title) => {
12+
setLastUpvoted(title);
13+
}
14+
915
return (
1016
<div className="postlist">
17+
{lastUpvoted && <div className="postlist__last-upvoted">Last upvoted post: {lastUpvoted}</div>}
1118
{posts.map((post) => (
12-
<Post key={post.id} postData={post} />
19+
<Post key={post.id} postData={post} handleUpvote={handleUpvote} />
1320
))}
1421
</div>
1522
);

0 commit comments

Comments
 (0)