saved states of post for each user

This commit is contained in:
Aditya Sankaran 2019-11-29 20:18:51 -05:00
parent 0c9778949a
commit b83eb0c897
4 changed files with 56 additions and 18 deletions

View File

@ -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) => { exports.likePost = (req, res) => {
let postData; let postData;
@ -201,6 +219,7 @@ exports.likePost = (req, res) => {
} }
exports.unlikePost = (req, res) => { exports.unlikePost = (req, res) => {
let postData; let postData;

View File

@ -70,7 +70,7 @@ app.post("/removeSub", fbAuth, removeSub);
/*------------------------------------------------------------------* /*------------------------------------------------------------------*
* handlers/post.js * * 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); app.get("/getallPostsforUser", fbAuth, getallPostsforUser);
@ -81,6 +81,7 @@ app.post("/putPost", fbAuth, putPost);
app.get("/like/:postId", fbAuth, likePost); app.get("/like/:postId", fbAuth, likePost);
app.get("/unlike/:postId", fbAuth, unlikePost); app.get("/unlike/:postId", fbAuth, unlikePost);
app.get("/checkforLikePost/:postId", fbAuth, checkforLikePost);
app.post("/quoteWithPost/:postId", fbAuth, quoteWithPost); app.post("/quoteWithPost/:postId", fbAuth, quoteWithPost);
app.post("/quoteWithoutPost/:postId", fbAuth, quoteWithoutPost); app.post("/quoteWithoutPost/:postId", fbAuth, quoteWithoutPost);

View File

@ -32,9 +32,6 @@ class Home extends Component {
this.setState({ this.setState({
posts: res.data posts: res.data
}) })
this.setState({posts: (this.state.posts).sort((a,b) =>
-a.createdAt.localeCompare(b.createdAt))
})
}) })
.catch(err => console.log(err)); .catch(err => console.log(err));
} }
@ -54,16 +51,15 @@ class Home extends Component {
} }
</Typography> </Typography>
<Typography variant="h7"><b>{post.userHandle}</b></Typography> <Typography variant="h7"><b>{post.userHandle}</b></Typography>
<Typography variant="body2" color={"textSecondary"}>{post.createdAt.substring(0,10) + <Typography variant="body2" color={"textSecondary"}>{post.createdAt}</Typography>
" " + post.createdAt.substring(11,19)}</Typography>
<br /> <br />
<Typography variant="body1"><b>{post.microBlogTitle}</b></Typography> <Typography variant="body1"><b>{post.microBlogTitle}</b></Typography>
<Typography variant="body2">{post.body}</Typography> <Typography variant="body2">{post.body}</Typography>
<br /> <br />
<Typography variant="body2"><b>Topics:</b> {post.microBlogTopics.join("," + " ")}</Typography> <Typography variant="body2"><b>Topics:</b> {post.microBlogTopics}</Typography>
<br /> <br />
<Typography variant="body2" color={"textSecondary"}>Likes {post.likeCount}</Typography> {/* <Typography variant="body2" color={"textSecondary"}>Likes {post.likeCount}</Typography> */}
<Like microBlog = {post.postId}></Like> <Like microBlog = {post.postId} count = {post.likeCount}></Like>
<Quote microblog = {post.postId}></Quote> <Quote microblog = {post.postId}></Quote>
</CardContent> </CardContent>
</Card> </Card>
@ -234,7 +230,7 @@ class Like extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
this.state = { this.state = {
like: false
} }
this.handleClick = this.handleClick.bind(this); this.handleClick = this.handleClick.bind(this);
@ -267,17 +263,43 @@ class Like extends Component {
console.log(err); 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() { render() {
const label = this.state.like ? 'Unlike' : 'Like' const label = this.state.like ? 'Unlike' : 'Like'
return( return(
<div> <div>
<Typography variant="body2" color={"textSecondary"}>Likes {this.props.count}</Typography>
<button onClick={this.handleClick}>{label}</button> <button onClick={this.handleClick}>{label}</button>
</div> </div>
) )
} }
} }
Quote.propTypes = {
user: PropTypes.object.isRequired
}
export default connect(mapStateToProps)(Home); Like.propTypes = {
user: PropTypes.object.isRequired
}
export default connect(mapStateToProps)(Home, Quote, Like);

View File

@ -132,9 +132,6 @@ class user extends Component {
this.setState({ this.setState({
posts: res.data posts: res.data
}) })
this.setState({posts: (this.state.posts).sort((a,b) =>
-a.createdAt.localeCompare(b.createdAt))
})
}) })
.catch(err => console.log(err)); .catch(err => console.log(err));
} }
@ -205,15 +202,14 @@ class user extends Component {
</Typography> </Typography>
<Typography variant="h7"><b>{post.userHandle}</b></Typography> <Typography variant="h7"><b>{post.userHandle}</b></Typography>
<Typography variant="body2" color={"textSecondary"}>{post.createdAt.substring(0,10) + <Typography variant="body2" color={"textSecondary"}>{post.createdAt}</Typography>
" " + post.createdAt.substring(11,19)}</Typography>
<br /> <br />
<Typography variant="body1"> <Typography variant="body1">
<b>{post.microBlogTitle}</b> <b>{post.microBlogTitle}</b>
</Typography> </Typography>
<Typography variant="body2">{post.body}</Typography> <Typography variant="body2">{post.body}</Typography>
<br /> <br />
<Typography variant="body2"><b>Topics:</b> {post.microBlogTopics.join("," + " ")}</Typography> <Typography variant="body2"><b>Topics:</b> {post.microBlogTopics}</Typography>
<br /> <br />
<Typography variant="body2" color={"textSecondary"}>Likes {post.likeCount}</Typography> <Typography variant="body2" color={"textSecondary"}>Likes {post.likeCount}</Typography>
</CardContent> </CardContent>