1
1
var React = require ( 'react-native' )
2
+ var { Icon } = require ( 'react-native-icons' )
3
+
4
+ var TopicService = require ( '../../services/TopicService' )
2
5
3
6
var {
4
7
Component,
5
- View
8
+ View,
9
+ TouchableOpacity,
10
+ StyleSheet,
11
+ Text,
12
+ ActivityIndicatorIOS
6
13
} = React
7
14
8
15
9
16
class CommentUp extends Component {
10
17
constructor ( props ) {
11
18
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
+ )
12
78
}
13
79
14
80
15
81
render ( ) {
82
+ let count = this . state . count || 0
16
83
return (
17
- < View >
84
+ < TouchableOpacity
85
+ onPress = { this . _onUpPress . bind ( this ) } >
86
+ < View style = { this . props . style } >
18
87
19
- </ View >
88
+ { this . _renderUpIcon ( ) }
89
+
90
+ { count == 0 ? null : ( < Text style = { styles . text } > { count } </ Text > ) }
91
+ </ View >
92
+ </ TouchableOpacity >
20
93
)
21
94
}
22
95
}
23
96
24
97
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