mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2025-12-16 18:28:47 +00:00
liking and unliking work
This commit is contained in:
parent
903ea35662
commit
1ccc195036
@ -1,6 +1,8 @@
|
|||||||
/* eslint-disable prefer-arrow-callback */
|
/* eslint-disable prefer-arrow-callback */
|
||||||
/* eslint-disable promise/always-return */
|
/* eslint-disable promise/always-return */
|
||||||
const admin = require('firebase-admin');
|
const admin = require('firebase-admin');
|
||||||
|
const { db } = require('../util/admin');
|
||||||
|
|
||||||
|
|
||||||
exports.putPost = (req, res) => {
|
exports.putPost = (req, res) => {
|
||||||
const newPost = {
|
const newPost = {
|
||||||
@ -39,11 +41,11 @@ exports.getallPostsforUser = (req, res) => {
|
|||||||
return res.status(200).json(posts);
|
return res.status(200).json(posts);
|
||||||
})
|
})
|
||||||
.then(function() {
|
.then(function() {
|
||||||
res.status(200).send("Successfully retrieved all user's posts from database.");
|
return res.status(200).json("Successfully retrieved all user's posts from database.");
|
||||||
return;
|
|
||||||
})
|
})
|
||||||
.catch(function(err) {
|
.catch(function(err) {
|
||||||
res.status(500).send("Failed to retrieve user's posts from database.", err);
|
return res.status(500).json("Failed to retrieve user's posts from database.", err);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -58,14 +60,93 @@ exports.getallPosts = (req, res) => {
|
|||||||
return res.status(200).json(posts);
|
return res.status(200).json(posts);
|
||||||
})
|
})
|
||||||
.then(function() {
|
.then(function() {
|
||||||
res.status(200).send("Successfully retrieved every post from database.");
|
return res.status(200).json("Successfully retrieved every post from database.");
|
||||||
return;
|
|
||||||
})
|
})
|
||||||
.catch(function(err) {
|
.catch(function(err) {
|
||||||
res.status(500).send("Failed to retrieve posts from database.", err);
|
return res.status(500).json("Failed to retrieve posts from database.", err);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.likePost = (req, res) => {
|
||||||
|
let postData;
|
||||||
|
const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle)
|
||||||
|
.where('postId', '==', req.params.postId).limit(1);
|
||||||
|
|
||||||
|
const postDoc = db.doc(`/posts/${req.params.postId}`);
|
||||||
|
|
||||||
|
postDoc.get()
|
||||||
|
.then((doc) => {
|
||||||
|
if(doc.exists) {
|
||||||
|
postData = doc.data();
|
||||||
|
return likeDoc.get();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return res.status(404).json({error: 'Post not found'});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then((data) => {
|
||||||
|
if (data.empty) {
|
||||||
|
return admin.firestore().collection('likes').add({
|
||||||
|
postId : req.params.postId,
|
||||||
|
userHandle: req.user.handle
|
||||||
|
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
postData.likeCount++;
|
||||||
|
return postDoc.update({likeCount : postData.likeCount})
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
return res.status(200).json(postData);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
return res.status(500).json({error: 'Something is wrong'});
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.unlikePost = (req, res) => {
|
||||||
|
|
||||||
|
let postData;
|
||||||
|
const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle)
|
||||||
|
.where('postId', '==', req.params.postId).limit(1);
|
||||||
|
|
||||||
|
const postDoc = db.doc(`/posts/${req.params.postId}`);
|
||||||
|
|
||||||
|
postDoc.get()
|
||||||
|
.then((doc) => {
|
||||||
|
if(doc.exists) {
|
||||||
|
postData = doc.data();
|
||||||
|
return likeDoc.get();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return res.status(404).json({error: 'Post not found'});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then((data) => {
|
||||||
|
return db
|
||||||
|
.doc(`/likes/${data.docs[0].id}`)
|
||||||
|
.delete()
|
||||||
|
.then(() => {
|
||||||
|
postData.likeCount--;
|
||||||
|
return postDoc.update({ likeCount: postData.likeCount });
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
res.status(200).json(postData);
|
||||||
|
});
|
||||||
|
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(err);
|
||||||
|
return res.status(500).json({error: 'Something is wrong'});
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
exports.getFilteredPosts = (req, res) => {
|
exports.getFilteredPosts = (req, res) => {
|
||||||
admin.firestore().collection('posts').where('userHandle', '==', 'new user').where('microBlogTopics', '==')
|
admin.firestore().collection('posts').where('userHandle', '==', 'new user').where('microBlogTopics', '==')
|
||||||
};
|
};
|
||||||
|
|||||||
@ -58,7 +58,7 @@ app.get("/getUserHandles", fbAuth, getUserHandles);
|
|||||||
/*------------------------------------------------------------------*
|
/*------------------------------------------------------------------*
|
||||||
* handlers/post.js *
|
* handlers/post.js *
|
||||||
*------------------------------------------------------------------*/
|
*------------------------------------------------------------------*/
|
||||||
const { getallPostsforUser, getallPosts, putPost } = require("./handlers/post");
|
const { getallPostsforUser, getallPosts, putPost, likePost, unlikePost} = require("./handlers/post");
|
||||||
|
|
||||||
app.get("/getallPostsforUser", fbAuth, getallPostsforUser);
|
app.get("/getallPostsforUser", fbAuth, getallPostsforUser);
|
||||||
|
|
||||||
@ -67,6 +67,9 @@ app.get("/getallPosts", getallPosts);
|
|||||||
// Adds one post to the database
|
// Adds one post to the database
|
||||||
app.post("/putPost", fbAuth, putPost);
|
app.post("/putPost", fbAuth, putPost);
|
||||||
|
|
||||||
|
app.get("/like/:postId", fbAuth, likePost);
|
||||||
|
app.get("/unlike/:postId", fbAuth, unlikePost);
|
||||||
|
|
||||||
/*------------------------------------------------------------------*
|
/*------------------------------------------------------------------*
|
||||||
* handlers/topic.js *
|
* handlers/topic.js *
|
||||||
*------------------------------------------------------------------*/
|
*------------------------------------------------------------------*/
|
||||||
|
|||||||
@ -41,5 +41,5 @@
|
|||||||
"last 1 safari version"
|
"last 1 safari version"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"proxy": "https://us-central1-twistter-e4649.cloudfunctions.net/api"
|
"proxy": "http://localhost:5001/twistter-e4649/us-central1/api"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,7 +17,10 @@ import noImage from '../images/no-img.png';
|
|||||||
import Writing_Microblogs from '../Writing_Microblogs';
|
import Writing_Microblogs from '../Writing_Microblogs';
|
||||||
|
|
||||||
class Home extends Component {
|
class Home extends Component {
|
||||||
state = {};
|
state = {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
axios
|
axios
|
||||||
@ -31,8 +34,9 @@ class Home extends Component {
|
|||||||
.catch(err => console.log(err));
|
.catch(err => console.log(err));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
let authenticated = this.props.user.authenticated;
|
let authenticated = this.props.user.authenticated;
|
||||||
|
|
||||||
let postMarkup = this.state.posts ? (
|
let postMarkup = this.state.posts ? (
|
||||||
this.state.posts.map(post =>
|
this.state.posts.map(post =>
|
||||||
@ -53,6 +57,7 @@ class Home extends Component {
|
|||||||
<Typography variant="body2"><b>Topics:</b> {post.microBlogTopics}</Typography>
|
<Typography variant="body2"><b>Topics:</b> {post.microBlogTopics}</Typography>
|
||||||
<br />
|
<br />
|
||||||
<Typography variant="body2" color={"textSecondary"}>Likes {post.likeCount} Comments {post.commentCount}</Typography>
|
<Typography variant="body2" color={"textSecondary"}>Likes {post.likeCount} Comments {post.commentCount}</Typography>
|
||||||
|
<Like microBlog = {post.postId}></Like>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
)
|
)
|
||||||
@ -96,6 +101,7 @@ class Home extends Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const mapStateToProps = (state) => ({
|
const mapStateToProps = (state) => ({
|
||||||
user: state.user
|
user: state.user
|
||||||
})
|
})
|
||||||
@ -104,4 +110,55 @@ Home.propTypes = {
|
|||||||
user: PropTypes.object.isRequired
|
user: PropTypes.object.isRequired
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Like extends Component {
|
||||||
|
|
||||||
|
constructor(props) {
|
||||||
|
super(props)
|
||||||
|
this.state = {
|
||||||
|
like: false
|
||||||
|
}
|
||||||
|
|
||||||
|
this.handleClick = this.handleClick.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
handleClick(){
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
like: !this.state.like
|
||||||
|
});
|
||||||
|
|
||||||
|
if(this.state.like == false)
|
||||||
|
{
|
||||||
|
|
||||||
|
axios.get(`/like/${this.props.microBlog}`)
|
||||||
|
.then((res) => {
|
||||||
|
console.log(res.data);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
axios.get(`/unlike/${this.props.microBlog}`)
|
||||||
|
.then((res) => {
|
||||||
|
console.log(res.data);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
render() {
|
||||||
|
const label = this.state.like ? 'Unlike' : 'Like'
|
||||||
|
return(
|
||||||
|
<div>
|
||||||
|
<button onClick={this.handleClick}>{label}</button>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
export default connect(mapStateToProps)(Home);
|
export default connect(mapStateToProps)(Home);
|
||||||
Loading…
Reference in New Issue
Block a user