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");
exports.putTopic = (req, res) => {
const newTopic = {
topic: req.body.topic
};
admin
.firestore()
.collection("topics")
.add(newTopic)
let new_following = [];
let userRef = db.doc(`/users/${req.userData.handle}`);
userRef
.get()
.then(doc => {
const resTopic = newTopic;
return res.status(200).json(resTopic);
new_following = doc.data().followedTopics;
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 => {
console.error(err);
return res.status(500).json({ error: "something is wrong" });
return res.status(500).json({ err });
});
return res.status(200).json({ message: "OK" });
})
.catch(err => {
return res.status(500).json({ err });
});
};
@ -40,22 +48,34 @@ exports.getAllTopics = (req, res) => {
};
exports.deleteTopic = (req, res) => {
const topic = db.doc(`/topics/${req.params.topicId}`);
topic
let new_following = [];
let userRef = db.doc(`/users/${req.userData.handle}`);
userRef
.get()
.then(doc => {
if (!doc.exists) {
return res.status(404).json({ error: "Topic not found" });
} else {
return topic.delete();
new_following = doc.data().followedTopics;
// remove username from array
new_following.forEach(function(follower, index) {
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 => {
console.error(err);
return res.status(500).json({ error: "Failed to delete topic." });
return res.status(500).json({ err });
});
return res.status(200).json({ message: "ok" });
})
.catch(err => {
return res.status(500).json({ err });
});
};

View File

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

View File

@ -1,44 +1,44 @@
/* eslint-disable */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import axios from 'axios';
import React, { Component } from "react";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import axios from "axios";
//import '../App.css';
// Material-UI
import withStyles from '@material-ui/core/styles/withStyles';
import { makeStyles, styled } from '@material-ui/core/styles';
import { Link } from 'react-router-dom';
import Card from '@material-ui/core/Card';
import CardMedia from '@material-ui/core/CardMedia';
import CardContent from '@material-ui/core/CardContent';
import Button from '@material-ui/core/Button';
import Grid from '@material-ui/core/Grid';
import withStyles from "@material-ui/core/styles/withStyles";
import { makeStyles, styled } from "@material-ui/core/styles";
import { Link } from "react-router-dom";
import Card from "@material-ui/core/Card";
import CardMedia from "@material-ui/core/CardMedia";
import CardContent from "@material-ui/core/CardContent";
import Button from "@material-ui/core/Button";
import Grid from "@material-ui/core/Grid";
import Chip from '@material-ui/core/Chip';
import Typography from '@material-ui/core/Typography';
import AddCircle from '@material-ui/icons/AddCircle';
import TextField from '@material-ui/core/TextField';
import VerifiedIcon from '@material-ui/icons/CheckSharp';
import Paper from '@material-ui/core/Paper';
import GridList from '@material-ui/core/GridList';
import GridListTile from '@material-ui/core/GridListTile';
import GridListTileBar from '@material-ui/core/GridListTileBar';
import Container from '@material-ui/core/Container';
import Chip from "@material-ui/core/Chip";
import Typography from "@material-ui/core/Typography";
import AddCircle from "@material-ui/icons/AddCircle";
import TextField from "@material-ui/core/TextField";
import VerifiedIcon from "@material-ui/icons/CheckSharp";
import Paper from "@material-ui/core/Paper";
import GridList from "@material-ui/core/GridList";
import GridListTile from "@material-ui/core/GridListTile";
import GridListTileBar from "@material-ui/core/GridListTileBar";
import Container from "@material-ui/core/Container";
// component
import '../App.css';
import noImage from '../images/no-img.png';
import Writing_Microblogs from '../Writing_Microblogs';
import "../App.css";
import noImage from "../images/no-img.png";
import Writing_Microblogs from "../Writing_Microblogs";
const MyChip = styled(Chip)({
margin: 2,
color: 'primary'
color: "primary"
});
const styles = {
button: {
positon: 'relative',
float: 'left',
positon: "relative",
float: "left",
marginLeft: 30,
marginTop: 20
},
@ -53,7 +53,7 @@ const styles = {
marginTop: 20
},
topicsContainer: {
border: 'lightgray solid 1px',
border: "lightgray solid 1px",
marginTop: 20,
paddingTop: 10,
paddingBottom: 10,
@ -77,9 +77,12 @@ class user extends Component {
newTopic: null
};
handleDelete = (topic) => {
handleDelete = topic => {
console.log(topic);
axios
.delete(`/deleteTopic/${topic.id}`)
.post(`/deleteTopic`, {
unfollow: topic
})
.then(function() {
location.reload();
})
@ -90,8 +93,8 @@ class user extends Component {
handleAddCircle = () => {
axios
.post('/putTopic', {
topic: this.state.newTopic
.post("/putTopic", {
following: this.state.newTopic
})
.then(function() {
location.reload();
@ -109,34 +112,28 @@ class user extends Component {
componentDidMount() {
axios
.get('/user')
.then((res) => {
.get("/user")
.then(res => {
this.setState({
profile: res.data.credentials.handle,
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,
topics: res.data.credentials.followedTopics
});
})
.catch((err) => console.log(err));
.catch(err => console.log(err));
axios
.get('/getAllTopics')
.then((res) => {
this.setState({
topics: res.data
});
})
.catch((err) => console.log(err));
axios
.get('/getallPostsforUser')
.then((res) => {
console.log(res.data);
.get("/getallPostsforUser")
.then(res => {
// console.log(res.data);
this.setState({
posts: res.data
});
})
.catch((err) => console.log(err));
.catch(err => console.log(err));
}
render() {
@ -146,7 +143,10 @@ class user extends Component {
let profileMarkup = this.state.profile ? (
<div>
<Typography variant="h5" className={classes.username}>
@{this.state.profile} {this.state.verified ? <VerifiedIcon style={{ fill: '#1397D5' }} /> : null}
@{this.state.profile}{" "}
{this.state.verified ? (
<VerifiedIcon style={{ fill: "#1397D5" }} />
) : null}
</Typography>
</div>
) : (
@ -155,11 +155,11 @@ class user extends Component {
let topicsMarkup = this.state.topics ? (
this.state.topics.map(
(topic) => (
topic => (
<MyChip
label={{ topic }.topic.topic}
key={{ topic }.topic.id}
onDelete={(key) => this.handleDelete(topic)}
label={topic}
key={topic.id}
onDelete={key => this.handleDelete(topic)}
/>
) // console.log({ topic }.topic.id)
)
@ -168,13 +168,23 @@ class user extends Component {
);
let imageMarkup = this.state.imageUrl ? (
<img className={classes.profileImage} src={this.state.imageUrl} height="250" width="250" />
<img
className={classes.profileImage}
src={this.state.imageUrl}
height="250"
width="250"
/>
) : (
<img className={classes.profileImage} src={noImage} height="250" width="250" />
<img
className={classes.profileImage}
src={noImage}
height="250"
width="250"
/>
);
let postMarkup = this.state.posts ? (
this.state.posts.map((post) => (
this.state.posts.map(post => (
<Card className={classes.card}>
<CardContent>
<Typography>
@ -187,7 +197,7 @@ class user extends Component {
<Typography variant="h7">
<b>{post.userHandle}</b>
</Typography>
<Typography variant="body2" color={'textSecondary'}>
<Typography variant="body2" color={"textSecondary"}>
{post.createdAt}
</Typography>
<br />
@ -200,7 +210,7 @@ class user extends Component {
<b>Topics:</b> {post.microBlogTopics}
</Typography>
<br />
<Typography variant="body2" color={'textSecondary'}>
<Typography variant="body2" color={"textSecondary"}>
Likes {post.likeCount} Comments {post.commentCount}
</Typography>
</CardContent>
@ -254,7 +264,7 @@ class user extends Component {
margin="normal"
variant="outlined"
value={this.state.newTopic}
onChange={(event) => this.handleChange(event)}
onChange={event => this.handleChange(event)}
/>
<AddCircle
className={classes.addCircle}
@ -262,6 +272,7 @@ class user extends Component {
// iconStyle={classes.addCircle}
clickable
onClick={this.handleAddCircle}
cursor="pointer"
/>
</Grid>
</Grid>
@ -279,7 +290,7 @@ class user extends Component {
}
}
const mapStateToProps = (state) => ({
const mapStateToProps = state => ({
user: state.user
});