From cc20e3099083e988d6070a96dd3d3d1b369d6e7e Mon Sep 17 00:00:00 2001 From: Aditya Sankaran Date: Fri, 1 Nov 2019 13:20:34 -0400 Subject: [PATCH] work --- functions/handlers/post.js | 33 +++++++++++++++++---- functions/index.js | 4 ++- twistter-frontend/package-lock.json | 16 ++++++++++ twistter-frontend/package.json | 1 + twistter-frontend/src/Like.js | 17 ++++------- twistter-frontend/src/Quote.js | 45 +++++++++++++++++++++++++++-- 6 files changed, 96 insertions(+), 20 deletions(-) diff --git a/functions/handlers/post.js b/functions/handlers/post.js index 816e8a8..7691f6f 100644 --- a/functions/handlers/post.js +++ b/functions/handlers/post.js @@ -50,7 +50,7 @@ exports.getPost = (req, res) => { } exports.likePost = (req, res) => { - let postData; + const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.userData.handle) .where('postId', '==', req.params.postId).limit(1); @@ -59,16 +59,15 @@ exports.likePost = (req, res) => { likeDoc.get() .then((data) => { if (data.empty) { - return admin.firestore().collection('likes').add({ + admin.firestore().collection('likes').add({ postId : req.params.postId, userHandle: req.userData.handle }) .then(() => { - postData.likeCount++; - return postDoc.update({likeCount : postData.likeCount}) + return postDoc.update({likeCount : firebase.firestore.FieldValue.increment(1) }) }) .then(() => { - return res.status(200).json(postData); + return res.status(200).json(postDoc); }) } else { @@ -114,6 +113,30 @@ exports.unlikePost = (re, res) => { } +exports.quotePost = (req, res) => { + + const likeDoc = admin.firestore().collection('posts').where('postId', '==', req.params.postId).limit(1); + + const quotedPost = { + quotingUser : req.userData.handle, + quotedAt: new Date().toISOString(), + body: req.body.body, + + } + admin.firestore().collection('posts').add(quotedPost) + .then((doc) => { + const resPost = quotedPost; + resPost.postId = doc.id; + return res.status(200).json(resPost); + } + ) + .catch((err) => { + console.error(err); + return res.status(500).json({error: 'Something is wrong'}); + }) +} + + exports.getallPostsforFeed = (req, res) => { admin.firestore().collection('posts').get() .then((data) => { diff --git a/functions/index.js b/functions/index.js index 0a8587e..f5ce8d5 100644 --- a/functions/index.js +++ b/functions/index.js @@ -44,7 +44,7 @@ app.get("/user", fbAuth, getAuthenticatedUser); /*------------------------------------------------------------------* * handlers/post.js * *------------------------------------------------------------------*/ -const { getallPostsforUser, putPost, getPost, getallPostsforFeed, likePost, unlikePost +const { getallPostsforUser, putPost, getPost, getallPostsforFeed, likePost, unlikePost, quotePost } = require("./handlers/post"); app.get("/getallPostsforUser", fbAuth, getallPostsforUser); @@ -54,6 +54,8 @@ app.get("/getallPostsforFeed", fbAuth, getallPostsforFeed); app.get("/putPost/:postId", fbAuth, getPost); app.get("/putPost/:postId/like", fbAuth, likePost); app.get("/putPost/:postId/unlike", fbAuth, unlikePost); +app.post("/putPost/:postId/quote", fbAuth, quotePost); + // Adds one post to the database diff --git a/twistter-frontend/package-lock.json b/twistter-frontend/package-lock.json index b112d85..9565447 100644 --- a/twistter-frontend/package-lock.json +++ b/twistter-frontend/package-lock.json @@ -3121,6 +3121,11 @@ "merge": "^1.2.0" } }, + "exenv": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/exenv/-/exenv-1.2.2.tgz", + "integrity": "sha1-KueOhdmJQVhnCwPUe+wfA72Ru50=" + }, "exit-hook": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/exit-hook/-/exit-hook-1.1.1.tgz", @@ -7270,6 +7275,17 @@ "resolved": "https://registry.npmjs.org/react-lifecycles-compat/-/react-lifecycles-compat-3.0.4.tgz", "integrity": "sha512-fBASbA6LnOU9dOU2eW7aQ8xmYBSXUIWr+UmF9b1efZBazGNO+rcXT/icdKnYm2pTwcRylVUYwW7H1PHfLekVzA==" }, + "react-modal": { + "version": "3.11.1", + "resolved": "https://registry.npmjs.org/react-modal/-/react-modal-3.11.1.tgz", + "integrity": "sha512-8uN744Yq0X2lbfSLxsEEc2UV3RjSRb4yDVxRQ1aGzPo86QjNOwhQSukDb8U8kR+636TRTvfMren10fgOjAy9eA==", + "requires": { + "exenv": "^1.2.0", + "prop-types": "^15.5.10", + "react-lifecycles-compat": "^3.0.0", + "warning": "^4.0.3" + } + }, "react-overlays": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/react-overlays/-/react-overlays-1.2.0.tgz", diff --git a/twistter-frontend/package.json b/twistter-frontend/package.json index 4f782a4..8dc9a23 100644 --- a/twistter-frontend/package.json +++ b/twistter-frontend/package.json @@ -18,6 +18,7 @@ "react-bootstrap": "^1.0.0-beta.14", "react-dom": "^16.9.0", "react-dropdown": "^1.6.4", + "react-modal": "^3.11.1", "react-redux": "^7.1.1", "react-router-dom": "^5.1.0", "react-scripts": "0.9.5", diff --git a/twistter-frontend/src/Like.js b/twistter-frontend/src/Like.js index 3dc3aa8..ff53344 100644 --- a/twistter-frontend/src/Like.js +++ b/twistter-frontend/src/Like.js @@ -9,38 +9,33 @@ class Like extends Component { super(props); this.state = { like : false, - Id : null + }; this.handleSubmit = this.handleSubmit.bind(this); } - handleSubmit = post => { + handleSubmit(){ this.setState({ like: !this.state.like }); - axios.get("https://us-central1-twistter-e4649.cloudfunctions.net/api/getallPostsforFeed") - .then((res) => { - const postData = res.data; - this.setState({Id: postData.id}) - - }) + + const postId = "AJdhYAE4diocF8UcrHDq" if(this.state.like == false) { - - axios.get(`/putPost/${this.state.Id}/like`) + axios.get(`/putPost/${postId}/like`) .then((res) => { console.log(res.data); }) } else { - axios.get(`/putPost/${this.state.Id}/unlike`) + axios.get(`/putPost/${postId}/unlike`) .then((res) => { console.log(res.data); }) diff --git a/twistter-frontend/src/Quote.js b/twistter-frontend/src/Quote.js index 81dc7c7..d8377e5 100644 --- a/twistter-frontend/src/Quote.js +++ b/twistter-frontend/src/Quote.js @@ -2,6 +2,8 @@ import React, { Component } from "react"; import { BrowserRouter as Router } from 'react-router-dom'; import Route from 'react-router-dom/Route'; import axios from 'axios'; +import Modal from "react-modal"; + @@ -11,7 +13,7 @@ class Quote extends Component { constructor(props) { super(props); this.state = { - value : '' + post: null }; this.handleSubmit = this.handleSubmit.bind(this); this.handleSubmit2 = this.handleSubmit2.bind(this); @@ -19,11 +21,48 @@ class Quote extends Component { } - handleSubmit() { + handleSubmit2() { + const postId = "AJdhYAE4diocF8UcrHDq"; + const postNoComment = { + body : "" + } + const headers = { + headers: { 'Content-Type': 'application/json'} + } + + axios.post(`/putPost/${postId}/quote`, postNoComment, headers) + .then((res) =>{ + alert('Quoting was successful!') + console.log(res.data); + }) + .catch((err) => { + alert('An error occured.'); + console.error(err); + }) + event.preventDefault(); } - handleSubmit2() { + handleSubmit() { + const postId = "AJdhYAE4diocF8UcrHDq"; + const postComment = { + body : "" + } + const headers = { + headers: { 'Content-Type': 'application/json'} + } + + axios + .post(`/putPost/${postId}/quote`, postComment, headers) + .then((res) =>{ + alert('Quoting was successful!') + console.log(res.data); + }) + .catch((err) => { + alert('An error occured.'); + console.error(err); + }) + event.preventDefault(); } render() {