Skip to content

Commit 26d4a59

Browse files
committed
feat: add reply comment
1 parent 7bc1e7f commit 26d4a59

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed

src/actions/topics.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ export const likeComment = ({ id, accesstoken }) => dispatch => {
4545
})
4646
}
4747

48+
export const submitReply = ({ accesstoken, topic_id, content }) => dispatch => {
49+
return fetch(`${domain}topic/${topic_id}/replies`, {
50+
method: 'POST',
51+
body: JSON.stringify({ accesstoken, content }),
52+
headers: new Headers({
53+
'Content-Type': 'application/json'
54+
})
55+
})
56+
.then(handleResponse)
57+
}
58+
4859
const _fetchTopics = (page, tab, mutation, dispatch) => {
4960
fetch(`${domain}topics?page=${page}&tab=${tab}`)
5061
.then(handleResponse)

src/components/TopicDetail/index.jsx

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import {
1414
fetchTopicDetail,
1515
collectTopic,
16+
submitReply,
1617
likeComment
1718
} from 'actions/topics'
1819
import {
@@ -26,20 +27,29 @@ import 'highlight.js/styles/default.css'
2627
const format = raw => formatter(new Date(raw))
2728

2829
class TopicDetail extends Component {
30+
constructor(props) {
31+
super(props)
32+
this.state = {
33+
reply: ''
34+
}
35+
}
2936
componentWillMount() {
30-
const id = this.props.match.params.id
31-
32-
this.props.fetchTopicDetail({
33-
id,
34-
accesstoken: this.props.accesstoken
35-
})
37+
this._fetchTopicDetail()
3638
}
3739
componentDidMount() {
3840
const container = document.querySelector('.topic_body')
3941
new Zoomme({
4042
container
4143
})
4244
}
45+
_fetchTopicDetail() {
46+
const id = this.props.match.params.id
47+
48+
this.props.fetchTopicDetail({
49+
id,
50+
accesstoken: this.props.accesstoken
51+
})
52+
}
4353
collect() {
4454
const {
4555
topic,
@@ -72,6 +82,32 @@ class TopicDetail extends Component {
7282
id
7383
})
7484
}
85+
handleInput(e) {
86+
this.setState({
87+
reply: e.target.value.trim()
88+
})
89+
}
90+
submit() {
91+
const {
92+
accesstoken,
93+
topic,
94+
submitReply
95+
} = this.props
96+
if (!accesstoken) {
97+
alert('请先登录!')
98+
return
99+
}
100+
101+
submitReply({
102+
accesstoken,
103+
topic_id: topic.id,
104+
content: this.state.reply
105+
}).then(res => {
106+
if (res.success) {
107+
this._fetchTopicDetail()
108+
}
109+
})
110+
}
75111
render() {
76112
const {
77113
replies,
@@ -121,6 +157,13 @@ class TopicDetail extends Component {
121157
<div className="comment_header">{!!commentList ? '评论列表' : '暂无评论'}</div>
122158
<div className="comment_list">{ReplyList}</div>
123159
</div>
160+
<div className="topic_reply">
161+
<p>添加评论</p>
162+
<div className="form">
163+
<textarea onChange={this.handleInput.bind(this)}></textarea>
164+
<button className="button button_info" onClick={this.submit.bind(this)}>提交</button>
165+
</div>
166+
</div>
124167
</div>
125168
)
126169
}
@@ -140,6 +183,7 @@ function mapDispatchToProps(dispatch) {
140183
return bindActionCreators({
141184
fetchTopicDetail,
142185
collectTopic,
186+
submitReply,
143187
likeComment
144188
}, dispatch)
145189
}

src/components/TopicDetail/topicDetail.less

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,25 @@
123123
}
124124
}
125125
}
126+
127+
.topic_reply {
128+
margin-top: 10px;
129+
background-color: #fff;
130+
p {
131+
height: 40px;
132+
line-height: 40px;
133+
padding: 0 10px;
134+
font-size: 16px;
135+
color: #333;
136+
}
137+
.form {
138+
padding: 5px 10px;
139+
font-size: 14px;
140+
}
141+
textarea {
142+
display: block;
143+
width: 100%;
144+
height: 140px;
145+
outline: none;
146+
}
147+
}

0 commit comments

Comments
 (0)