delete topic fully works

This commit is contained in:
Leon Liang 2019-10-31 14:32:54 -04:00
parent b20408c144
commit 50bc73870b
2 changed files with 49 additions and 43 deletions

View File

@ -1,9 +1,8 @@
/* eslint-disable promise/always-return */ /* eslint-disable promise/always-return */
const { admin, db } = require("../util/admin"); const { admin, db } = require("../util/admin");
exports.putTopic = (req, res) => { exports.putTopic = (req, res) => {
const newTopic = { const newTopic = {
topic: req.body.topic, topic: req.body.topic
topicId: null
}; };
admin admin
@ -12,12 +11,11 @@ exports.putTopic = (req, res) => {
.add(newTopic) .add(newTopic)
.then(doc => { .then(doc => {
const resTopic = newTopic; const resTopic = newTopic;
newTopic.topicId = doc.id;
return res.status(200).json(resTopic); return res.status(200).json(resTopic);
}) })
.catch(err => { .catch(err => {
console.error(err); console.error(err);
return res.status(500).json({ error: "something is wrong" }); return res.status(500).json({ error: "something is wrong" });
}); });
}; };
@ -29,13 +27,16 @@ exports.getAllTopics = (req, res) => {
.then(data => { .then(data => {
let topics = []; let topics = [];
data.forEach(function(doc) { data.forEach(function(doc) {
topics.push(doc.data()); topics.push({
topic: doc.data().topic,
id: doc.id
});
}); });
return res.status(200).json(topics); return res.status(200).json(topics);
}) })
.catch(err => { .catch(err => {
console.error(err); console.error(err);
return res.status(500).json({ error: "Failed to fetch all topics." }); return res.status(500).json({ error: "Failed to fetch all topics." });
}); });
}; };
@ -45,16 +46,16 @@ exports.deleteTopic = (req, res) => {
.get() .get()
.then(doc => { .then(doc => {
if (!doc.exists) { if (!doc.exists) {
return res.status(404).json({ error: "Topic not found" }); return res.status(404).json({ error: "Topic not found" });
} else { } else {
return topic.delete(); return topic.delete();
} }
}) })
.then(() => { .then(() => {
res.json({ message: "Topic successfully deleted!" }); res.json({ message: "Topic successfully deleted!" });
}) })
.catch(err => { .catch(err => {
console.error(err); console.error(err);
return res.status(500).json({ error: "Failed to delete topic." }); return res.status(500).json({ error: "Failed to delete topic." });
}); });
}; };

View File

@ -1,8 +1,8 @@
/* 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 axios from "axios"; import axios from "axios";
//import '../App.css'; //import '../App.css';
import { makeStyles, styled } from "@material-ui/core/styles"; import { makeStyles, styled } from "@material-ui/core/styles";
import Grid from "@material-ui/core/Grid"; import Grid from "@material-ui/core/Grid";
import Card from "@material-ui/core/Card"; import Card from "@material-ui/core/Card";
@ -11,8 +11,7 @@ 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";
// component // component
import Userline from "../Userline";
import noImage from "../images/no-img.png"; import noImage from "../images/no-img.png";
const MyChip = styled(Chip)({ const MyChip = styled(Chip)({
@ -25,22 +24,18 @@ class user extends Component {
profile: null, profile: null,
imageUrl: null, imageUrl: null,
topics: null, topics: null,
newTopic: null, newTopic: null
deleteTopic: null
}; };
handleDelete = event => { handleDelete = topic => {
// axios axios
// .delete(`/deleteTopic/${topic}`) .delete(`/deleteTopic/${topic.id}`)
// .then( .then(function() {
// function (response) { location.reload();
// console.log(response); })
// } .catch(function(err) {
// ) console.log(err);
// .catch(function (err) { });
// console.log(err);
// });
console.log(event);
}; };
handleAddCircle = () => { handleAddCircle = () => {
@ -84,22 +79,25 @@ class user extends Component {
render() { render() {
let profileMarkup = this.state.profile ? ( let profileMarkup = this.state.profile ? (
<p> <p>
<Typography variant="h5">{this.state.profile}</Typography>         <Typography variant="h5">{this.state.profile}</Typography>
      
</p> </p>
) : ( ) : (
<p>loading username...</p> <p>loading username...</p>
); );
let topicsMarkup = this.state.topics ? ( let topicsMarkup = this.state.topics ? (
this.state.topics.map(topic => ( this.state.topics.map(
topic => (
<MyChip <MyChip
label={{ topic }.topic.topic} label={{ topic }.topic.topic}
key={{ topic }.topic.topicId} key={{ topic }.topic.id}
onDelete={this.handleDelete} onDelete={key => this.handleDelete(topic)}
/> />
)) ) // console.log({ topic }.topic.id)
)
) : ( ) : (
<p> loading topics...</p> <p> loading topics...</p>
); );
let imageMarkup = this.state.imageUrl ? ( let imageMarkup = this.state.imageUrl ? (
@ -110,24 +108,31 @@ class user extends Component {
return ( return (
<Grid container spacing={16}> <Grid container spacing={16}>
        
<Grid item sm={8} xs={12}> <Grid item sm={8} xs={12}>
<p>Post</p>           <p>Post</p>
        
</Grid> </Grid>
        
<Grid item sm={4} xs={12}> <Grid item sm={4} xs={12}>
{imageMarkup}           {imageMarkup}
{profileMarkup}           {profileMarkup}
{topicsMarkup}           {topicsMarkup}
          
<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 color="primary" clickable onClick={this.handleAddCircle} /> <AddCircle color="primary" clickable onClick={this.handleAddCircle} />
        
</Grid> </Grid>
      
</Grid> </Grid>
); );
} }