'use strict'; import React from 'react'; import DropdownButton from 'react-bootstrap/lib/DropdownButton'; import Glyphicon from 'react-bootstrap/lib/Glyphicon'; import MenuItem from 'react-bootstrap/lib/MenuItem'; import Nav from 'react-bootstrap/lib/Nav'; import NotificationActions from '../actions/notification_actions'; import NotificationStore from '../stores/notification_store'; import withContext from './context/with_context'; import { currentUserShape } from './prop_types'; import { getLangText } from '../utils/lang'; let HeaderNotifications = React.createClass({ propTypes: { // Injected through HOCs currentUser: currentUserShape.isRequired, // eslint-disable-line react/sort-prop-types isLoggedIn: React.PropTypes.bool.isRequired // eslint-disable-line react/sort-prop-types }, getInitialState() { return NotificationStore.getState(); }, componentDidMount() { NotificationStore.listen(this.onChange); if (this.props.isLoggedIn) { this.refreshNotifications(); } }, componentWillReceiveProps(nextProps) { if (nextProps.currentUser && nextProps.currentUser.email !== this.props.currentUser.email) { this.refreshNotifications(); } }, componentWillUnmount() { NotificationStore.unlisten(this.onChange); }, onChange(state) { this.setState(state); }, refreshNotifications() { NotificationActions.fetchPieceListNotifications(); NotificationActions.fetchEditionListNotifications(); }, renderNotifications({ notifications, isPiece }) { if (notifications.length) { return (
{`${(isPiece ? 'Artworks' : 'Editions')} (${notifications.length})`}
{notifications.map((notification, i) => { const pieceOrEdition = isPiece ? notification.piece : notification.edition; const href = isPiece ? `/pieces/${pieceOrEdition.id}` : `/editions/${pieceOrEdition.bitcoin_id}`; return ( ); })}
); } else { return null; } }, render() { const { editionListNotifications, pieceListNotifications } = this.state; if (pieceListNotifications.length || editionListNotifications.length) { let numNotifications = 0; if (pieceListNotifications.length) { numNotifications += pieceListNotifications.length; } if (editionListNotifications.length) { numNotifications += editionListNotifications.length; } return ( ); } return null; } }); let NotificationListItem = React.createClass({ propTypes: { notification: React.PropTypes.array, pieceOrEdition: React.PropTypes.object, }, getNotificationText(){ let numNotifications = null; if (this.props.notification.length > 1){ numNotifications =
+ {this.props.notification.length - 1} {getLangText('more...')}
; } return (
{this.props.notification[0].action_str} {numNotifications}
); }, render() { if (this.props.pieceOrEdition) { return (

{this.props.pieceOrEdition.title}

by {this.props.pieceOrEdition.artist_name}
{this.getNotificationText()}
); } return null; } }); export default withContext(HeaderNotifications, 'currentUser', 'isLoggedIn');