Skip to content

Commit 335f6c9

Browse files
authored
Merge pull request febobo#17 from febobo/develop
add user reply
2 parents ea682a6 + e3c1352 commit 335f6c9

File tree

8 files changed

+210
-63
lines changed

8 files changed

+210
-63
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"react-native-vector-icons": "^2.0.2",
2121
"react-redux": "^4.4.5",
2222
"redux": "^3.5.2",
23+
"redux-actions": "^0.10.0",
2324
"redux-logger": "^2.6.1",
2425
"redux-promise": "^0.5.3",
2526
"redux-thunk": "^2.1.0"

src/actions/ActionTypes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ export const CHECK_TOKEN = 'CHECK_TOKEN' // 检查token
66
export const TOAST = 'TOAST'; // 吐司
77
export const REPLY_TOPIC = 'REPLY_TOPIC'; // 评论
88
export const UPS_TOPIC = 'UPS_TOPIC'; // topic 点赞
9+
export const GET_TOPIC_DETAIL = 'GET_TOPIC_DETAIL'; // 获取文章详情
10+
export const UP_REPLY = 'UP_REPLY'; // 评论点赞

src/actions/CommentActions.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,23 @@ export const replyTopicById = createAction(types.REPLY_TOPIC , topicService.repl
99
replyId,
1010
user
1111
} , resolved , rejected) => {
12-
console.log(resolved)
1312
resolved();
1413
return {
14+
topicId,
15+
content : markdown.parse(content),
16+
replyId,
17+
user
18+
}
19+
})
20+
21+
export const getTopicDetail = createAction(types.GET_TOPIC_DETAIL , topicService.getReply ,(id) =>{
22+
return {
23+
id
24+
}
25+
})
26+
27+
export const upReply = createAction(types.UP_REPLY , topicService.upReply ,(id) =>{
28+
return {
29+
id
1530
}
1631
})

src/page/Comment.js

Lines changed: 133 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { connect } from 'react-redux';
2222
import TabShow from '../components/TabShow';
2323
import connectComponent from '../utils/connectComponent';
2424
import config from '../config';
25+
import * as QrCodeComponent from './QrCode';
26+
const QrCode = connectComponent(QrCodeComponent);
2527

2628
let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})
2729

@@ -33,6 +35,14 @@ class Comment extends Component {
3335
this.keyboardWillHideEvent = DeviceEventEmitter.addListener('keyboardWillHide', this.keyboardWillHide.bind(this));
3436
}
3537

