Comment.js 7.0 KB

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