Merge pull request #91 from ClaytonWWilson/fix_profile

Fix profile
This commit is contained in:
Leon Liang 2019-11-21 20:21:24 -05:00 committed by GitHub
commit 64cc9bd156
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 310 additions and 279 deletions

View File

@ -1,20 +1,28 @@
const { admin, db } = require("../util/admin"); const { admin, db } = require("../util/admin");
exports.putTopic = (req, res) => { exports.putTopic = (req, res) => {
const newTopic = { let new_following = [];
topic: req.body.topic let userRef = db.doc(`/users/${req.userData.handle}`);
}; userRef
.get()
admin
.firestore()
.collection("topics")
.add(newTopic)
.then(doc => { .then(doc => {
const resTopic = newTopic; new_following = doc.data().followedTopics;
return res.status(200).json(resTopic); new_following.push(req.body.following);
// add stuff
userRef
.set({ followedTopics: new_following }, { merge: true })
.then(doc => {
return res
.status(201)
.json({ message: `Following ${req.body.following}` });
})
.catch(err => {
return res.status(500).json({ err });
});
return res.status(200).json({ message: "OK" });
}) })
.catch(err => { .catch(err => {
console.error(err); return res.status(500).json({ err });
return res.status(500).json({ error: "something is wrong" });
}); });
}; };
@ -40,22 +48,34 @@ exports.getAllTopics = (req, res) => {
}; };
exports.deleteTopic = (req, res) => { exports.deleteTopic = (req, res) => {
const topic = db.doc(`/topics/${req.params.topicId}`); let new_following = [];
topic let userRef = db.doc(`/users/${req.userData.handle}`);
userRef
.get() .get()
.then(doc => { .then(doc => {
if (!doc.exists) { new_following = doc.data().followedTopics;
return res.status(404).json({ error: "Topic not found" }); // remove username from array
} else { new_following.forEach(function(follower, index) {
return topic.delete(); if (follower === `${req.body.unfollow}`) {
} new_following.splice(index, 1);
}) }
.then(() => { });
return res.json({ message: "Topic successfully deleted!" });
// update database
userRef
.set({ followedTopics: new_following }, { merge: true })
.then(doc => {
return res
.status(202)
.json({ message: `Successfully unfollow ${req.body.unfollow}` });
})
.catch(err => {
return res.status(500).json({ err });
});
return res.status(200).json({ message: "ok" });
}) })
.catch(err => { .catch(err => {
console.error(err); return res.status(500).json({ err });
return res.status(500).json({ error: "Failed to delete topic." });
}); });
}; };

View File

@ -96,7 +96,7 @@ app.post("/putTopic", fbAuth, putTopic);
app.get("/getAllTopics", fbAuth, getAllTopics); app.get("/getAllTopics", fbAuth, getAllTopics);
// delete a specific topic // delete a specific topic
app.delete("/deleteTopic/:topicId", fbAuth, deleteTopic); app.post("/deleteTopic", fbAuth, deleteTopic);
// get topic for this user // get topic for this user
app.post("/getUserTopics", fbAuth, getUserTopics); app.post("/getUserTopics", fbAuth, getUserTopics);

View File

