All posts from db can now show on home page timeline

This commit is contained in:
Aaron Sun 2019-10-30 18:04:35 -04:00
parent a61c296ddf
commit 26afabe709
6 changed files with 123 additions and 29 deletions

View File

@ -22,7 +22,7 @@ exports.putPost = (req, res) => {
})
.catch((err) => {
console.error(err);
return res.status(500).json({ error: 'something is wrong'});
return res.status(500).json({ error: 'something went wrong'});
});
};
@ -41,6 +41,25 @@ exports.getallPostsforUser = (req, res) => {
return;
})
.catch(function(err) {
res.status(500).send("Failed to retrieve all user's posts from database.", err);
res.status(500).send("Failed to retrieve user's posts from database.", err);
});
};
exports.getallPosts = (req, res) => {
var post_query = admin.firestore().collection("posts");
post_query.get()
.then(function(allPosts) {
let posts = [];
allPosts.forEach(function(doc) {
posts.push(doc.data());
});
return res.status(200).json(posts);
})
.then(function() {
res.status(200).send("Successfully retrieved every post from database.");
return;
})
.catch(function(err) {
res.status(500).send("Failed to retrieve posts from database.", err);
});
};

View File

@ -44,11 +44,12 @@ app.get("/user", fbAuth, getAuthenticatedUser);
/*------------------------------------------------------------------*
* handlers/post.js *
*------------------------------------------------------------------*/
const { getallPostsforUser, putPost
} = require("./handlers/post");
const { getallPostsforUser, getallPosts, putPost } = require("./handlers/post");
app.get("/getallPostsforUser", fbAuth, getallPostsforUser);
app.get("/getallPosts", getallPosts);
// Adds one post to the database
app.post("/putPost", fbAuth, putPost);

View File

@ -63,23 +63,20 @@ class App extends Component {
<div className='container' >
<Navbar />
</div>
<div className="app">
<Switch>
{/* AuthRoute checks if the user is logged in and if they are it redirects them to /home */}
<AuthRoute exact path="/signup" component={signup} />
<AuthRoute exact path="/login" component={login} />
<AuthRoute exact path="/" component={home}/>
<Route exact path="/logout" component={logout} />
<Route exact path="/delete" component={Delete} />
<Route exact path="/home" component={home} />
<Route exact path="/user" component={user} />
<Route exact path="/home" component={writeMicroblog} />
<Route exact path="/edit" component={editProfile} />
<AuthRoute exact path="/" component={home}/>
</Switch>
</div>
</Router>
</Provider>
</MuiThemeProvider>

View File

@ -10,7 +10,7 @@ import Button from '@material-ui/core/Button';
import withStyles from "@material-ui/core/styles/withStyles";
// Redux stuff
// import { logoutUser } from '../../redux/actions/userActions';
import { logoutUser } from '../../redux/actions/userActions';
import { connect } from 'react-redux';
const styles = {
@ -63,13 +63,9 @@ const mapStateToProps = (state) => ({
user: state.user
})
// const mapActionsToProps = { logoutUser };
Navbar.propTypes = {
user: PropTypes.object.isRequired,
classes: PropTypes.object.isRequired
}
export default connect(mapStateToProps)(withStyles(styles)(Navbar));
// export default Navbar;

View File

@ -1,12 +1,74 @@
/* eslint-disable */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import axios from 'axios';
// Material UI and React Router
import Grid from '@material-ui/core/Grid';
import Card from '@material-ui/core/Card';
import CardContent from '@material-ui/core/CardContent';
import Typography from "@material-ui/core/Typography";
// component
import '../App.css';
import logo from '../images/twistter-logo.png';
import noImage from '../images/no-img.png';
import Writing_Microblogs from '../Writing_Microblogs';
class Home extends Component {
state = {};
componentDidMount() {
axios
.get("/getallPosts")
.then(res => {
console.log(res.data);
this.setState({
posts: res.data
})
})
.catch(err => console.log(err));
}
render() {
let authenticated = this.props.user.authenticated;
let postMarkup = this.state.posts ? (
this.state.posts.map(post =>
<Card>
<CardContent>
<Typography>
{
this.state.imageUrl ? (<img src={this.state.imageUrl} height="250" width="250" />) :
(<img src={noImage} height="50" width="50"/>)
}
</Typography>
<Typography variant="h7"><b>{post.userHandle}</b></Typography>
<Typography variant="body2" color={"textSecondary"}>{post.createdAt}</Typography>
<br />
<Typography variant="body1"><b>{post.microBlogTitle}</b></Typography>
<Typography variant="body2">{post.body}</Typography>
<br />
<Typography variant="body2"><b>Topics:</b> {post.microBlogTopics}</Typography>
<br />
<Typography variant="body2" color={"textSecondary"}>Likes {post.likeCount} Comments {post.commentCount}</Typography>
</CardContent>
</Card>
)
) : (<p>My Posts</p>);
return (
authenticated ?
<Grid container spacing={16}>
<Grid item sm={4} xs={8}>
<Writing_Microblogs />
</Grid>
<Grid item sm={4} xs={8}>
{postMarkup}
</Grid>
</Grid>
:
<div>
<div>
<img src={logo} className="app-logo" alt="logo" />
@ -34,4 +96,12 @@ class Home extends Component {
}
}
export default Home;
const mapStateToProps = (state) => ({
user: state.user
})
Home.propTypes = {
user: PropTypes.object.isRequired
}
export default connect(mapStateToProps)(Home);

View File

@ -1,8 +1,10 @@
/* eslint-disable */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import axios from 'axios';
//import '../App.css';
// Material UI and React Router
import { Link } from 'react-router-dom';
import { makeStyles, styled } from '@material-ui/core/styles';
import Grid from '@material-ui/core/Grid';
@ -16,7 +18,7 @@ import AddCircle from '@material-ui/icons/AddCircle';
import TextField from '@material-ui/core/TextField';
// component
import Userline from '../Userline';
import '../App.css';
import noImage from '../images/no-img.png';
import Writing_Microblogs from '../Writing_Microblogs';
@ -87,7 +89,8 @@ class user extends Component {
}
render() {
const classes = this.props;
let authenticated = this.props.user.authenticated;
let classes = this.props;
let profileMarkup = this.state.profile ? (
<p>
<Typography variant='h5'>{this.state.profile}</Typography>
@ -148,7 +151,7 @@ class user extends Component {
onClick={this.handleAddCircle}
/>
<br />
<Button component={ Link } to='/edit'>Edit Profile Info</Button>
{authenticated && <Button component={ Link } to='/edit'>Edit Profile Info</Button>}
</Grid>
<Grid item sm={4} xs={8}>
{postMarkup}
@ -161,4 +164,12 @@ class user extends Component {
}
}
export default user;
const mapStateToProps = (state) => ({
user: state.user
})
user.propTypes = {
user: PropTypes.object.isRequired
}
export default connect(mapStateToProps)(user);