Comment.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import React, { Component } from "react";
  2. import {
  3. StyleSheet,
  4. Text,
  5. View,
  6. Image,
  7. TextInput,
  8. TouchableOpacity
  9. } from "react-native";
  10. import courseDetails from '../services/courseDetails'
  11. export default class Comment extends Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. text: '',
  16. input: false,
  17. default: '写评论',
  18. postsList: [],
  19. postsId: ''
  20. };
  21. }
  22. componentWillReceiveProps(nextProps) {
  23. console.log('--------', nextProps)
  24. this.setState({
  25. postsList: nextProps.postsList
  26. })
  27. }
  28. render() {
  29. return (
  30. <View style={styles.courseComment}>
  31. <View style={[styles.padding, styles.title]}>
  32. <Text style={styles.font}>{this.props.title}</Text>
  33. <TouchableOpacity onPress={() => this.comments()}>
  34. <View style={[ styles.button, styles.center ]}>
  35. <Image source={require('../images/courseDetails/comment.png')} style={styles.iconSize} />
  36. <Text>写评论</Text>
  37. </View>
  38. </TouchableOpacity>
  39. </View>
  40. {
  41. this.state.postsList.map((item, index) =>
  42. this.listRender(item, index)
  43. )
  44. }
  45. {
  46. this.state.input ?
  47. <View style={[ styles.input, this.state.fullStyle]}>
  48. <TextInput
  49. style={{height: '100%', borderColor: '#DFDFDF', borderWidth: 1, width: '90%', backgroundColor: '#fff', borderRadius: 6, padding: 0, textAlignVertical: 'top', padding: 10}}
  50. onChangeText={(text) => this.setState({text})}
  51. value={this.state.text}
  52. numberOfLines={7}
  53. multiline = {true}
  54. autoFocus = {true}
  55. defaultValue= {this.state.default}
  56. />
  57. <Text style={{ color: '#58A8FA', fontSize: 18}} onPress={() => this.send()}>发送</Text>
  58. </View>
  59. :
  60. null
  61. }
  62. </View>
  63. );
  64. }
  65. listRender(item, index) {
  66. return (
  67. <View style={[ styles.padding ]} key={ index }>
  68. <View style={[ styles.title ]}>
  69. <View style={[ styles.center ]}>
  70. <Image style={[styles.headImg ]}></Image>
  71. <View>
  72. <Text style={{ fontSize: 16, color: '#373737' }}>{item.posts.columnNames}</Text>
  73. <Text style={{ fontSize: 14, color: '#7F7F7F' } }>2019-07-05</Text>
  74. </View>
  75. </View>
  76. <TouchableOpacity onPress={() => this.comments(item.posts.id)}>
  77. <View style={[ styles.center ]}>
  78. <Image source={require('../images/courseDetails/reply.png')} style={styles.reply} />
  79. <Text>回复</Text>
  80. </View>
  81. </TouchableOpacity>
  82. </View>
  83. <View style={ styles.padding }>
  84. <Text>{item.posts.content}</Text>
  85. </View>
  86. {
  87. item.replies.map((items, indexs) =>
  88. this.repliesList(items, indexs)
  89. )
  90. }
  91. </View>
  92. )
  93. }
  94. repliesList (items, indexs) {
  95. return (
  96. <View style={[ styles.padding, styles.column ]} key={ indexs }>
  97. <Text style={ styles.color }>卡通小熊</Text>
  98. <Text>{ items.content }</Text>
  99. </View>
  100. )
  101. }
  102. comments(id) {
  103. this.setState({
  104. input: true
  105. })
  106. if (id) {
  107. this.setState({
  108. postsId: id
  109. })
  110. }
  111. }
  112. send() {
  113. const columnId = this.props.courseId;
  114. const content = this.state.text;
  115. const postsId = this.state.postsId;
  116. console.log(postsId)
  117. if (!content) {
  118. alert('请输入内容')
  119. return false
  120. }
  121. if (!postsId) {
  122. courseDetails.addCommentList({
  123. columnId,
  124. columnNames: "卡通小熊",
  125. content
  126. }).then(res => {
  127. console.log(res)
  128. if ( res.code == 200) {
  129. courseDetails.getPostsList(columnId).success(res => {
  130. const postsList = res.data.list;
  131. this.setState({
  132. input: false,
  133. postsList,
  134. text: ''
  135. })
  136. }).fail(error => {
  137. console.log('失败', error)
  138. })
  139. }
  140. }).catch( err => console.log(err))
  141. }else {
  142. courseDetails.addReplyList({
  143. postsId,
  144. content
  145. }).then(res => {
  146. console.log(res)
  147. if ( res.code == 200) {
  148. courseDetails.getPostsList(columnId).success(res => {
  149. const postsList = res.data.list;
  150. console.log('==========', postsList)
  151. this.setState({
  152. input: false,
  153. postsList,
  154. text: ''
  155. })
  156. }).fail(error => {
  157. console.log('失败', error)
  158. })
  159. }
  160. }).catch( err => console.log(err))
  161. }
  162. }
  163. }
  164. const styles = StyleSheet.create({
  165. courseComment: {
  166. width: '100%',
  167. backgroundColor: '#fff',
  168. marginTop: 8,
  169. marginBottom: 60
  170. },
  171. padding: {
  172. paddingLeft: 20,
  173. paddingRight: 20,
  174. paddingTop: 10,
  175. paddingBottom: 10,
  176. },
  177. title: {
  178. display: 'flex',
  179. flexDirection: 'row',
  180. justifyContent: 'space-between',
  181. alignItems: 'center',
  182. },
  183. font: {
  184. fontSize: 24,
  185. color: 'black',
  186. fontWeight: 'bold',
  187. },
  188. center: {
  189. display: 'flex',
  190. flexDirection: 'row',
  191. alignItems: 'center',
  192. justifyContent: 'center',
  193. },
  194. button: {
  195. width: 98,
  196. height: 28,
  197. fontSize: 20,
  198. borderRadius: 28,
  199. color: 'black',
  200. backgroundColor: '#F0F1F5'
  201. },
  202. headImg: {
  203. width: 40,
  204. height: 40,
  205. backgroundColor: "red",
  206. marginRight: 10,
  207. borderRadius: 40
  208. },
  209. iconSize: {
  210. width: 20,
  211. height: 20,
  212. marginRight: 10
  213. },
  214. reply: {
  215. width: 16,
  216. height: 15,
  217. marginRight: 5
  218. },
  219. courseFont: {
  220. color: '#373737',
  221. fontSize: 16
  222. },
  223. column: {
  224. display: 'flex',
  225. flexDirection: 'column',
  226. backgroundColor: '#F3F6FF'
  227. },
  228. color: {
  229. color: '#518AD1'
  230. },
  231. input: {
  232. width: '100%',
  233. height: 124,
  234. backgroundColor: '#F9F9F9',
  235. paddingLeft: 18,
  236. paddingTop: 18,
  237. paddingBottom: 18,
  238. paddingRight: 24,
  239. display: 'flex',
  240. flexDirection: 'row',
  241. justifyContent: 'space-between',
  242. alignItems: 'flex-end'
  243. }
  244. });