diff --git a/functions/handlers/post.js b/functions/handlers/post.js
index 3c6cb9e..2f5ce4f 100644
--- a/functions/handlers/post.js
+++ b/functions/handlers/post.js
@@ -136,25 +136,23 @@ exports.getOtherUsersPosts = (req, res) => {
};
exports.quoteWithPost = (req, res) => {
- let quoteData;
- const quoteDoc = admin
- .firestore()
- .collection("quote")
- .where("userHandle", "==", req.user.handle)
- .where("postId", "==", req.params.postId)
- .limit(1);
+ let quoteData;
+ const quoteDoc = admin.firestore().collection('quote').
+ where('userHandle', '==', req.user.handle).
+ where('quoteId', '==', req.params.postId).limit(1);
- const postDoc = db.doc(`/posts/${req.params.postId}`);
+ const postDoc = db.doc(`/posts/${req.params.postId}`);
- postDoc
- .get()
- .then(doc => {
- if (doc.exists) {
- quoteData = doc.data();
- return quoteDoc.get();
- } else {
- return res.status(404).json({ error: "Post not found" });
- }
+ postDoc.get()
+ .then((doc) => {
+ if(doc.exists) {
+ quoteData = doc.data();
+ return quoteDoc.get();
+ }
+ else
+ {
+ return res.status(404).json({error: 'Post not found'});
+ }
})
.then(data => {
if (data.empty) {
@@ -202,25 +200,23 @@ exports.quoteWithPost = (req, res) => {
};
exports.quoteWithoutPost = (req, res) => {
- let quoteData;
- const quoteDoc = admin
- .firestore()
- .collection("quote")
- .where("userHandle", "==", req.user.handle)
- .where("postId", "==", req.params.postId)
- .limit(1);
+ let quoteData;
+ const quoteDoc = admin.firestore().collection('quote').
+ where('userHandle', '==', req.user.handle).
+ where('quoteId', '==', req.params.postId).limit(1);
- const postDoc = db.doc(`/posts/${req.params.postId}`);
+ const postDoc = db.doc(`/posts/${req.params.postId}`);
- postDoc
- .get()
- .then(doc => {
- if (doc.exists) {
- quoteData = doc.data();
- return quoteDoc.get();
- } else {
- return res.status(404).json({ error: "Post not found" });
- }
+ postDoc.get()
+ .then((doc) => {
+ if(doc.exists) {
+ quoteData = doc.data();
+ return quoteDoc.get();
+ }
+ else
+ {
+ return res.status(404).json({error: 'Post not found'});
+ }
})
.then(data => {
if (data.empty) {
@@ -292,94 +288,186 @@ exports.checkforLikePost = (req, res) => {
};
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}`);
+ const postId = req.params.postId;
+ let likedPostDoc;
+ db.doc(`/users/${req.userData.handle}`)
+ .get()
+ .then((userDoc) => {
+ let likes = userDoc.data().likes;
+ if (likes === undefined || likes === null) {
+ likes = [];
+ }
- postDoc
- .get()
- .then(doc => {
- if (doc.exists) {
- postData = doc.data();
- return likeDoc.get();
- } else {
- return res.status(404).json({ error: "Post not found" });
- }
- })
+ if (likes.includes(postId)) {
+ return res.status(400).json({error: "This user has already liked this post"});
+ }
- .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" });
- return res.status(500).json({ error: err });
- });
-};
+ likes.push(postId);
-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 });
+ return userDoc.ref.update({likes})
})
.then(() => {
- res.status(200).json(postData);
- });
- })
- .catch(err => {
- console.error(err);
- return res.status(500).json({ error: "Something is wrong" });
- });
-};
+ return db.doc(`/posts/${postId}`).get()
+
+ })
+ .then((postDoc) => {
+ let postData = postDoc.data();
+ postData.likeCount++;
+ likedPostDoc = postData;
+ return postDoc.ref.update({likeCount : postData.likeCount})
+ })
+ .then(() => {
+ return res.status(201).json(likedPostDoc);
+ })
+ .catch((err) => {
+ console.log(err);
+ return res.status(500).json({error: err});
+ })
+
+ // 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) => {
+
+ const postId = req.params.postId;
+ let likedPostDoc;
+ db.doc(`/users/${req.userData.handle}`)
+ .get()
+ .then((userDoc) => {
+ let likes = userDoc.data().likes;
+ if (likes === undefined || likes === null) {
+ likes = [];
+ }
+
+ if (!likes.includes(postId)) {
+ return res.status(400).json({error: "This user hasn't liked this post yet"});
+ }
+
+ let i;
+ for (i = 0; i < likes.length; i++) {
+ if (likes[i] === postId) {
+ likes.splice(i, 1);
+ }
+ }
+
+ return userDoc.ref.update({likes})
+ })
+ .then(() => {
+ return db.doc(`/posts/${postId}`).get()
+
+ })
+ .then((postDoc) => {
+ let postData = postDoc.data();
+ postData.likeCount--;
+ likedPostDoc = postData;
+ return postDoc.ref.update({likeCount : postData.likeCount})
+ })
+ .then(() => {
+ return res.status(201).json(likedPostDoc);
+ })
+ .catch((err) => {
+ console.log(err);
+ return res.status(500).json({error: err});
+ })
+
+ // 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.getLikes = (req, res) => {
+ db.doc(`/users/${req.userData.handle}`)
+ .get()
+ .then((doc) => {
+ let likes = doc.data().likes;
+ if (likes === undefined || likes === null) {
+ likes = [];
+ }
+ return res.status(200).json({likes});
+ })
+ .catch((err) => {
+ console.log(err);
+ return res.status(500).json({error: err});
+ })
+}
exports.getFilteredPosts = (req, res) => {
+
admin
.firestore()
.collection("posts")
.where("userHandle", "==", "new user")
.where("microBlogTopics", "==");
};
+
diff --git a/functions/index.js b/functions/index.js
index 4f7c3fb..d64c9c0 100644
--- a/functions/index.js
+++ b/functions/index.js
@@ -81,17 +81,9 @@ app.post("/removeSub", fbAuth, removeSub);
/*------------------------------------------------------------------*
* handlers/post.js *
*------------------------------------------------------------------*/
-const {
- getallPostsforUser,
- getallPosts,
- putPost,
- likePost,
- unlikePost,
- quoteWithPost,
- quoteWithoutPost,
- checkforLikePost,
- getOtherUsersPosts
-} = require("./handlers/post");
+
+const { getallPostsforUser, getallPosts, putPost, likePost, unlikePost, getLikes, quoteWithPost, quoteWithoutPost, checkforLikePost} = require("./handlers/post");
+
app.get("/getallPostsforUser", fbAuth, getallPostsforUser);
@@ -100,6 +92,7 @@ app.get("/getallPosts", getallPosts);
// Adds one post to the database
app.post("/putPost", fbAuth, putPost);
+app.get("/likes", fbAuth, getLikes);
app.get("/like/:postId", fbAuth, likePost);
app.get("/unlike/:postId", fbAuth, unlikePost);
app.get("/checkforLikePost/:postId", fbAuth, checkforLikePost);
diff --git a/twistter-frontend/src/pages/Home.js b/twistter-frontend/src/pages/Home.js
index 38610f9..69d4828 100644
--- a/twistter-frontend/src/pages/Home.js
+++ b/twistter-frontend/src/pages/Home.js
@@ -5,10 +5,14 @@ import { connect } from "react-redux";
import axios from "axios";
// Material UI and React Router
+
import CircularProgress from '@material-ui/core/CircularProgress';
+import Button from '@material-ui/core/Button';
import Grid from "@material-ui/core/Grid";
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
+import TextField from '@material-ui/core/TextField';
+
import Typography from "@material-ui/core/Typography";
import withStyles from '@material-ui/styles/withStyles';
@@ -19,6 +23,9 @@ import noImage from '../images/no-img.png';
import Writing_Microblogs from '../Writing_Microblogs';
import ReactModal from 'react-modal';
+// Redux
+import { likePost, unlikePost, getLikes } from '../redux/actions/userActions';
+
const styles = {
card: {
@@ -28,7 +35,7 @@ const styles = {
class Home extends Component {
state = {
-
+ likes: []
};
@@ -42,6 +49,34 @@ class Home extends Component {
});
})
.catch(err => console.log(err));
+
+ this.props.getLikes();
+ }
+
+ componentWillReceiveProps(nextProps) {
+ this.setState({
+ likes: nextProps.user.likes
+ })
+ }
+
+ handleClickLikeButton = (event) => {
+ // Need the ternary if statement because the user can click on the text or body of the
+ // Button and they are two different html elements
+ let postId = event.target.dataset.key ? event.target.dataset.key : event.target.parentNode.dataset.key;
+ console.log(postId)
+
+ let doc = document.getElementById(postId);
+ // console.log(postId);
+ if (this.state.likes.includes(postId)) {
+ this.props.unlikePost(postId, this.state.likes)
+ doc.dataset.likes--;
+ } else {
+ this.props.likePost(postId, this.state.likes)
+ doc.dataset.likes++;
+ }
+
+ doc.innerHTML = "Likes " + doc.dataset.likes;
+
}
formatDate(dateString) {
@@ -50,6 +85,7 @@ class Home extends Component {
}
render() {
+
const { UI:{ loading } } = this.props;
let authenticated = this.props.user.authenticated;
let {classes} = this.props;
@@ -79,9 +115,21 @@ class Home extends Component {
- {/*
+
+
+ {/* */}
+
)
@@ -207,14 +255,14 @@ class Quote extends Component {
render() {
return (