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;
|