38+
componentWillMount(){
39+
const { data } = this.props.Detail;
40+
if(data && data.id){
41+
const { actions } = this.props;
42+
actions.getTopicDetail(data.id)
43+
}
44+
}
45+
3646
keyboardWillShow (e){
3747
this.commentView && this.commentView.setNativeProps({
3848
style: {
@@ -49,39 +59,80 @@ class Comment extends Component {
4959
})
5060
}
5161

52-
reply (){
53-
const { actions ,Detail } = this.props;
62+
reply (name){
63+
const { actions ,Detail , User , navigator} = this.props;
64+
if(!User.data){
65+
return actions.toast('登陆后评论!',2000)
66+
}
67+
5468
if(!this.textInputValue){
5569
return actions.toast('评论不能为空')
5670
}
57-
console.log(this)
71+
5872
actions.replyTopicById({
5973
topicId: Detail.data.id,
6074
content: this.textInputValue + config.replySuffix,
6175
// replyId: this.replyId,
6276
user: {
63-
loginname: '212',
64-
avatar_url: '2212'
77+
loginname: 'test',
78+
avatar_url: 'http://test.imgs.wn518.com/upimages/ys-sales/2016-05-05/fc37e0ae1cdc0282019f3d7d25d6fcdf_1_0_0_480_480_0.jpg'
6579
}
6680
},()=>{
6781
this._resetReplyForm()
6882
})
6983
}
7084

85+
_onAuthorTextPress(authorName) {
86+
// if (!this.props.user) return;
87+
const { actions ,User} = this.props;
88+
if(!User.data){
89+
return actions.toast('登陆后评论!',2000)
90+
}
91+
92+
let text = (this.textInputValue || '') + ` @${authorName} `;
93+
this.textInput.setNativeProps({
94+
text: text
95+
});
96+
this.textInputValue = text;
97+
}
98+
99+
_scrollToTop() {
100+
this._listView.scrollTo({
101+
x: 0,
102+
y: 0
103+
});
104+
}
105+
71106
_resetReplyForm() {
72107
this.replyId = null;
73108
this.textInput.setNativeProps({
74109
text: ''
75110
});
76111
this.textInputValue = '';
77112
this.textInput.blur();
113+
this._scrollToTop()
78114
}
79115

116+
_upreply (id){
117+
const { actions } = this.props;
118+
actions.upReply(id);
119+
}
120+
121+
goLogin(){
122+
const { navigator } = this.props;
123+
navigator.push({
124+
component : QrCode
125+
})
126+
}
127+
80128
componentUnMount(){
81129
this.keyboardWillHideEvent.remove()
82130
this.keyboardWillShowEvent.remove()
83131
}
132+
133+
84134
render (){
135+
85136
const pointContent = (()=>{
86137
return (
87138
<Icon
@@ -92,7 +143,8 @@ class Comment extends Component {
92143
)
93144
})();
94145

95-
const { Detail , navigator} = this.props;
146+
const { Comment , navigator , User} = this.props;
147+
console.log(this)
96148
return (
97149
<View style={[styles.container]}>
98150
<View style={[styles.commentHeader]}>
@@ -104,7 +156,7 @@ class Comment extends Component {
104156
<View>
105157
<TouchableOpacity>
106158
<Text style={{fontSize:16,color:'#fff'}}>
107-
评论({Detail && Detail.data && Detail.data.reply_count})
159+
评论({Comment.topics.data && Comment.topics.data.replies && Comment.topics.data.replies.length})
108160
</Text>
109161
</TouchableOpacity>
110162
</View>
@@ -114,9 +166,10 @@ class Comment extends Component {
114166
ref={(view) => this.commentView = view}
115167
>
116168
{
117-
Detail && Detail.data && Detail.data.reply_count !=0 ?
169+
Comment.topics.data && Comment.topics.data.replies && Comment.topics.data.replies.length !=0 ?
118170
<ListView
119-
dataSource={ds.cloneWithRows(Detail.data.replies)}
171+
ref={(view) => this._listView = view}
172+
dataSource={ds.cloneWithRows(Comment.topics.data.replies.reverse())}
120173
renderRow={this._renderRow.bind(this)}
121174
initialListSize={10}
122175
onEndReachedThreshold={0}
@@ -128,45 +181,54 @@ class Comment extends Component {
128181
renderFooter={null}
129182
/> :
130183
<View style={{flexDirection:'row',paddingTop:30,alignItems : 'center',justifyContent : 'center'}}>
131-
<Text style={{fontSize:20,color:'#red'}}>暂无评论</Text>
184+
<Text style={{fontSize:20}}>暂无评论</Text>
132185
</View>
133186
}
134187
</View>
135-
<View style={[styles.commentBox]}>
136-
<Image
137-
style={[styles.article,styles.authorHeader]}
138-
source={{uri : 'http://test.imgs.wn518.com/upimages/ys-sales/2016-05-05/fc37e0ae1cdc0282019f3d7d25d6fcdf_1_0_0_480_480_0.jpg'}}
139-
/>
140-
<View style={{flex:1}}>
141-
<TextInput
142-
ref={(view) => this.textInput = view}
143-
value={this.state && this.state.textInput}
144-
onChangeText={(text) => {
145-
this.textInput.setNativeProps({
146-
text: text
147-
});
148-
this.textInputValue = text;
149-
this.setState({textInput:text})
150-
}}
151-
style={{height: 40, borderWidth:1,borderColor:'#eee'}}
152-
placeholder="说说我的看法"
153-
placeholderTextColor="#ccc"
154-
keyboardType={"default"}
155-
/>
156-
</View>
157-
<View>
158-
<TouchableOpacity
159-
style={[styles.replyBtn]}
160-
onPress={()=>this.reply()}
161-
>
162-
<Icon
163-
name='ios-undo'
164-
size={ 30 }
165-
color='#333'
166-
/>
188+
{
189+
User.data ?
190+
<View style={[styles.commentBox]}>
191+
<Image
192+
style={[styles.article,styles.authorHeader]}
193+
source={{uri : User.data.avatar_url}}
194+
/>
195+
<View style={{flex:1}}>
196+
<TextInput
197+
ref={(view) => this.textInput = view}
198+
199+
onChangeText={(text) => {
200+
this.textInput.setNativeProps({
201+
text: text
202+
});
203+
this.textInputValue = text;
204+
}}
205+
style={{height: 40, borderWidth:1,borderColor:'#eee'}}
206+
placeholder="说说我的看法"
207+
placeholderTextColor="#ccc"
208+
keyboardType={"default"}
209+
210+
/>
211+
</View>
212+
<View>
213+
<TouchableOpacity
214+
style={[styles.replyBtn]}
215+
onPress={()=>this.reply()}
216+
>
217+
<Icon
218+
name='ios-undo'
219+
size={ 30 }
220+
color='#ccc'
221+
/>
222+
</TouchableOpacity>
223+
</View>
224+
</View> :
225+
<View style={[styles.commentBox,{flexDirection:'row',alignItems:'center',justifyContent:"center"}]}>
226+
<TouchableOpacity onPress={()=>this.goLogin()}>
227+
<Text style={{fontSize:20,color:'#ccc',flex : 1,textAlign:'center'}}>登录后评论</Text>
167228
</TouchableOpacity>
168229
</View>
169-
</View>
230+
}
231+
170232
</View>
171233
)
172234
}
@@ -197,6 +259,30 @@ class Comment extends Component {
197259
content={rowData.content}
198260
style={htmlStyles}
199261
/>
262+
<View style={styles.undo}>
263+
<TouchableOpacity
264+
onPress={()=>this._upreply(rowData)}
265+
>
266+
<View>
267+
<Icon
268+
name='md-thumbs-up'
269+
size={ 20 }
270+
color='#ccc'
271+
/>
272+
</View>
273+
</TouchableOpacity>
274+
<TouchableOpacity
275+
onPress={()=>this._onAuthorTextPress(rowData.author.loginname)}
276+
>
277+
<View>
278+
<Icon
279+
name='ios-undo'
280+
size={ 20 }
281+
color='#ccc'
282+
/>
283+
</View>
284+
</TouchableOpacity>
285+
</View>
200286
</View>
201287
</View>
202288
)
@@ -227,6 +313,10 @@ const styles = StyleSheet.create({
227313
paddingTop : 5,
228314
marginLeft:5
229315
},
316+
undo : {
317+
flexDirection : 'row',
318+
justifyContent : 'space-between'
319+
},
230320
commentHeader : {
231321
height : headerHeight,
232322
paddingTop:20,
@@ -440,15 +530,3 @@ export function mapStateToProps(state){
440530
Detail : state.Detail
441531
}
442532
}
443-
// const mapActionCreators = (dispatch) => ({
444-
// comment : bindActionCreators(CommentActions , dispatch),
445-
// detail : bindActionCreators(DetailActions , dispatch),
446-
// })
447-
//
448-
// const mapStateToProps = (state)=>
449-
// ({
450-
// Comment : state.Comment,
451-
// Detail : state.Detail
452-
// })
453-
//
454-
// export default connect (mapStateToProps , mapActionCreators)(Comment)

src/page/Essence.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class Essence extends Component {
5959

6060
_getList(params){
6161
const { getList } = this.props.actions;
62+
const { data } = this.props.Essence
63+
if(data && data.length && page == 1) return;
6264
getList(params)
6365
}
6466

src/reducers/Comment.js

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,46 @@
11
import * as types from '../actions/actionTypes';
22

3-
const tabInitState = {
4-
selectedTab : 'essence'
3+
const initialState = {
4+
topics: {}
5+
};
6+
7+
function reply(topicId , replyId , content , user , state , payload){
8+
9+
let topic = state.topics.data;
10+
11+
topic.replies = topic.replies.reverse().concat([{
12+
topicId,
13+
replyId,
14+
content,
15+
author : user,
16+
create_at : new Date(),
17+
ups : []
18+
}])
19+
20+
return {
21+
...state,
22+
topics : { data : topic}
23+
}
24+
525
}
6-
export default function Comment (state=tabInitState , action={}){
26+
27+
export default function Comment (state=initialState , action={}){
28+
const { payload , meta={} } = action;
29+
const { topicId, content, replyId , user } = meta;
730
switch (action.type) {
8-
case types.TAB_CHANGE:
31+
case types.REPLY_TOPIC:
32+
return reply(topicId , replyId , content , user , state , payload)
33+
break;
34+
case types.GET_TOPIC_DETAIL:
935
return Object.assign(
1036
{} , state , {
11-
selectedTab : action.selectedTab
37+
topics : payload
1238
}
1339
)
1440
break;
41+
case types.UP_REPLY:
42+
return upReply(topicId , replyId , content , user , state , payload)
43+
break;
1544
default:
1645
return state;
1746
}

src/reducers/User.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as types from '../actions/actionTypes';
22

33
export default function User (state={} , action={}){
4-
console.log(action,111)
54
const {payload, error, meta = {}, type} = action;
65
switch (action.type) {
76
case types.CHECK_TOKEN:

0 commit comments

Comments
 (0)