import React, { Component } from "react";
import {
StyleSheet,
Text,
View,
Image,
TextInput,
TouchableOpacity
} from "react-native";
import courseDetails from '../services/courseDetails'
export default class Comment extends Component {
constructor(props) {
super(props);
this.state = {
text: '',
input: false,
default: '写评论',
postsList: [],
postsId: ''
};
}
componentWillReceiveProps(nextProps) {
console.log('id--------', nextProps.courseId)
// this.setState({
// postsList: nextProps.postsList
// })
// courseDetails.getPostsList(nextProps.columnId).success(res => {
// const postsList = res.data.list;
// console.log('==========', postsList)
// this.setState({
// postsList,
// })
// }).fail(error => {
// console.log('失败', error)
// })
courseDetails.getPostsList(nextProps.courseId).then(res => {
console.log('评论列表', res)
const postsList = res.data.list;
console.log(postsList)
this.setState({
postsList,
})
}).catch(error => {
console.log('失败', error)
})
}
render() {
return (
{this.props.title}
this.comments()}>
写评论
{
this.state.postsList.map((item, index) =>
this.listRender(item, index)
)
}
{
this.state.input ?
this.setState({text})}
value={this.state.text}
numberOfLines={7}
multiline = {true}
autoFocus = {true}
defaultValue= {this.state.default}
/>
this.send()}>发送
:
null
}
);
}
listRender(item, index) {
return (
{item.user ? item.user.nickName : '游客'}
{ new Date(item.posts.gmtCreated).toLocaleDateString() }
this.comments(item.posts.id)}>
回复
{item.posts.content}
{
item.replies.map((items, indexs) =>
this.repliesList(items, indexs)
)
}
)
}
repliesList (items, indexs) {
return (
{items.user ? items.user.nickName : '游客'}
{ items.content }
)
}
comments(id) {
this.setState({
input: true
})
if (id) {
this.setState({
postsId: id
})
}
}
send() {
const columnId = this.props.courseId;
const content = this.state.text;
const postsId = this.state.postsId;
console.log(postsId)
if (!content) {
alert('请输入内容')
return false
}
if (!postsId) {
courseDetails.addCommentList({
columnId,
columnNames: "卡通小熊",
content
}).then(res => {
console.log(res)
if ( res.code == 200) {
courseDetails.getPostsList(columnId).then(res => {
const postsList = res.data.list;
this.setState({
input: false,
postsList,
text: ''
})
}).catch(error => {
console.log('失败', error)
})
}
}).catch( err => console.log(err))
}else {
courseDetails.addReplyList({
postsId,
content
}).then(res => {
console.log(res)
if ( res.code == 200) {
courseDetails.getPostsList(columnId).then(res => {
const postsList = res.data.list;
console.log('==========', postsList)
this.setState({
input: false,
postsList,
text: ''
})
}).catch(error => {
console.log('失败', error)
})
}
}).catch( err => console.log(err))
}
}
}
const styles = StyleSheet.create({
courseComment: {
width: '100%',
backgroundColor: '#fff',
marginTop: 8,
// marginBottom: 60
},
padding: {
paddingLeft: 20,
paddingRight: 20,
paddingTop: 10,
paddingBottom: 10,
},
title: {
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
},
font: {
fontSize: 18,
color: 'black',
fontWeight: 'bold',
},
center: {
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
button: {
width: 98,
height: 28,
fontSize: 20,
borderRadius: 28,
color: 'black',
backgroundColor: '#F0F1F5'
},
headImg: {
width: 40,
height: 40,
backgroundColor: "red",
marginRight: 10,
borderRadius: 40
},
iconSize: {
width: 20,
height: 20,
marginRight: 10
},
reply: {
width: 16,
height: 15,
marginRight: 5
},
courseFont: {
color: '#373737',
fontSize: 16
},
column: {
display: 'flex',
flexDirection: 'column',
backgroundColor: '#F3F6FF',
borderColor: '#D8D8D8',
borderBottomWidth: 1
},
color: {
color: '#518AD1'
},
input: {
width: '100%',
height: 124,
backgroundColor: '#F9F9F9',
paddingLeft: 18,
paddingTop: 18,
paddingBottom: 18,
paddingRight: 24,
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'flex-end'
}
});