import React from 'react';
import ReactDOM from 'react-dom';
import Comment from './comment.jsx';
import Attachment from './attachment.jsx';
import * as commonFuncs from '../../services/common-functions';
import PropTypes from 'prop-types';
class Post extends React.Component {
constructor(props) {
super(props);
this.state = { message: '' };
this.toggleCollapse = this.toggleCollapse.bind(this);
this.removeBox = this.removeBox.bind(this);
this.onSendMessage = this.onSendMessage.bind(this);
}
toggleCollapse(e) {
const box = ReactDOM.findDOMNode(this).children[0];
const boxBody = ReactDOM.findDOMNode(this).children[0].children[1];
const icon = e.currentTarget.children[0];
commonFuncs.toggleBoxCollapse(box, boxBody, icon);
}
removeBox(e) {
const box = ReactDOM.findDOMNode(this).children[0];
commonFuncs.removeBox(box);
}
onSendMessage(e) {
e.preventDefault();
e.stopPropagation();
this.props.onSubmit(this.state.message);
}
render() {
const comments = this.props.postData.comments ? this.props.postData.comments.map((commentDetails, i) => {
return (
<Comment key={i} name={commentDetails.name} img={commentDetails.img} date={commentDetails.date} content={commentDetails.content} />
);
}) : [];
const postImg = this.props.postData.postImg ? <img className="img-responsive pad" src={this.props.postData.postImg} alt="Photo" /> : '';
const attachments = this.props.postData.attachments ? this.props.postData.attachments.map((attachmentDetails, i) => {
return (
<Attachment
key={i}
title={attachmentDetails.title}
img={attachmentDetails.img}
link={attachmentDetails.link}
onClick={attachmentDetails.onClick}
content={attachmentDetails.content}
>
{attachmentDetails.markup}
</Attachment>
);
}) : [];
return (
<div className={"col-md-" + this.props.width}>
<div className="box box-widget">
<div className="box-header with-border">
<div className="user-block">
<img className="img-circle" src={this.props.postData.displayImg} alt="User image" />
<span className="username">
<a
href={this.props.postData.link}
onClick={this.props.postData.onClick ? e => {
e.preventDefault();
e.stopPropagation();
this.props.postData.onClick();
} : () => {}}
>
{this.props.postData.displayName}
</a>
</span>
<span className="description">
{this.props.postData.status}
{this.props.postData.status && this.props.postData.date ? ' - ' : ''}
{this.props.postData.date}
</span>
</div>
<div className="box-tools">
<button className="btn btn-box-tool" data-widget="collapse" onClick={this.toggleCollapse}>
<i className="fa fa-minus" />
</button>
<button className="btn btn-box-tool" data-widget="remove" onClick={this.removeBox}>
<i className="fa fa-times" />
</button>
</div>
</div>
<div className="box-body">
{postImg}
<p>{this.props.postData.content}</p>
{attachments}
{this.props.children}
</div>
<div className="box-footer box-comments">
{comments}
</div>
<div className="box-footer">
<form noValidate onSubmit={this.onSendMessage}>
{this.props.userImg ? <img className="img-responsive img-circle img-sm" src={this.props.userImg} alt="alt text" /> : ''}
<div className={this.props.userImg ? 'img-push' : ''}>
<input
type="text"
className="form-control input-sm"
placeholder="Press enter to post comment"
onChange={e => { this.setState({ message: e.currentTarget.value }); }}
/>
</div>
</form>
</div>
</div>
</div>
);
}
}
Post.propTypes = {
userImg: PropTypes.string,
width: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
postData: PropTypes.object,
onSubmit: PropTypes.func
};
Post.defaultProps = {
width: 3,
onSubmit: () => {}
};
export default Post;
|