import React from 'react';
import NotificationItem from './notification-item.jsx';
import PropTypes from 'prop-types';
class HeaderNotifications extends React.Component {
constructor(props) {
super(props);
this.state = { open: false };
this.toggleDropdown = this.toggleDropdown.bind(this);
}
toggleDropdown(e) {
if ((e.type === 'blur' && this.state.open) || e.type !== 'blur') {
this.setState({ open: !this.state.open });
}
}
render() {
const notificationList = this.props.notifications.map((notificationDetails, i) => {
return (
<NotificationItem
key={i}
icon={notificationDetails.icon}
content={notificationDetails.content}
link={notificationDetails.link}
onClick={notificationDetails.onClick}
/>
);
});
return (
<li className={`dropdown notifications-menu${this.state.open ? ' open' : ''}`} tabIndex="0" onBlur={this.toggleDropdown}>
<a className="dropdown-toggle" data-toggle="dropdown-menu" onClick={this.toggleDropdown}>
<i className="fa fa-bell-o" />
<span className="label label-warning">
{this.props.count || this.props.notifications.length}
</span>
</a>
<ul className="dropdown-menu">
<li className="header">
You have {this.props.count || this.props.notifications.length} notifications
</li>
<li>
<div className="slimScrollDiv">
<ul className="menu">
{notificationList}
</ul>
<div className="slimScrollBar" />
<div className="slimScrollRail" />
</div>
</li>
{this.props.clickHandler ? (
<li className="footer" onClick={this.props.clickHandler}>
<a>View all</a>
</li>
) : ''}
</ul>
</li>
);
}
}
HeaderNotifications.propTypes = {
count: PropTypes.number,
notifications: PropTypes.array,
clickHandler: PropTypes.func
};
HeaderNotifications.defaultProps = {
notifications: []
};
export default HeaderNotifications;
|