2017-09-20 03:48:46 +00:00
|
|
|
import React, { Component } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
|
|
|
|
import onClickOutsideAddon from 'react-onclickoutside'
|
2017-10-14 16:31:20 +00:00
|
|
|
import Notification from './Notification'
|
|
|
|
import Loading from './Loading'
|
2017-09-20 03:48:46 +00:00
|
|
|
|
|
|
|
class NotificationBox extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
notifications: PropTypes.array,
|
|
|
|
fetchNotifications: PropTypes.func.isRequired,
|
2017-09-22 22:38:38 +00:00
|
|
|
toggleNotificationsBox: PropTypes.func.isRequired,
|
|
|
|
markAsRead: PropTypes.func.isRequired,
|
|
|
|
markAsUnread: PropTypes.func.isRequired
|
2017-09-20 03:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount = () => {
|
|
|
|
const { notifications, fetchNotifications } = this.props
|
|
|
|
if (!notifications) {
|
|
|
|
fetchNotifications()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClickOutside = () => {
|
|
|
|
this.props.toggleNotificationsBox()
|
|
|
|
}
|
|
|
|
|
2017-09-29 18:05:39 +00:00
|
|
|
hasSomeNotifications = () => {
|
|
|
|
const { notifications } = this.props
|
|
|
|
return notifications && notifications.length > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
showLoading = () => {
|
|
|
|
return <li><Loading margin='30px auto' /></li>
|
|
|
|
}
|
|
|
|
|
|
|
|
showEmpty = () => {
|
|
|
|
return <li className='notificationsEmpty'>
|
|
|
|
You have no notifications. <br />
|
|
|
|
More time for dancing.
|
|
|
|
</li>
|
|
|
|
}
|
|
|
|
|
|
|
|
showNotifications = () => {
|
2017-09-22 22:38:38 +00:00
|
|
|
const { notifications, markAsRead, markAsUnread } = this.props
|
2017-09-29 18:05:39 +00:00
|
|
|
if (!this.hasSomeNotifications()) {
|
|
|
|
return this.showEmpty()
|
|
|
|
}
|
|
|
|
return notifications.slice(0, 10).map(
|
|
|
|
n => <Notification notification={n}
|
|
|
|
markAsRead={markAsRead}
|
|
|
|
markAsUnread={markAsUnread}
|
|
|
|
key={`notification-${n.id}`} />
|
|
|
|
).concat([
|
|
|
|
<li key='notification-see-all'>
|
|
|
|
<a href='/notifications' className='notificationsBoxSeeAll'>
|
|
|
|
See all
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
render = () => {
|
|
|
|
const { notifications } = this.props
|
2017-09-20 03:48:46 +00:00
|
|
|
return <div className='notificationsBox'>
|
|
|
|
<div className='notificationsBoxTriangle' />
|
2017-09-22 22:38:38 +00:00
|
|
|
<ul className='notifications'>
|
2017-09-29 18:05:39 +00:00
|
|
|
{notifications ? this.showNotifications() : this.showLoading()}
|
2017-09-20 03:48:46 +00:00
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default onClickOutsideAddon(NotificationBox)
|