From 13a140175989a26ea835d2de478cdb757cf5321c Mon Sep 17 00:00:00 2001 From: Leon Liang Date: Sun, 27 Oct 2019 02:53:12 -0400 Subject: [PATCH 01/79] Reformat code --- functions/handlers/topic.js | 80 ++++---- functions/index.js | 9 +- twistter-frontend/src/App.js | 56 +++--- twistter-frontend/src/Writing_Microblogs.js | 209 +++++++++++--------- twistter-frontend/src/pages/user.js | 128 ++++++------ 5 files changed, 258 insertions(+), 224 deletions(-) diff --git a/functions/handlers/topic.js b/functions/handlers/topic.js index 4d3dc3d..c4d7b10 100644 --- a/functions/handlers/topic.js +++ b/functions/handlers/topic.js @@ -1,52 +1,60 @@ /* eslint-disable promise/always-return */ const { admin, db } = require("../util/admin"); exports.putTopic = (req, res) => { + const newTopic = { + topic: req.body.topic, + topicId: null + }; - const newTopic = { - topic: req.body.topic - }; - - admin.firestore().collection('topics').add(newTopic) - .then((doc) => { - const resTopic = newTopic; - newTopic.topicId = doc.id; - return res.status(200).json(resTopic); + admin + .firestore() + .collection("topics") + .add(newTopic) + .then(doc => { + const resTopic = newTopic; + newTopic.topicId = doc.id; + return res.status(200).json(resTopic); }) - .catch((err) => { - console.error(err); - return res.status(500).json({ error: 'something is wrong'}); + .catch(err => { + console.error(err); + return res.status(500).json({ error: "something is wrong" }); }); }; exports.getAllTopics = (req, res) => { - admin.firestore().collection('topics').get() - .then((data) => { - let topics = []; - data.forEach(function(doc) { - topics.push(doc.data()); - }); - return res.status(200).json(topics); - }) - .catch((err) => { - console.error(err); - return res.status(500).json({error: 'Failed to fetch all topics.'}) + admin + .firestore() + .collection("topics") + .get() + .then(data => { + let topics = []; + data.forEach(function(doc) { + topics.push(doc.data()); + }); + return res.status(200).json(topics); }) + .catch(err => { + console.error(err); + return res.status(500).json({ error: "Failed to fetch all topics." }); + }); }; exports.deleteTopic = (req, res) => { - const topic = db.doc(`/topics/${req.params.topicId}`); - topic.get().then((doc) => { - if (!doc.exists) { - return res.status(404).json({error: 'Topic not found'}); - } else { - return topic.delete(); - } + const topic = db.doc(`/topics/${req.params.topicId}`); + topic + .get() + .then(doc => { + if (!doc.exists) { + return res.status(404).json({ error: "Topic not found" }); + } else { + return topic.delete(); + } }) .then(() => { - res.json({ message: 'Topic successfully deleted!'}); + res.json({ message: "Topic successfully deleted!" }); }) - .catch((err) => { - console.error(err); - return res.status(500).json({error: 'Failed to delete topic.'}) - }) -} \ No newline at end of file + .catch(err => { + console.error(err); + return res.status(500).json({ error: "Failed to delete topic." }); + }); +}; diff --git a/functions/index.js b/functions/index.js index 3be20a4..981eabd 100644 --- a/functions/index.js +++ b/functions/index.js @@ -44,8 +44,7 @@ app.get("/user", fbAuth, getAuthenticatedUser); /*------------------------------------------------------------------* * handlers/post.js * *------------------------------------------------------------------*/ -const { getallPostsforUser, putPost -} = require("./handlers/post"); +const { getallPostsforUser, putPost } = require("./handlers/post"); app.get("/getallPostsforUser", getallPostsforUser); @@ -55,11 +54,7 @@ app.post("/putPost", fbAuth, putPost); /*------------------------------------------------------------------* * handlers/topic.js * *------------------------------------------------------------------*/ -const { - putTopic, - getAllTopics, - deleteTopic -} = require("./handlers/topic"); +const { putTopic, getAllTopics, deleteTopic } = require("./handlers/topic"); // add topic to database app.post("/putTopic", fbAuth, putTopic); diff --git a/twistter-frontend/src/App.js b/twistter-frontend/src/App.js index c413374..992347d 100644 --- a/twistter-frontend/src/App.js +++ b/twistter-frontend/src/App.js @@ -10,27 +10,27 @@ import jwtDecode from "jwt-decode"; // Redux import { Provider } from "react-redux"; import store from "./redux/store"; -import MuiThemeProvider from '@material-ui/core/styles/MuiThemeProvider'; -import createMuiTheme from '@material-ui/core/styles/createMuiTheme'; -import themeObject from './util/theme'; -import { SET_AUTHENTICATED } from './redux/types'; -import { logoutUser, getUserData } from './redux/actions/userActions'; +import MuiThemeProvider from "@material-ui/core/styles/MuiThemeProvider"; +import createMuiTheme from "@material-ui/core/styles/createMuiTheme"; +import themeObject from "./util/theme"; +import { SET_AUTHENTICATED } from "./redux/types"; +import { logoutUser, getUserData } from "./redux/actions/userActions"; // Components import AuthRoute from "./util/AuthRoute"; -axios.defaults.baseURL = 'http://localhost:5006/twistter-e4649/us-central1/api'; +axios.defaults.baseURL = "http://localhost:5006/twistter-e4649/us-central1/api"; // Pages -import home from './pages/Home'; -import signup from './pages/Signup'; -import login from './pages/Login'; -import user from './pages/user'; -import logout from './pages/Logout'; -import Delete from './pages/Delete'; -import writeMicroblog from './Writing_Microblogs.js'; -import editProfile from './pages/editProfile'; -import userLine from './Userline.js'; +import home from "./pages/Home"; +import signup from "./pages/Signup"; +import login from "./pages/Login"; +import user from "./pages/user"; +import logout from "./pages/Logout"; +import Delete from "./pages/Delete"; +import writeMicroblog from "./Writing_Microblogs.js"; +import editProfile from "./pages/editProfile"; +import userLine from "./Userline.js"; const theme = createMuiTheme(themeObject); @@ -42,39 +42,37 @@ if (token) { window.location.href = "/login"; } else { store.dispatch({ type: SET_AUTHENTICATED }); - axios.defaults.headers.common['Authorization'] = token; + axios.defaults.headers.common["Authorization"] = token; store.dispatch(getUserData()); } } - class App extends Component { render() { return ( -
+
- {/* AuthRoute checks if the user is logged in and if they are it redirects them to /home */} - - - - + {/* AuthRoute checks if the user is logged in and if they are it redirects them to /home */} + + + + - - - - {/* */} + + + + {/* */} - +
- diff --git a/twistter-frontend/src/Writing_Microblogs.js b/twistter-frontend/src/Writing_Microblogs.js index b1570d8..8d3dced 100644 --- a/twistter-frontend/src/Writing_Microblogs.js +++ b/twistter-frontend/src/Writing_Microblogs.js @@ -1,108 +1,129 @@ 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 { BrowserRouter as Router } from "react-router-dom"; +import Route from "react-router-dom/Route"; +import axios from "axios"; class Writing_Microblogs extends Component { - - constructor(props) { - super(props); - this.state = { - value: '', - title: '', - topics: '', - characterCount: 250 - - }; - + constructor(props) { + super(props); + this.state = { + value: "", + title: "", + topics: "", + characterCount: 250 + }; - this.handleChange = this.handleChange.bind(this); - this.handleSubmit = this.handleSubmit.bind(this); - this.handleChangeforPost = this.handleChangeforPost.bind(this); - this.handleChangeforTopics = this.handleChangeforTopics.bind(this); - - } + this.handleChange = this.handleChange.bind(this); + this.handleSubmit = this.handleSubmit.bind(this); + this.handleChangeforPost = this.handleChangeforPost.bind(this); + this.handleChangeforTopics = this.handleChangeforTopics.bind(this); + } - handleChange(event) { - this.setState( {title: event.target.value }); - } + handleChange(event) { + this.setState({ title: event.target.value }); + } - handleChangeforTopics(event) { - this.setState( {topics: event.target.value}); - } + handleChangeforTopics(event) { + this.setState({ topics: event.target.value }); + } - handleSubmit(event) { - // alert('A title for the microblog was inputted: ' + this.state.title + '\nA microblog was posted: ' + this.state.value); - const postData = { - body: this.state.value, - userHandle: "new user", - userImage: "bing-url", - microBlogTitle: this.state.title, - microBlogTopics: this.state.topics.split(', ') - } - const headers = { - headers: { 'Content-Type': 'application/json'} - } - - axios - .post('/putPost', postData, headers) - .then((res) =>{ - alert('Post was shared successfully!') - console.log(res.data); - }) - .catch((err) => { - alert('An error occured.'); - console.error(err); - }) - event.preventDefault(); - this.setState({value: '', title: '',characterCount: 250, topics: ''}) - } + handleSubmit(event) { + // alert('A title for the microblog was inputted: ' + this.state.title + '\nA microblog was posted: ' + this.state.value); + const postData = { + body: this.state.value, + userHandle: "new user", + userImage: "bing-url", + microBlogTitle: this.state.title, + microBlogTopics: this.state.topics.split(", ") + }; + const headers = { + headers: { "Content-Type": "application/json" } + }; - handleChangeforPost(event) { - this.setState({value: event.target.value }) - } + axios + .post("/putPost", postData, headers) + .then(res => { + alert("Post was shared successfully!"); + console.log(res.data); + }) + .catch(err => { + alert("An error occured."); + console.error(err); + }); + event.preventDefault(); + this.setState({ value: "", title: "", characterCount: 250, topics: "" }); + } - handleChangeforCharacterCount(event) { - const charCount = event.target.value.length - const charRemaining = 250 - charCount - this.setState({characterCount: charRemaining }) - - } + handleChangeforPost(event) { + this.setState({ value: event.target.value }); + } - render() { - return ( -
-
-
-