import React from 'react'; import { Link } from 'react-router-dom'; import { withRouter } from 'react-router-dom'; class Comments extends React.Component { constructor(props) { super(props); this.allComments = this.allComments.bind(this); this.commentForm = this.commentForm.bind(this); this.update = this.update.bind(this); this.handleSubmit = this.handleSubmit.bind(this); this.execDeleteComment = this.execDeleteComment.bind(this); this.state = { body: '', author_id:this.props.currentUser.id, post_id:this.props.post.id }; } componentDidMount(){ } componentWillReceiveProps(newProps){ this.setState({ body: '', author_id:newProps.currentUser.id, post_id:newProps.post.id }); } execDeleteComment(postId,key) { this.props.deleteComment(postId,key).then(()=>this.prop); } allComments() { if (this.props.comments) { let comments = this.props.comments; let commentKeys = Object.keys(this.props.comments); return commentKeys.map((key) => { key = parseInt(key); const picStyle = {backgroundImage:"url("+comments[key].authorpic+")"}; let deleteComment = () => { if(this.props.currentUser.id === comments[key].authorid) { return(
this.execDeleteComment(comments[key].post_id,key)}> ...
); } }; return (

{comments[key].authorf}

{comments[key].body}
{deleteComment()}
); }); } } update(field) { return (e) => { this.setState({[field]: e.target.value}); }; } handleSubmit(e) { e.preventDefault(); this.props.postComment(this.props.post.id, this.state); } commentForm() { return(
); } render() { return(
{this.allComments()}
{this.commentForm()}
); } } export default withRouter(Comments);