@ -1,290 +1,301 @@
/* eslint-disable */ /* eslint-disable */
import React, { Component } from 'react'; import React, { Component } from "react";
import PropTypes from 'prop-types'; import PropTypes from "prop-types";
import { connect } from 'react-redux'; import { connect } from "react-redux";
import axios from 'axios'; import axios from "axios";
//import '../App.css'; //import '../App.css';
// Material-UI // Material-UI
import withStyles from '@material-ui/core/styles/withStyles'; import withStyles from "@material-ui/core/styles/withStyles";
import { makeStyles, styled } from '@material-ui/core/styles'; import { makeStyles, styled } from "@material-ui/core/styles";
import { Link } from 'react-router-dom'; import { Link } from "react-router-dom";
import Card from '@material-ui/core/Card'; import Card from "@material-ui/core/Card";
import CardMedia from '@material-ui/core/CardMedia'; import CardMedia from "@material-ui/core/CardMedia";
import CardContent from '@material-ui/core/CardContent'; import CardContent from "@material-ui/core/CardContent";
import Button from '@material-ui/core/Button'; import Button from "@material-ui/core/Button";
import Grid from '@material-ui/core/Grid'; import Grid from "@material-ui/core/Grid";
import Chip from '@material-ui/core/Chip'; import Chip from "@material-ui/core/Chip";
import Typography from '@material-ui/core/Typography'; import Typography from "@material-ui/core/Typography";
import AddCircle from '@material-ui/icons/AddCircle'; import AddCircle from "@material-ui/icons/AddCircle";
import TextField from '@material-ui/core/TextField'; import TextField from "@material-ui/core/TextField";
import VerifiedIcon from '@material-ui/icons/CheckSharp'; import VerifiedIcon from "@material-ui/icons/CheckSharp";
import Paper from '@material-ui/core/Paper'; import Paper from "@material-ui/core/Paper";
import GridList from '@material-ui/core/GridList'; import GridList from "@material-ui/core/GridList";
import GridListTile from '@material-ui/core/GridListTile'; import GridListTile from "@material-ui/core/GridListTile";
import GridListTileBar from '@material-ui/core/GridListTileBar'; import GridListTileBar from "@material-ui/core/GridListTileBar";
import Container from '@material-ui/core/Container'; import Container from "@material-ui/core/Container";
// component // component
import '../App.css'; import "../App.css";
import noImage from '../images/no-img.png'; import noImage from "../images/no-img.png";
import Writing_Microblogs from '../Writing_Microblogs'; import Writing_Microblogs from "../Writing_Microblogs";
const MyChip = styled(Chip)({ const MyChip = styled(Chip)({
margin: 2, margin: 2,
color: 'primary' color: "primary"
}); });
const styles = { const styles = {
button: { button: {
positon: 'relative', positon: "relative",
float: 'left', float: "left",
marginLeft: 30, marginLeft: 30,
marginTop: 20 marginTop: 20
}, },
paper: { paper: {
// marginLeft: "10%", // marginLeft: "10%",
// marginRight: "10%" // marginRight: "10%"
}, },
card: { card: {
marginBottom: 10 marginBottom: 10
}, },
profileImage: { profileImage: {
marginTop: 20 marginTop: 20
}, },
topicsContainer: { topicsContainer: {
border: 'lightgray solid 1px', border: "lightgray solid 1px",
marginTop: 20, marginTop: 20,
paddingTop: 10, paddingTop: 10,
paddingBottom: 10, paddingBottom: 10,
height: 300 height: 300
}, },
addCircle: { addCircle: {
width: 65, width: 65,
height: 65, height: 65,
marginTop: 10 marginTop: 10
}, },
username: { username: {
marginBottom: 100 marginBottom: 100
} }
}; };
class user extends Component { class user extends Component {
state = { state = {
profile: null, profile: null,
imageUrl: null, imageUrl: null,
topics: null, topics: null,
newTopic: null newTopic: null
}; };
handleDelete = (topic) => { handleDelete = topic => {
axios console.log(topic);
.delete(`/deleteTopic/${topic.id}`) axios
.then(function() { .post(`/deleteTopic`, {
location.reload(); unfollow: topic
}) })
.catch(function(err) { .then(function() {
console.log(err); location.reload();
}); })
}; .catch(function(err) {
console.log(err);
});
};
handleAddCircle = () => { handleAddCircle = () => {
axios axios
.post('/putTopic', { .post("/putTopic", {
topic: this.state.newTopic following: this.state.newTopic
}) })
.then(function() { .then(function() {
location.reload(); location.reload();
}) })
.catch(function(err) { .catch(function(err) {
console.log(err); console.log(err);
}); });
}; };
handleChange(event) { handleChange(event) {
this.setState({ this.setState({
newTopic: event.target.value newTopic: event.target.value
}); });
} }
componentDidMount() { componentDidMount() {
axios axios
.get('/user') .get("/user")
.then((res) => { .then(res => {
this.setState({ this.setState({
profile: res.data.credentials.handle, profile: res.data.credentials.handle,
imageUrl: res.data.credentials.imageUrl, imageUrl: res.data.credentials.imageUrl,
verified: res.data.credentials.verified ? res.data.credentials.verified : false verified: res.data.credentials.verified
}); ? res.data.credentials.verified
}) : false,
.catch((err) => console.log(err)); topics: res.data.credentials.followedTopics
});
})
.catch(err => console.log(err));
axios axios
.get('/getAllTopics') .get("/getallPostsforUser")
.then((res) => { .then(res => {
this.setState({ // console.log(res.data);
topics: res.data this.setState({
}); posts: res.data
}) });
.catch((err) => console.log(err)); })
.catch(err => console.log(err));
}
axios render() {
.get('/getallPostsforUser') const { classes } = this.props;
.then((res) => { let authenticated = this.props.user.authenticated;
console.log(res.data);
this.setState({
posts: res.data
});
})
.catch((err) => console.log(err));
}
render() { let profileMarkup = this.state.profile ? (
const { classes } = this.props; <div>
let authenticated = this.props.user.authenticated; <Typography variant="h5" className={classes.username}>
@{this.state.profile}{" "}
{this.state.verified ? (
<VerifiedIcon style={{ fill: "#1397D5" }} />
) : null}
</Typography>
</div>
) : (
<p className={classes.username}>loading username...</p>
);
let profileMarkup = this.state.profile ? ( let topicsMarkup = this.state.topics ? (
<div> this.state.topics.map(
<Typography variant="h5" className={classes.username}> topic => (
@{this.state.profile} {this.state.verified ? <VerifiedIcon style={{ fill: '#1397D5' }} /> : null} <MyChip
</Typography> label={topic}
</div> key={topic.id}
) : ( onDelete={key => this.handleDelete(topic)}
<p className={classes.username}>loading username...</p> />
); ) // console.log({ topic }.topic.id)
)
) : (
<p> loading topics...</p>
);
let topicsMarkup = this.state.topics ? ( let imageMarkup = this.state.imageUrl ? (
this.state.topics.map( <img
(topic) => ( className={classes.profileImage}
<MyChip src={this.state.imageUrl}
label={{ topic }.topic.topic} height="250"
key={{ topic }.topic.id} width="250"
onDelete={(key) => this.handleDelete(topic)} />
/> ) : (
) // console.log({ topic }.topic.id) <img
) className={classes.profileImage}
) : ( src={noImage}
<p> loading topics...</p> height="250"
); width="250"
/>
);
let imageMarkup = this.state.imageUrl ? ( let postMarkup = this.state.posts ? (
<img className={classes.profileImage} src={this.state.imageUrl} height="250" width="250" /> this.state.posts.map(post => (
) : ( <Card className={classes.card}>
<img className={classes.profileImage} src={noImage} height="250" width="250" /> <CardContent>
); <Typography>
{this.state.imageUrl ? (
<img src={this.state.imageUrl} height="50" width="50" />
) : (
<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>
);
let postMarkup = this.state.posts ? ( // FIX: This needs to check if user's profile page being displayed
this.state.posts.map((post) => ( // is the same as the user who is logged in
<Card className={classes.card}> // Can't check for that right now, because this page is always
<CardContent> // showing the logged in users profile, instead of retreiving the
<Typography> // profile based on the URL entered
{this.state.imageUrl ? ( let editButtonMarkup = true ? (
<img src={this.state.imageUrl} height="50" width="50" />
) : (
<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>
);
// FIX: This needs to check if user's profile page being displayed
// is the same as the user who is logged in
// Can't check for that right now, because this page is always
// showing the logged in users profile, instead of retreiving the
// profile based on the URL entered
let editButtonMarkup = true ? (
<Link to="/edit"> <Link to="/edit">
<Button className={classes.button} variant="outlined" color="primary"> <Button className={classes.button} variant="outlined" color="primary">
Edit Profile Edit Profile
</Button> </Button>
</Link> </Link>
) : null; ) : null;
return ( return (
<div> <div>
{/* <Paper className={classes.paper}> */} {/* <Paper className={classes.paper}> */}
<Grid container direction="column"> <Grid container direction="column">
<Grid item> <Grid item>
<Grid container> <Grid container>
<Grid item sm> <Grid item sm>
{editButtonMarkup} {editButtonMarkup}
</Grid> </Grid>
<Grid item sm> <Grid item sm>
{/* <Grid container direction="column"> */} {/* <Grid container direction="column"> */}
{/* <Grid item sm> */} {/* <Grid item sm> */}
{imageMarkup} {imageMarkup}
{profileMarkup} {profileMarkup}
{/* </Grid> */} {/* </Grid> */}
{/* <Grid item sm> */} {/* <Grid item sm> */}
{/* {postMarkup} */} {/* {postMarkup} */}
{/* </Grid> */} {/* </Grid> */}
{/* </Grid> */} {/* </Grid> */}
</Grid> </Grid>
<Grid item sm> <Grid item sm>
<Container className={classes.topicsContainer} maxWidth="xs"> <Container className={classes.topicsContainer} maxWidth="xs">
{topicsMarkup} {topicsMarkup}
</Container> </Container>
<TextField <TextField
id="newTopic" id="newTopic"
label="new topic" label="new topic"
defaultValue="" defaultValue=""
margin="normal" margin="normal"
variant="outlined" variant="outlined"
value={this.state.newTopic} value={this.state.newTopic}
onChange={(event) => this.handleChange(event)} onChange={event => this.handleChange(event)}
/> />
<AddCircle <AddCircle
className={classes.addCircle} className={classes.addCircle}
color="primary" color="primary"
// iconStyle={classes.addCircle} // iconStyle={classes.addCircle}
clickable clickable
onClick={this.handleAddCircle} onClick={this.handleAddCircle}
/> cursor="pointer"
</Grid> />
</Grid> </Grid>
</Grid> </Grid>
<Grid item> </Grid>
<Grid container> <Grid item>
<Grid item sm /> <Grid container>
<Grid item>{postMarkup}</Grid> <Grid item sm />
<Grid item sm /> <Grid item>{postMarkup}</Grid>
</Grid> <Grid item sm />
</Grid> </Grid>
</Grid> </Grid>
</div> </Grid>
); </div>
} );
}
} }
const mapStateToProps = (state) => ({ const mapStateToProps = state => ({
user: state.user user: state.user
}); });
user.propTypes = { user.propTypes = {
user: PropTypes.object.isRequired user: PropTypes.object.isRequired
}; };
export default connect(mapStateToProps)(withStyles(styles)(user)); export default connect(mapStateToProps)(withStyles(styles)(user));