All files / components/chat-box chat-box.jsx

51.61% Statements 16/31
50% Branches 6/12
40% Functions 4/10
50% Lines 15/30
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 132 133 134 135 136 137 138 139 140 141              1x 1x 1x 1x 1x 1x 1x                                                   1x 1x 1x   1x                   1x 1x                                                                                                               1x                                 1x                          
import React from 'react';
import ReactDOM from 'react-dom';
import * as commonFuncs from '../../services/common-functions';
import PropTypes from 'prop-types';
 
class ChatBox extends React.Component {
  constructor(props) {
    super(props);
    this.state = { message: '' };
    this.toggleCollapse = this.toggleCollapse.bind(this);
    this.removeBox = this.removeBox.bind(this);
    this.toggleChat = this.toggleChat.bind(this);
    this.onSendMessage = this.onSendMessage.bind(this);
    this.isContacts = this.isContacts.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() {
    const box = ReactDOM.findDOMNode(this).children[0];
    commonFuncs.removeBox(box);
  }
 
  toggleChat() {
    const box = ReactDOM.findDOMNode(this).children[0];
    const classy = ' direct-chat-contacts-open';
    if (box.className.indexOf(classy) >= 0) {
      box.className = box.className.replace(classy, '');
    } else {
      box.className = `${box.className}${classy}`;
    }
  }
 
  isContacts() {
    let hasContactChild = false;
    React.Children.forEach(this.props.children, child => {
      Eif (`${child.type}`.includes('Contacts')) { hasContactChild = true; }
    });
    return this.props.children && hasContactChild;
  }
 
  onSendMessage(e) {
    e.preventDefault();
    e.stopPropagation();
    this.props.onSubmit(this.state.message);
  }
 
  render() {
    const borderClass = this.props.border === true ? 'box-solid' : '';
    return (
      <div className={`col-md-${this.props.width}`}>
        <div className={`box direct-chat ${this.props.headerTheme} ${this.props.chatTheme} ${borderClass}`}>
          <div className="box-header with-border">
            <h3 className="box-title">{this.props.title}</h3>
            <div className="box-tools pull-right">
              {this.props.notifications > 0 ? (
                <span data-toggle="tooltip" title="" className={`badge ${this.props.notificationTheme}`} data-original-title={`${this.props.notifications} New Messages`}>
                  {this.props.notifications}
                </span>
              ) : ''}
              {this.isContacts() ? (
                <button className="btn btn-box-tool" data-toggle="tooltip" title="" data-widget="chat-pane-toggle" data-original-title="Contacts" onClick={this.toggleChat}>
                  <i className="fa fa-comments" />
                </button>
              ) : ''}
              <button className="btn btn-box-tool" data-toggle="collapse" onClick={this.toggleCollapse}>
                <i className="fa fa-minus" />
              </button>
              <button className="btn btn-box-tool" data-toggle="remove" onClick={this.removeBox}>
                <i className="fa fa-times" />
              </button>
            </div>
          </div>
          <div className="box-body">
            {this.props.children}
          </div>
          <div className="box-footer">
            <form noValidate onSubmit={this.onSendMessage}>
              <input
                type="text"
                name="message"
                placeholder="Type message..."
                className="form-control"
                value={this.state.message}
                onChange={(e) => {
                  this.setState({ message: e.currentTarget.value });
                }}
              />
              <span className="input-group-btn">
                <button
                  type="button"
                  className={`btn btn-flat ${this.props.buttonTheme}`}
                  onClick={this.onSendMessage}
                >
                  Send
                </button>
              </span>
            </form>
          </div>
        </div>
      </div>
    );
  }
}
 
ChatBox.propTypes = {
  width: PropTypes.number,
  border: PropTypes.bool,
  headerTheme: PropTypes.string,
  notificationTheme: PropTypes.string,
  chatTheme: PropTypes.string,
  buttonTheme: PropTypes.string,
  title: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number
  ]),
  notifications: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number
  ]),
  onSubmit: PropTypes.func
};
ChatBox.defaultProps = {
  width: 3,
  border: false,
  headerTheme: 'box-primary',
  notificationTheme: 'bg-light-blue',
  chatTheme: 'direct-chat-primary',
  buttonTheme: 'btn-primary',
  title: 'Chat Box',
  notifications: 0,
  onSubmit: () => {}
};
 
export default ChatBox;