2017-08-31 17:05:22 -04:00
|
|
|
var connect = require('react-redux').connect;
|
|
|
|
var React = require('react');
|
|
|
|
|
|
|
|
var messageActions = require('../../redux/messages.js');
|
|
|
|
var render = require('../../lib/render.jsx');
|
|
|
|
var sessionActions = require('../../redux/session.js');
|
|
|
|
|
|
|
|
var Page = require('../../components/page/www/page.jsx');
|
|
|
|
var MessagesPresentation = require('./presentation.jsx');
|
|
|
|
|
|
|
|
var Messages = React.createClass({
|
|
|
|
type: 'ConnectedMessages',
|
|
|
|
getInitialState: function () {
|
|
|
|
return {
|
2017-10-24 13:10:32 -04:00
|
|
|
filter: ''
|
2017-08-31 17:05:22 -04:00
|
|
|
};
|
|
|
|
},
|
|
|
|
getDefaultProps: function () {
|
|
|
|
return {
|
|
|
|
sessionStatus: sessionActions.Status.NOT_FETCHED,
|
|
|
|
user: {},
|
|
|
|
flags: {},
|
|
|
|
messageOffset: 0,
|
|
|
|
numNewMessages: 0
|
|
|
|
};
|
|
|
|
},
|
|
|
|
componentDidUpdate: function (prevProps) {
|
2017-10-24 13:10:32 -04:00
|
|
|
if (this.props.user.username !== prevProps.user.username) {
|
2017-08-31 17:05:22 -04:00
|
|
|
if (this.props.user.token) {
|
|
|
|
this.props.dispatch(
|
|
|
|
messageActions.getMessages(
|
|
|
|
this.props.user.username,
|
|
|
|
this.props.user.token,
|
2017-10-24 13:10:32 -04:00
|
|
|
{
|
|
|
|
messages: this.props.messages,
|
|
|
|
offset: this.props.messageOffset,
|
|
|
|
filter: this.state.filter
|
|
|
|
}
|
2017-08-31 17:05:22 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
this.props.dispatch(
|
|
|
|
messageActions.getAdminMessages(
|
|
|
|
this.props.user.username, this.props.user.token, this.props.messageOffset
|
|
|
|
)
|
|
|
|
);
|
|
|
|
this.props.dispatch(
|
|
|
|
messageActions.getScratcherInvite(this.props.user.username, this.props.user.token)
|
|
|
|
);
|
2017-09-05 14:31:10 -04:00
|
|
|
} else {
|
|
|
|
// user is logged out, empty messages
|
|
|
|
this.props.dispatch(messageActions.setMessages([]));
|
|
|
|
this.props.dispatch(messageActions.setAdminMessages([]));
|
|
|
|
this.props.dispatch(messageActions.setScratcherInvite({}));
|
|
|
|
this.props.dispatch(messageActions.setMessagesOffset(0));
|
2017-08-31 17:05:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
componentDidMount: function () {
|
|
|
|
if (this.props.user.token) {
|
|
|
|
this.props.dispatch(
|
|
|
|
messageActions.getMessages(
|
|
|
|
this.props.user.username,
|
|
|
|
this.props.user.token,
|
2017-10-24 13:10:32 -04:00
|
|
|
{
|
|
|
|
messages: this.props.messages,
|
|
|
|
offset: this.props.messageOffset,
|
|
|
|
filter: this.state.filter
|
|
|
|
}
|
2017-08-31 17:05:22 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
this.props.dispatch(
|
|
|
|
messageActions.getAdminMessages(
|
|
|
|
this.props.user.username, this.props.user.token, this.props.messageOffset
|
|
|
|
)
|
|
|
|
);
|
|
|
|
this.props.dispatch(
|
|
|
|
messageActions.getScractherInvite(this.props.user.username, this.props.user.token)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
handleFilterClick: function (field, choice) {
|
2017-10-24 13:10:32 -04:00
|
|
|
if (this.props.user.token) {
|
|
|
|
this.props.dispatch(
|
|
|
|
messageActions.getMessages(
|
|
|
|
this.props.user.username,
|
|
|
|
this.props.user.token,
|
|
|
|
{
|
|
|
|
filter: choice,
|
|
|
|
clearCount: false
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
2017-08-31 17:05:22 -04:00
|
|
|
}
|
2017-10-24 13:10:32 -04:00
|
|
|
this.setState({filter: choice});
|
2017-08-31 17:05:22 -04:00
|
|
|
},
|
|
|
|
handleMessageDismiss: function (messageType, messageId) {
|
|
|
|
var adminMessages = null;
|
|
|
|
if (messageType === 'notification') {
|
|
|
|
adminMessages = this.props.adminMessages;
|
|
|
|
}
|
|
|
|
this.props.dispatch(
|
2017-09-01 11:07:21 -04:00
|
|
|
messageActions.clearAdminMessage(
|
|
|
|
messageType, messageId, this.props.numNewMessages, adminMessages
|
|
|
|
)
|
2017-08-31 17:05:22 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
handleLoadMoreMessages: function () {
|
|
|
|
this.props.dispatch(
|
|
|
|
messageActions.getMessages(
|
|
|
|
this.props.user.username,
|
|
|
|
this.props.user.token,
|
2017-10-24 13:10:32 -04:00
|
|
|
{
|
|
|
|
messages: this.props.messages,
|
|
|
|
offset: this.props.messageOffset,
|
|
|
|
filter: this.state.filter,
|
|
|
|
clearCount: false
|
|
|
|
}
|
2017-08-31 17:05:22 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
render: function () {
|
|
|
|
var loadMore = true;
|
|
|
|
if (this.props.messageOffset > this.props.messages.length && this.props.messageOffset > 0) {
|
|
|
|
loadMore = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(
|
|
|
|
<MessagesPresentation
|
|
|
|
sessionStatus={this.props.sessionStatus}
|
|
|
|
user={this.props.user}
|
2017-10-24 13:10:32 -04:00
|
|
|
messages={this.props.messages}
|
2017-08-31 17:05:22 -04:00
|
|
|
adminMessages={this.props.adminMessages}
|
|
|
|
scratcherInvite={this.props.invite}
|
2017-11-03 10:16:01 -04:00
|
|
|
numNewMessages={this.props.numNewMessages}
|
2017-08-31 17:05:22 -04:00
|
|
|
handleFilterClick={this.handleFilterClick}
|
|
|
|
handleAdminDismiss={this.handleMessageDismiss}
|
|
|
|
loadMore={loadMore}
|
|
|
|
loadMoreMethod={this.handleLoadMoreMessages}
|
|
|
|
requestStatus={this.props.requestStatus}
|
2017-10-24 13:10:32 -04:00
|
|
|
filter={this.props.filter}
|
2017-08-31 17:05:22 -04:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var mapStateToProps = function (state) {
|
|
|
|
return {
|
|
|
|
sessionStatus: state.session.status,
|
|
|
|
user: state.session.session.user,
|
|
|
|
flags: state.session.session.flags,
|
|
|
|
numNewMessages: state.messageCount.messageCount,
|
|
|
|
messages: state.messages.messages.social,
|
|
|
|
adminMessages: state.messages.messages.admin,
|
2017-09-08 10:24:39 -04:00
|
|
|
invite: state.messages.messages.invite,
|
2017-08-31 17:05:22 -04:00
|
|
|
messageOffset: state.messages.messages.socialOffset,
|
|
|
|
requestStatus: state.messages.status
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
var ConnectedMessages = connect(mapStateToProps)(Messages);
|
|
|
|
render(
|
|
|
|
<Page><ConnectedMessages /></Page>,
|
|
|
|
document.getElementById('app'),
|
|
|
|
{messages: messageActions.messagesReducer}
|
|
|
|
);
|