2015-09-01 18:11:18 +02:00
|
|
|
'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';
|
2016-06-16 17:28:42 +02:00
|
|
|
import LinkContainer from 'react-router-bootstrap/lib/LinkContainer';
|
2015-09-01 18:11:18 +02:00
|
|
|
|
2015-09-03 14:15:00 +02:00
|
|
|
import NotificationActions from '../actions/notification_actions';
|
|
|
|
import NotificationStore from '../stores/notification_store';
|
2015-09-01 18:11:18 +02:00
|
|
|
|
2016-06-08 13:11:16 +02:00
|
|
|
import withContext from './context/with_context';
|
2016-06-06 18:52:05 +02:00
|
|
|
import { currentUserShape } from './prop_types';
|
|
|
|
|
2016-06-13 14:35:02 +02:00
|
|
|
import { getLangText } from '../utils/lang';
|
2015-09-01 18:11:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
let HeaderNotifications = React.createClass({
|
2016-03-10 13:41:36 +01:00
|
|
|
propTypes: {
|
2016-06-06 18:52:05 +02:00
|
|
|
// Injected through HOCs
|
|
|
|
currentUser: currentUserShape.isRequired, // eslint-disable-line react/sort-prop-types
|
2016-06-07 12:05:07 +02:00
|
|
|
isLoggedIn: React.PropTypes.bool.isRequired // eslint-disable-line react/sort-prop-types
|
2016-03-10 13:41:36 +01:00
|
|
|
},
|
|
|
|
|
2015-09-01 18:11:18 +02:00
|
|
|
getInitialState() {
|
2016-02-05 10:39:24 +01:00
|
|
|
return NotificationStore.getState();
|
2015-09-01 18:11:18 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount() {
|
2015-09-03 14:15:00 +02:00
|
|
|
NotificationStore.listen(this.onChange);
|
2016-03-10 13:41:36 +01:00
|
|
|
|
2016-06-07 12:05:07 +02:00
|
|
|
if (this.props.isLoggedIn) {
|
2016-03-10 13:41:36 +01:00
|
|
|
this.refreshNotifications();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (nextProps.currentUser && nextProps.currentUser.email !== this.props.currentUser.email) {
|
|
|
|
this.refreshNotifications();
|
|
|
|
}
|
2015-09-01 18:11:18 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2015-09-03 14:15:00 +02:00
|
|
|
NotificationStore.unlisten(this.onChange);
|
2015-09-01 18:11:18 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onChange(state) {
|
|
|
|
this.setState(state);
|
|
|
|
},
|
|
|
|
|
2016-03-10 13:41:36 +01:00
|
|
|
refreshNotifications() {
|
|
|
|
NotificationActions.fetchPieceListNotifications();
|
|
|
|
NotificationActions.fetchEditionListNotifications();
|
|
|
|
},
|
|
|
|
|
|
|
|
renderNotifications({ notifications, isPiece }) {
|
2015-12-16 10:24:54 +01:00
|
|
|
if (notifications.length) {
|
2015-09-03 14:15:00 +02:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className="notification-header">
|
2015-12-16 10:24:54 +01:00
|
|
|
{`${(isPiece ? 'Artworks' : 'Editions')} (${notifications.length})`}
|
2015-09-03 14:15:00 +02:00
|
|
|
</div>
|
2016-06-16 17:29:45 +02:00
|
|
|
{notifications.map((notification) => {
|
2016-05-04 16:44:39 +02:00
|
|
|
const pieceOrEdition = isPiece ? notification.piece : notification.edition;
|
|
|
|
const href = isPiece ? `/pieces/${pieceOrEdition.id}`
|
|
|
|
: `/editions/${pieceOrEdition.bitcoin_id}`;
|
|
|
|
|
2015-09-03 14:15:00 +02:00
|
|
|
return (
|
2016-06-16 17:28:42 +02:00
|
|
|
<LinkContainer key={href} to={href}>
|
|
|
|
<MenuItem>
|
|
|
|
<NotificationListItem
|
|
|
|
notification={notification.notification}
|
|
|
|
pieceOrEdition={pieceOrEdition} />
|
|
|
|
</MenuItem>
|
|
|
|
</LinkContainer>
|
2015-12-16 10:24:54 +01:00
|
|
|
);
|
|
|
|
})}
|
2015-09-03 14:15:00 +02:00
|
|
|
</div>
|
|
|
|
);
|
2015-12-16 10:24:54 +01:00
|
|
|
} else {
|
|
|
|
return null;
|
2015-09-03 14:15:00 +02:00
|
|
|
}
|
2015-09-01 18:38:24 +02:00
|
|
|
},
|
|
|
|
|
2015-09-01 18:11:18 +02:00
|
|
|
render() {
|
2015-12-16 10:24:54 +01:00
|
|
|
const { editionListNotifications, pieceListNotifications } = this.state;
|
|
|
|
if (pieceListNotifications.length || editionListNotifications.length) {
|
2015-09-03 14:15:00 +02:00
|
|
|
let numNotifications = 0;
|
2015-12-16 10:24:54 +01:00
|
|
|
|
|
|
|
if (pieceListNotifications.length) {
|
|
|
|
numNotifications += pieceListNotifications.length;
|
2015-09-03 14:15:00 +02:00
|
|
|
}
|
2015-12-16 10:24:54 +01:00
|
|
|
if (editionListNotifications.length) {
|
|
|
|
numNotifications += editionListNotifications.length;
|
2015-09-03 14:15:00 +02:00
|
|
|
}
|
2015-12-16 10:24:54 +01:00
|
|
|
|
2015-09-01 18:11:18 +02:00
|
|
|
return (
|
2016-06-08 17:43:07 +02:00
|
|
|
<Nav navbar pullRight className="notification-menu">
|
2015-09-01 18:11:18 +02:00
|
|
|
<DropdownButton
|
2016-02-05 10:39:24 +01:00
|
|
|
ref='dropdownButton'
|
2016-02-05 10:38:59 +01:00
|
|
|
id="header-notification-dropdown"
|
2015-09-01 18:11:18 +02:00
|
|
|
title={
|
|
|
|
<span>
|
2016-06-16 17:29:45 +02:00
|
|
|
<Glyphicon color="green" glyph="envelope" />
|
2015-09-03 14:15:00 +02:00
|
|
|
<span className="notification-amount">({numNotifications})</span>
|
2015-09-01 18:11:18 +02:00
|
|
|
</span>
|
2016-06-08 17:43:07 +02:00
|
|
|
}>
|
2016-03-10 13:41:36 +01:00
|
|
|
{this.renderNotifications({ notifications: pieceListNotifications, isPiece: true })}
|
|
|
|
{this.renderNotifications({ notifications: editionListNotifications, isPiece: false })}
|
2015-09-01 18:11:18 +02:00
|
|
|
</DropdownButton>
|
|
|
|
</Nav>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let NotificationListItem = React.createClass({
|
|
|
|
propTypes: {
|
2015-09-03 14:15:00 +02:00
|
|
|
notification: React.PropTypes.array,
|
|
|
|
pieceOrEdition: React.PropTypes.object,
|
|
|
|
},
|
2015-09-03 17:25:22 +02:00
|
|
|
|
2016-06-16 17:29:45 +02:00
|
|
|
getNotificationText() {
|
2015-09-03 17:25:22 +02:00
|
|
|
let numNotifications = null;
|
2016-06-16 17:29:45 +02:00
|
|
|
if (this.props.notification.length > 1) {
|
|
|
|
numNotifications = (
|
|
|
|
<div>
|
|
|
|
+ {this.props.notification.length - 1} {getLangText('more...')}
|
|
|
|
</div>
|
|
|
|
);
|
2015-09-03 17:25:22 +02:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div className="notification-action">
|
|
|
|
{this.props.notification[0].action_str}
|
|
|
|
{numNotifications}
|
|
|
|
</div>);
|
|
|
|
},
|
|
|
|
|
2015-09-01 18:11:18 +02:00
|
|
|
render() {
|
2016-06-16 17:29:45 +02:00
|
|
|
const { pieceOrEdition } = this.props;
|
|
|
|
|
|
|
|
if (pieceOrEdition) {
|
2015-09-01 18:11:18 +02:00
|
|
|
return (
|
2016-05-04 16:44:39 +02:00
|
|
|
<div className="row notification-wrapper">
|
|
|
|
<div className="col-xs-4 clear-paddings">
|
|
|
|
<div className="thumbnail-wrapper">
|
2016-06-16 17:29:45 +02:00
|
|
|
<img role="presentation" src={pieceOrEdition.thumbnail.url_safe} />
|
2015-09-01 18:11:18 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2016-05-04 16:44:39 +02:00
|
|
|
<div className="col-xs-8 notification-list-item-header">
|
2016-06-16 17:29:45 +02:00
|
|
|
<h1>{pieceOrEdition.title}</h1>
|
|
|
|
<div className="sub-header">by {pieceOrEdition.artist_name}</div>
|
2016-05-04 16:44:39 +02:00
|
|
|
{this.getNotificationText()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2015-09-01 18:11:18 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-08 13:11:16 +02:00
|
|
|
export default withContext(HeaderNotifications, 'currentUser', 'isLoggedIn');
|