All files / components/post post.jsx

53.85% Statements 14/26
50% Branches 8/16
27.27% Functions 3/11
53.85% Lines 14/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131                  1x 1x 1x 1x 1x                               1x 1x 1x       1x         1x 1x                           1x                                                                                                                       1x                 1x            
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;