metamaps--metamaps/src/components/NotificationBox.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react'
import PropTypes from 'prop-types'
2018-03-06 07:08:25 -05:00
import { Link } from 'react-router'
import onClickOutsideAddon from 'react-onclickoutside'
import Notification from './Notification'
import Loading from './Loading'
class NotificationBox extends Component {
static propTypes = {
notifications: PropTypes.array,
2018-03-07 18:38:53 -05:00
loading: PropTypes.bool.isRequired,
fetchNotifications: PropTypes.func.isRequired,
2017-09-22 18:38:38 -04:00
toggleNotificationsBox: PropTypes.func.isRequired,
markAsRead: PropTypes.func.isRequired,
markAsUnread: PropTypes.func.isRequired
}
componentDidMount = () => {
2018-03-07 15:35:27 -05:00
this.props.fetchNotifications()
}
handleClickOutside = () => {
this.props.toggleNotificationsBox()
}
2017-09-29 14:05:39 -04:00
hasSomeNotifications = () => {
const { notifications } = this.props
2018-03-07 15:35:27 -05:00
return notifications.length > 0
2017-09-29 14:05:39 -04:00
}
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 18:38:38 -04:00
const { notifications, markAsRead, markAsUnread } = this.props
2017-09-29 14:05:39 -04:00
if (!this.hasSomeNotifications()) {
return this.showEmpty()
}
return notifications.slice(0, 10).map(
n => <Notification notification={n}
markAsRead={markAsRead}
markAsUnread={markAsUnread}
2018-03-07 15:35:27 -05:00
key={`notification-${n.id}`}
onClick={() => this.props.toggleNotificationsBox()} />
2017-09-29 14:05:39 -04:00
).concat([
<li key='notification-see-all'>
2018-03-07 15:35:27 -05:00
<Link to='/notifications' className='notificationsBoxSeeAll' onClick={() => this.props.toggleNotificationsBox()}>
2017-09-29 14:05:39 -04:00
See all
2018-03-06 07:08:25 -05:00
</Link>
2017-09-29 14:05:39 -04:00
</li>
])
}
render = () => {
2018-03-08 07:25:54 -05:00
const { notifications, loading } = this.props
return <div className='notificationsBox'>
<div className='notificationsBoxTriangle' />
2017-09-22 18:38:38 -04:00
<ul className='notifications'>
2018-03-08 07:25:54 -05:00
{notifications.length === 0 && loading ? this.showLoading() : this.showNotifications()}
</ul>
</div>
}
}
export default onClickOutsideAddon(NotificationBox)