118 lines
3.6 KiB
JavaScript
Raw Normal View History

import { Fragment } from 'react'
2020-03-03 22:45:16 -05:00
import ImmutablePropTypes from 'react-immutable-proptypes'
import ImmutablePureComponent from 'react-immutable-pure-component'
import { FormattedMessage } from 'react-intl'
2020-04-07 21:06:59 -04:00
import debounce from 'lodash.debounce'
2019-08-13 11:54:29 -04:00
import {
expandNotifications,
scrollTopNotifications,
dequeueNotifications,
2020-04-23 23:17:27 -04:00
} from '../actions/notifications'
import NotificationContainer from '../containers/notification_container'
import ScrollableList from '../components/scrollable_list'
import TimelineQueueButtonHeader from '../components/timeline_queue_button_header'
import Block from '../components/block'
2019-08-13 11:54:29 -04:00
2020-04-11 18:29:19 -04:00
const mapStateToProps = (state) => ({
notifications: state.getIn(['notifications', 'items']),
sortedNotifications: state.getIn(['notifications', 'sortedItems']),
2019-08-13 11:54:29 -04:00
isLoading: state.getIn(['notifications', 'isLoading'], true),
hasMore: state.getIn(['notifications', 'hasMore']),
totalQueuedNotificationsCount: state.getIn(['notifications', 'totalQueuedNotificationsCount'], 0),
2020-03-03 22:45:16 -05:00
})
2019-08-13 11:54:29 -04:00
2020-02-25 11:04:44 -05:00
export default
@connect(mapStateToProps)
2019-08-13 11:54:29 -04:00
class Notifications extends ImmutablePureComponent {
static propTypes = {
sortedNotifications: ImmutablePropTypes.list.isRequired,
2019-08-13 11:54:29 -04:00
notifications: ImmutablePropTypes.list.isRequired,
dispatch: PropTypes.func.isRequired,
isLoading: PropTypes.bool,
hasMore: PropTypes.bool,
dequeueNotifications: PropTypes.func,
totalQueuedNotificationsCount: PropTypes.number,
2020-03-03 22:45:16 -05:00
}
2019-08-13 11:54:29 -04:00
componentWillUnmount() {
2020-03-03 22:45:16 -05:00
this.handleLoadOlder.cancel()
this.handleScrollToTop.cancel()
this.handleScroll.cancel()
this.props.dispatch(scrollTopNotifications(false))
2019-08-13 11:54:29 -04:00
}
componentDidMount() {
2020-03-03 22:45:16 -05:00
this.handleDequeueNotifications()
this.props.dispatch(scrollTopNotifications(true))
2019-08-13 11:54:29 -04:00
}
handleLoadOlder = debounce(() => {
2020-03-03 22:45:16 -05:00
const last = this.props.notifications.last()
this.props.dispatch(expandNotifications({ maxId: last && last.get('id') }))
2020-03-03 22:45:16 -05:00
}, 300, { leading: true })
2019-08-13 11:54:29 -04:00
handleScrollToTop = debounce(() => {
2020-03-03 22:45:16 -05:00
this.props.dispatch(scrollTopNotifications(true))
}, 100)
2019-08-13 11:54:29 -04:00
handleScroll = debounce(() => {
2020-03-03 22:45:16 -05:00
this.props.dispatch(scrollTopNotifications(false))
}, 100)
2019-08-13 11:54:29 -04:00
handleDequeueNotifications = () => {
2020-03-03 22:45:16 -05:00
this.props.dispatch(dequeueNotifications())
}
2019-08-13 11:54:29 -04:00
render() {
2020-03-03 22:45:16 -05:00
const {
sortedNotifications,
2020-03-03 22:45:16 -05:00
isLoading,
hasMore,
totalQueuedNotificationsCount,
2020-03-03 22:45:16 -05:00
} = this.props
let scrollableContent = null
2019-08-13 11:54:29 -04:00
2020-02-22 18:26:23 -05:00
// : todo : include follow requests
2019-08-13 11:54:29 -04:00
if (isLoading && this.scrollableContent) {
2020-03-03 22:45:16 -05:00
scrollableContent = this.scrollableContent
} else if (sortedNotifications.size > 0 || hasMore) {
scrollableContent = sortedNotifications.map((item, index) => (
2019-08-13 11:54:29 -04:00
<NotificationContainer
2020-03-03 22:45:16 -05:00
key={`notification-${index}`}
2019-08-13 11:54:29 -04:00
notification={item}
/>
2020-03-03 22:45:16 -05:00
))
2019-08-13 11:54:29 -04:00
}
2020-03-03 22:45:16 -05:00
this.scrollableContent = scrollableContent
2019-08-13 11:54:29 -04:00
return (
<Fragment>
2020-03-02 17:26:25 -05:00
<TimelineQueueButtonHeader
onClick={this.handleDequeueNotifications}
count={totalQueuedNotificationsCount}
itemType='notification'
/>
2020-03-03 22:45:16 -05:00
<Block>
<ScrollableList
scrollKey='notifications'
isLoading={isLoading}
showLoading={isLoading && sortedNotifications.size === 0}
2020-03-03 22:45:16 -05:00
hasMore={hasMore}
emptyMessage={<FormattedMessage id='empty_column.notifications' defaultMessage="You don't have any notifications yet. Interact with others to start the conversation." />}
onLoadMore={this.handleLoadOlder}
onScrollToTop={this.handleScrollToTop}
onScroll={this.handleScroll}
>
{scrollableContent}
2020-03-03 22:45:16 -05:00
</ScrollableList>
</Block>
</Fragment>
2020-03-02 17:26:25 -05:00
)
2019-08-13 11:54:29 -04:00
}
}