diff --git a/functions/handlers/post.js b/functions/handlers/post.js index 2ccdc8e..8ef48c9 100644 --- a/functions/handlers/post.js +++ b/functions/handlers/post.js @@ -160,6 +160,24 @@ exports.quoteWithoutPost = (req, res) => { } +exports.checkforLikePost = (req, res) => { + const likedPostDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle) + .where('postId', '==', req.params.postId).limit(1); + let result; + + likedPostDoc.get() + .then((data) => { + if (data.empty) { + result = false; + return res.status(200).json(result); + } + else + { + result = true; + return res.status(200).json(result); + } + }) +} exports.likePost = (req, res) => { let postData; @@ -201,6 +219,7 @@ exports.likePost = (req, res) => { } + exports.unlikePost = (req, res) => { let postData; diff --git a/functions/index.js b/functions/index.js index f4ab762..ee203eb 100644 --- a/functions/index.js +++ b/functions/index.js @@ -70,7 +70,7 @@ app.post("/removeSub", fbAuth, removeSub); /*------------------------------------------------------------------* * handlers/post.js * *------------------------------------------------------------------*/ -const { getallPostsforUser, getallPosts, putPost, likePost, unlikePost, quoteWithPost, quoteWithoutPost} = require("./handlers/post"); +const { getallPostsforUser, getallPosts, putPost, likePost, unlikePost, quoteWithPost, quoteWithoutPost, checkforLikePost} = require("./handlers/post"); app.get("/getallPostsforUser", fbAuth, getallPostsforUser); @@ -81,6 +81,7 @@ app.post("/putPost", fbAuth, putPost); app.get("/like/:postId", fbAuth, likePost); app.get("/unlike/:postId", fbAuth, unlikePost); +app.get("/checkforLikePost/:postId", fbAuth, checkforLikePost); app.post("/quoteWithPost/:postId", fbAuth, quoteWithPost); app.post("/quoteWithoutPost/:postId", fbAuth, quoteWithoutPost); diff --git a/twistter-frontend/src/pages/Home.js b/twistter-frontend/src/pages/Home.js index fd288c0..22f6ce2 100644 --- a/twistter-frontend/src/pages/Home.js +++ b/twistter-frontend/src/pages/Home.js @@ -32,9 +32,6 @@ class Home extends Component { this.setState({ posts: res.data }) - this.setState({posts: (this.state.posts).sort((a,b) => - -a.createdAt.localeCompare(b.createdAt)) - }) }) .catch(err => console.log(err)); } @@ -54,16 +51,15 @@ class Home extends Component { } {post.userHandle} - {post.createdAt.substring(0,10) + - " " + post.createdAt.substring(11,19)} + {post.createdAt}
{post.microBlogTitle} {post.body}
- Topics: {post.microBlogTopics.join("," + " ")} + Topics: {post.microBlogTopics}
- Likes {post.likeCount} - + {/* Likes {post.likeCount} */} + @@ -234,7 +230,7 @@ class Like extends Component { constructor(props) { super(props) this.state = { - like: false + } this.handleClick = this.handleClick.bind(this); @@ -267,17 +263,43 @@ class Like extends Component { console.log(err); }) } - + + } + + componentDidMount() { + axios.get(`/checkforLikePost/${this.props.microBlog}`) + .then((res) => { + this.setState({ + like: res.data + }) + console.log(res.data); + }) + .catch((err) => { + console.log(err) + }) + } + render() { + const label = this.state.like ? 'Unlike' : 'Like' return(
+ Likes {this.props.count}
) } } +Quote.propTypes = { + user: PropTypes.object.isRequired +} -export default connect(mapStateToProps)(Home); \ No newline at end of file +Like.propTypes = { + user: PropTypes.object.isRequired +} + + + +export default connect(mapStateToProps)(Home, Quote, Like); \ No newline at end of file diff --git a/twistter-frontend/src/pages/user.js b/twistter-frontend/src/pages/user.js index 0a11258..7e6f14a 100644 --- a/twistter-frontend/src/pages/user.js +++ b/twistter-frontend/src/pages/user.js @@ -132,9 +132,6 @@ class user extends Component { this.setState({ posts: res.data }) - this.setState({posts: (this.state.posts).sort((a,b) => - -a.createdAt.localeCompare(b.createdAt)) - }) }) .catch(err => console.log(err)); } @@ -205,15 +202,14 @@ class user extends Component { {post.userHandle} - {post.createdAt.substring(0,10) + - " " + post.createdAt.substring(11,19)} + {post.createdAt}
{post.microBlogTitle} {post.body}
- Topics: {post.microBlogTopics.join("," + " ")} + Topics: {post.microBlogTopics}
Likes {post.likeCount}