Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit 5ebd4dd

Browse files
author
soliury
committed
add comment up
1 parent 359f72b commit 5ebd4dd

File tree

5 files changed

+348
-235
lines changed

5 files changed

+348
-235
lines changed
Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,116 @@
11
var React = require('react-native')
2+
var { Icon } = require('react-native-icons')
3+
4+
var TopicService = require('../../services/TopicService')
25

36
var {
47
Component,
5-
View
8+
View,
9+
TouchableOpacity,
10+
StyleSheet,
11+
Text,
12+
ActivityIndicatorIOS
613
} = React
714

815

916
class CommentUp extends Component {
1017
constructor(props) {
1118
super(props)
19+
this.state = {
20+
isUped: this._isUped(this.props.user.id, this.props.ups),
21+
isLoading: false,
22+
count: this.props.ups.length
23+
}
24+
}
25+
26+
27+
_onUpPress() {
28+
if (this.state.isLoading) return
29+
30+
this.setState({
31+
isLoading: true
32+
})
33+
34+
TopicService.req.upComment(this.props.replyId, this.props.user.token)
35+
.then(isUped=> {
36+
let count = this.state.count
37+
isUped ? count++ : count--
38+
this.setState({
39+
isUped: isUped,
40+
isLoading: false,
41+
count: count < 0 ? 0 : count
42+
})
43+
})
44+
.catch(err=> {
45+
console.warn(err)
46+
this.setState({
47+
isLoading: false
48+
})
49+
})
50+
.done()
51+
}
52+
53+
54+
_isUped(id, ups) {
55+
return ups.some(item=> {
56+
return item == id
57+
})
58+
}
59+
60+
61+
_renderUpIcon() {
62+
if (this.state.isLoading) {
63+
return (
64+
<ActivityIndicatorIOS
65+
size='small'
66+
style={styles.loading}
67+
></ActivityIndicatorIOS>
68+
)
69+
}
70+
return (
71+
<Icon
72+
name={'ion|thumbsup'}
73+
size={20}
74+
color={this.state.isUped ? '#3498DB':'rgba(0,0,0,0.2)'}
75+
style={styles.upIcon}
76+
/>
77+
)
1278
}
1379

1480

1581
render() {
82+
let count = this.state.count || 0
1683
return (
17-
<View>
84+
<TouchableOpacity
85+
onPress={this._onUpPress.bind(this)}>
86+
<View style={this.props.style}>
1887

19-
</View>
88+
{this._renderUpIcon()}
89+
90+
{count == 0 ? null : (<Text style={styles.text}>{count}</Text>)}
91+
</View>
92+
</TouchableOpacity>
2093
)
2194
}
2295
}
2396

2497

25-
module.exports = CommentUp
98+
var styles = StyleSheet.create({
99+
upIcon: {
100+
height: 12,
101+
width: 16
102+
},
103+
text: {
104+
paddingLeft: 7,
105+
fontSize: 12,
106+
color: 'rgba(0,0,0,0.2)',
107+
height: 12
108+
},
109+
loading: {
110+
height: 12,
111+
width: 16
112+
}
113+
})
114+
115+
116+
module.exports = CommentUp

0 commit comments

Comments
 (0)