-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create comments in different post
- Add CommentForm - Create new action and reducer to handle adding new comment - Add `RESET_COMMENT_AFTER_CHANGE_PATH` to reinitialize comments list when switch to different post
- Loading branch information
1 parent
c7b4c42
commit 0ef028c
Showing
7 changed files
with
161 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React, { Component } from 'react'; | ||
import { connect } from 'react-redux'; | ||
|
||
const uuidv4 = require('uuid/v4'); | ||
|
||
class CommentForm extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = ({ | ||
author:'', | ||
body:'', | ||
deleted:false, | ||
parentDeleted:false, | ||
voteScore:0 | ||
}) | ||
this.handleSubmit = this.handleSubmit.bind(this) | ||
this.handleChange = this.handleChange.bind(this) | ||
} | ||
handleSubmit(e) { | ||
e.preventDefault(); | ||
const newComment = { | ||
id: uuidv4(), | ||
author:this.state.author, | ||
body:this.state.body, | ||
deleted:this.state.deleted, | ||
parentId: this.props.post.id, | ||
parentDeleted:this.state.deleted, | ||
voteScore:this.state.voteScore, | ||
timestamp: Date.now(), | ||
} | ||
this.props.onSubmitRequest(newComment) | ||
} | ||
|
||
handleChange(e){ | ||
const { name, value } = e.target; | ||
this.setState({ | ||
[name]: value, | ||
}); | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
if (this.props.comments != nextProps.comments){ | ||
this.setState({ | ||
author:'', | ||
body:'' | ||
}) | ||
} | ||
} | ||
componentDidMount() { | ||
} | ||
render() { | ||
const { author, body } = this.state | ||
return ( | ||
<div> | ||
<form onSubmit={this.handleSubmit}> | ||
<input type="text" name="body" placeholder="New Comment" value={body} onChange={this.handleChange} required/> | ||
<input type="text" name="author" placeholder="Your Name" value={author} onChange={this.handleChange} required/> | ||
<input type='submit' value='Submit'/> | ||
</form> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default CommentForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,48 @@ | ||
import { combineReducers } from 'redux' | ||
|
||
import { | ||
GET_THIS_POST_COMMENTS | ||
GET_THIS_POST_COMMENTS, | ||
ADD_COMMENT, | ||
RESET_CURRENT_COMMENT_AFTER_CHANGE_PATH | ||
} from './action' | ||
|
||
function getThisPostComments (state={ | ||
isFetching: true, | ||
comments: [] | ||
}, action) { | ||
const { comments } = action | ||
switch (action.type) { | ||
case GET_THIS_POST_COMMENTS: | ||
return Object.assign({}, state, { | ||
isFetching: false, | ||
comments | ||
}) | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
function CurrentCommentsReducer (state={ | ||
comments: [] | ||
}, action) { | ||
const { comment, comments } = action | ||
switch (action.type) { | ||
case ADD_COMMENT: | ||
return Object.assign({}, state, { | ||
comments:[ | ||
...comments, | ||
comment | ||
] | ||
}) | ||
case RESET_CURRENT_COMMENT_AFTER_CHANGE_PATH: | ||
return Object.assign({}, state, { | ||
comments:[ | ||
] | ||
}) | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
export default combineReducers({ | ||
getThisPostComments | ||
getThisPostComments, | ||
CurrentCommentsReducer | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters