From b1faacd312f0d821a291dcc56ef3b16a327a15ba Mon Sep 17 00:00:00 2001 From: Aditya Sankaran Date: Sun, 29 Sep 2019 19:07:35 -0400 Subject: [PATCH 1/3] backend and frontend connect in progress --- functions/handlers/post.js | 14 +++++++++----- twistter-frontend/src/Writing_Microblogs.js | 9 ++++++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/functions/handlers/post.js b/functions/handlers/post.js index 71b97eb..3a27c1a 100644 --- a/functions/handlers/post.js +++ b/functions/handlers/post.js @@ -1,3 +1,4 @@ +const admin = require('firebase-admin'); /* eslint-disable promise/always-return */ exports.putPost = (req, res) => { if (req.body.body.trim() === '') { @@ -6,18 +7,21 @@ exports.putPost = (req, res) => { const newPost = { body: req.body.body, - userHandle: req.user.handle, - userImage: req.user.imageUrl, + userHandle: req.body.userHandle, + userImage: req.body.userImage, + title: req.body.title, createdAt: new Date().toISOString(), likeCount: 0, - commentCount: 0 + commentCount: 0, + }; - db.collection('post').add(newPost) - .then((doc) => { + admin.firestore().collection('posts').add(newPost) + .then((doc) => { const resPost = newPost; resPost.postId = doc.id; res.json(resPost); + }) .catch((err) => { res.status(500).json({ error: 'something is wrong'}); diff --git a/twistter-frontend/src/Writing_Microblogs.js b/twistter-frontend/src/Writing_Microblogs.js index eca41d4..4769a9a 100644 --- a/twistter-frontend/src/Writing_Microblogs.js +++ b/twistter-frontend/src/Writing_Microblogs.js @@ -1,6 +1,7 @@ import React, { Component } from "react"; import { BrowserRouter as Router } from 'react-router-dom'; import Route from 'react-router-dom/Route'; +import axios from 'axios'; class Writing_Microblogs extends Component { @@ -25,7 +26,13 @@ class Writing_Microblogs extends Component { } handleSubmit(event) { - alert('A title for the microblog was inputted: ' + this.state.title + '\nA microblog was posted: ' + this.state.value); + // alert('A title for the microblog was inputted: ' + this.state.title + '\nA microblog was posted: ' + this.state.value); + const response = await axios.post( + 'http://localhost:5001/twistter-e4649/us-central1/api/putPost', + { }, + { headers: { 'Content-Type': 'application/json'} } + ) + console.log(response.data); event.preventDefault(); } From 90442fe3cd885b5b7e48fc2ba699f3c4642ab3b3 Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Sun, 29 Sep 2019 23:59:36 -0400 Subject: [PATCH 2/3] Fixing 'firebase deploy' errors and warnings --- functions/handlers/post.js | 4 ++-- functions/handlers/users.js | 6 +++++- functions/util/validator.js | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/functions/handlers/post.js b/functions/handlers/post.js index 71b97eb..e2d195b 100644 --- a/functions/handlers/post.js +++ b/functions/handlers/post.js @@ -17,11 +17,11 @@ exports.putPost = (req, res) => { .then((doc) => { const resPost = newPost; resPost.postId = doc.id; - res.json(resPost); + return res.status(200).json(resPost); }) .catch((err) => { - res.status(500).json({ error: 'something is wrong'}); console.error(err); + return res.status(500).json({ error: 'something is wrong'}); }); }; diff --git a/functions/handlers/users.js b/functions/handlers/users.js index 240bfdd..c165ae0 100644 --- a/functions/handlers/users.js +++ b/functions/handlers/users.js @@ -9,6 +9,10 @@ exports.getProfileInfo = (req, res) => { db.collection('users').doc(req.user.handle).get() .then((data) => { return res.status(200).json(data.data()); + }) + .catch((err) => { + console.error(err); + return res.status(500).json(err); }); }; @@ -41,7 +45,7 @@ exports.updateProfileInfo = (req, res) => { exports.getUserDetails = (req, res) => { let userData = {}; - db.doc('/users/${req.params.handle}').get().then((doc) => { + db.doc(`/users/${req.params.handle}`).get().then((doc) => { if (doc.exists) { userData.user = doc.data(); return db.collection('post').where('userHandle', '==', req.params.handle) diff --git a/functions/util/validator.js b/functions/util/validator.js index ca29c6c..3ab6226 100644 --- a/functions/util/validator.js +++ b/functions/util/validator.js @@ -4,7 +4,7 @@ const isEmpty = (str) => { }; const isEmail = (str) => { - const emailRegEx = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + const emailRegEx = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; if (str.match(emailRegEx)) return true; else return false; } From 55dc62e57e9907993a9725ca1c9b07245a2dc896 Mon Sep 17 00:00:00 2001 From: Aditya Sankaran Date: Mon, 30 Sep 2019 18:06:23 -0400 Subject: [PATCH 3/3] connected backend to frontend for writing microblogs --- functions/handlers/post.js | 6 ++---- functions/index.js | 3 ++- twistter-frontend/src/App.js | 2 ++ twistter-frontend/src/Userline.js | 24 +++++++++++++++++++++ twistter-frontend/src/Writing_Microblogs.js | 11 ++++++++-- 5 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 twistter-frontend/src/Userline.js diff --git a/functions/handlers/post.js b/functions/handlers/post.js index 3a27c1a..68ac37c 100644 --- a/functions/handlers/post.js +++ b/functions/handlers/post.js @@ -1,15 +1,13 @@ const admin = require('firebase-admin'); /* eslint-disable promise/always-return */ exports.putPost = (req, res) => { - if (req.body.body.trim() === '') { - return res.status(400).json({ body: 'Body must not be empty!'}); - } + const newPost = { body: req.body.body, userHandle: req.body.userHandle, userImage: req.body.userImage, - title: req.body.title, + microBlogTitle: req.body.microBlogTitle, createdAt: new Date().toISOString(), likeCount: 0, commentCount: 0, diff --git a/functions/index.js b/functions/index.js index 7d0498a..b4dbbcd 100644 --- a/functions/index.js +++ b/functions/index.js @@ -19,7 +19,8 @@ const { // post routes -app.post('/putPost', FBauth, putPost); +//app.post('/putPost', FBauth, putPost); +app.post('/putPost', putPost); // users routes app.get('/getUser/:handle', getUserDetails); diff --git a/twistter-frontend/src/App.js b/twistter-frontend/src/App.js index 3b0a077..1fea229 100644 --- a/twistter-frontend/src/App.js +++ b/twistter-frontend/src/App.js @@ -12,6 +12,7 @@ import register from './Register.js'; import login from './Login.js'; import user from './pages/user'; import writeMicroblog from './Writing_Microblogs.js'; +import userLine from './Userline.js'; class App extends Component { render() { @@ -25,6 +26,7 @@ class App extends Component { + diff --git a/twistter-frontend/src/Userline.js b/twistter-frontend/src/Userline.js new file mode 100644 index 0000000..af4ba7a --- /dev/null +++ b/twistter-frontend/src/Userline.js @@ -0,0 +1,24 @@ +import React, { Component } from "react"; +import { BrowserRouter as Router } from 'react-router-dom'; +import Route from 'react-router-dom/Route'; +import axios from 'axios'; + +class Userline extends Component { + + constructor(props) + { + super(props); + this.state = { + + } + } + + render() { + return ( +

Hi

+ ) + } + +} + +export default Userline; \ No newline at end of file diff --git a/twistter-frontend/src/Writing_Microblogs.js b/twistter-frontend/src/Writing_Microblogs.js index 4769a9a..d9cbb1e 100644 --- a/twistter-frontend/src/Writing_Microblogs.js +++ b/twistter-frontend/src/Writing_Microblogs.js @@ -27,12 +27,19 @@ class Writing_Microblogs extends Component { handleSubmit(event) { // alert('A title for the microblog was inputted: ' + this.state.title + '\nA microblog was posted: ' + this.state.value); - const response = await axios.post( + + const response = axios.post( 'http://localhost:5001/twistter-e4649/us-central1/api/putPost', - { }, + { body: this.state.value, + userHandle: "new user", + userImage: "bing-url", + microBlogTitle: this.state.title + + }, { headers: { 'Content-Type': 'application/json'} } ) console.log(response.data); + event.preventDefault(); }