Comment.js 6.3 KB

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