diff --git a/server/api/comments.js b/server/api/comments.js index 3dcc6b2..f063ae9 100755 --- a/server/api/comments.js +++ b/server/api/comments.js @@ -10,3 +10,13 @@ router.get('/', async (req, res, next) => { next(err) } }) + +router.post('/', async (req, res, next) => { + try { + const comment = await Comment.create({ text: req.body.text }) + await comment.setParent(req.body.parentId) + res.send(comment) + } catch (err) { + next(err) + } +}) diff --git a/server/db/seed.js b/server/db/seed.js index 73b8769..f750c2f 100755 --- a/server/db/seed.js +++ b/server/db/seed.js @@ -1,31 +1,14 @@ const db = require('../db') const { Comment } = require('./models') -const tc1 = { text: 'c1' } -const tc2 = { text: 'c2' } -const tc3 = { text: 'c3' } -const tc4 = { text: 'c4' } -const tc5 = { text: 'c5' } -const tc6 = { text: 'c6' } +const tc1 = { text: 'FIRST' } async function runSeed() { await db.sync({ force: true }) console.log('db synced!') console.log('seeding...') try { - const c1 = await Comment.create(tc1) - const c2 = await Comment.create(tc2) - const c3 = await Comment.create(tc3) - const c4 = await Comment.create(tc4) - const c5 = await Comment.create(tc5) - const c6 = await Comment.create(tc6) - - await c1.addReply(2) - - await c2.addReplies([c3, c4]) - await c5.setParent(c4) - - await c6.setParent(c1) + await Comment.create(tc1) console.log('seeded successfully') } catch (err) { diff --git a/src/App.js b/src/App.js index 4629786..e0f4c42 100644 --- a/src/App.js +++ b/src/App.js @@ -9,6 +9,7 @@ class App extends React.Component { constructor() { super() this.state = { loading: true } + this.submitComment = this.submitComment.bind(this) } componentDidMount() { @@ -35,6 +36,11 @@ class App extends React.Component { } } + async submitComment(submittedData) { + await axios.post(API + '/api/comments', submittedData) + await this.fetchData() + } + renderLoading() { return
loading...
} @@ -47,7 +53,7 @@ class App extends React.Component { return (
- +
) } diff --git a/src/Components/Comments/SingleComment/CommentReply.js b/src/Components/Comments/SingleComment/CommentReply.js new file mode 100644 index 0000000..7d00541 --- /dev/null +++ b/src/Components/Comments/SingleComment/CommentReply.js @@ -0,0 +1,35 @@ +import React from 'react' + +class CommentReply extends React.Component { + constructor(props) { + super(props) + this.state = { parentId: props.id, text: '' } + this.handleSubmit = this.handleSubmit.bind(this) + this.handleChange = this.handleChange.bind(this) + } + handleChange(e) { + this.setState({ [e.target.name]: e.target.value }) + } + + handleSubmit(e) { + e.preventDefault() + this.props.submitComment(this.state) + this.props.showReply(null) + } + render() { + return ( +
+