2016-03-18 11:51:22 -04:00
|
|
|
var connect = require('react-redux').connect;
|
2015-10-20 14:42:17 -04:00
|
|
|
var injectIntl = require('react-intl').injectIntl;
|
2015-09-02 15:08:58 -04:00
|
|
|
var React = require('react');
|
|
|
|
|
2016-06-06 10:09:01 -04:00
|
|
|
var api = require('../../lib/api');
|
2016-09-08 17:23:49 -04:00
|
|
|
var log = require('../../lib/log');
|
2016-06-06 10:09:01 -04:00
|
|
|
var render = require('../../lib/render.jsx');
|
2016-05-19 16:55:25 -04:00
|
|
|
var sessionActions = require('../../redux/session.js');
|
2016-03-18 11:51:22 -04:00
|
|
|
|
2016-04-21 18:13:21 -04:00
|
|
|
var Page = require('../../components/page/www/page.jsx');
|
2017-07-17 12:19:49 -04:00
|
|
|
var SplashPresentation = require('./presentation.jsx');
|
2015-09-02 15:08:58 -04:00
|
|
|
|
2015-10-20 14:42:17 -04:00
|
|
|
var Splash = injectIntl(React.createClass({
|
2015-10-09 16:16:37 -04:00
|
|
|
type: 'Splash',
|
2015-09-02 15:08:58 -04:00
|
|
|
getInitialState: function () {
|
|
|
|
return {
|
2017-07-17 12:19:49 -04:00
|
|
|
projectCount: 20000000, // gets the shared project count
|
2016-01-14 10:25:03 -05:00
|
|
|
activity: [], // recent social actions taken by users someone is following
|
|
|
|
news: [], // gets news posts from the scratch Tumblr
|
2017-07-13 15:35:18 -04:00
|
|
|
sharedByFollowing: [], // "Projects by Scratchers I'm Following"
|
|
|
|
lovedByFollowing: [], // "Projects Loved by Scratchers I'm Following"
|
|
|
|
inStudiosFollowing: [], // "Projects in Studios I'm Following"
|
2016-01-14 10:25:03 -05:00
|
|
|
featuredGlobal: {}, // global homepage rows, such as "Featured Projects"
|
2017-07-17 12:19:49 -04:00
|
|
|
emailConfirmationModalOpen: false, // flag that determines whether to show banner to request email conf.
|
2017-04-03 14:32:38 -04:00
|
|
|
refreshCacheStatus: 'notrequested'
|
2015-09-02 15:08:58 -04:00
|
|
|
};
|
|
|
|
},
|
2016-03-18 11:51:22 -04:00
|
|
|
getDefaultProps: function () {
|
|
|
|
return {
|
2017-07-17 12:19:49 -04:00
|
|
|
sessionStatus: sessionActions.Status.NOT_FETCHED,
|
|
|
|
user: {},
|
|
|
|
flags: {},
|
|
|
|
isEducator: false,
|
|
|
|
isAdmin: false
|
2016-03-18 12:40:30 -04:00
|
|
|
};
|
2016-03-18 11:51:22 -04:00
|
|
|
},
|
2016-03-18 12:40:30 -04:00
|
|
|
componentDidUpdate: function (prevProps) {
|
2017-07-17 12:19:49 -04:00
|
|
|
if (this.props.user != prevProps.user) {
|
2017-07-20 15:01:36 -04:00
|
|
|
if (this.props.user.username) {
|
2017-07-17 12:19:49 -04:00
|
|
|
this.getActivity(this.props.user.username);
|
2017-08-31 17:05:22 -04:00
|
|
|
this.getSharedByFollowing(this.props.user.username, this.props.user.token);
|
|
|
|
this.getInStudiosFollowing(this.props.user.username, this.props.user.token);
|
|
|
|
this.getLovedByFollowing(this.props.user.username, this.props.user.token);
|
2015-10-20 13:18:57 -04:00
|
|
|
this.getNews();
|
|
|
|
} else {
|
2017-07-13 15:35:18 -04:00
|
|
|
this.setState({sharedByFollowing: []});
|
|
|
|
this.setState({lovedByFollowing: []});
|
|
|
|
this.setState({inStudiosFollowing: []});
|
2015-10-24 14:52:11 -04:00
|
|
|
this.setState({activity: []});
|
|
|
|
this.setState({news: []});
|
2015-10-21 14:12:34 -04:00
|
|
|
this.getProjectCount();
|
2015-10-25 12:05:52 -04:00
|
|
|
}
|
|
|
|
if (this.shouldShowEmailConfirmation()) {
|
|
|
|
window.addEventListener('message', this.onMessage);
|
|
|
|
} else {
|
2015-10-23 19:02:00 -04:00
|
|
|
window.removeEventListener('message', this.onMessage);
|
2015-10-20 13:18:57 -04:00
|
|
|
}
|
2015-10-07 16:23:08 -04:00
|
|
|
}
|
|
|
|
},
|
2015-09-02 15:08:58 -04:00
|
|
|
componentDidMount: function () {
|
2015-10-21 15:12:41 -04:00
|
|
|
this.getFeaturedGlobal();
|
2017-07-20 15:01:36 -04:00
|
|
|
if (this.props.user.username) {
|
2017-07-17 12:19:49 -04:00
|
|
|
this.getActivity(this.props.user.username);
|
2017-08-31 17:05:22 -04:00
|
|
|
this.getSharedByFollowing(this.props.user.username, this.props.user.token);
|
|
|
|
this.getInStudiosFollowing(this.props.user.username, this.props.user.token);
|
|
|
|
this.getLovedByFollowing(this.props.user.username, this.props.user.token);
|
2015-10-20 13:18:57 -04:00
|
|
|
this.getNews();
|
2015-10-21 14:12:34 -04:00
|
|
|
} else {
|
|
|
|
this.getProjectCount();
|
2015-10-07 16:23:08 -04:00
|
|
|
}
|
2015-10-23 19:02:00 -04:00
|
|
|
},
|
2017-07-17 12:19:49 -04:00
|
|
|
getActivity: function (username) {
|
2016-06-06 10:09:01 -04:00
|
|
|
api({
|
2017-07-17 12:19:49 -04:00
|
|
|
uri: '/proxy/users/' + username + '/activity?limit=5'
|
2015-10-20 12:25:30 -04:00
|
|
|
}, function (err, body) {
|
2016-09-08 17:23:49 -04:00
|
|
|
if (!body) return log.error('No response body');
|
|
|
|
if (!err) return this.setState({activity: body});
|
2015-10-20 12:25:30 -04:00
|
|
|
}.bind(this));
|
|
|
|
},
|
2015-10-21 15:12:41 -04:00
|
|
|
getFeaturedGlobal: function () {
|
2016-06-06 10:09:01 -04:00
|
|
|
api({
|
2015-10-21 15:12:41 -04:00
|
|
|
uri: '/proxy/featured'
|
2015-10-20 12:25:30 -04:00
|
|
|
}, function (err, body) {
|
2016-09-08 17:23:49 -04:00
|
|
|
if (!body) return log.error('No response body');
|
|
|
|
if (!err) return this.setState({featuredGlobal: body});
|
2015-10-21 15:12:41 -04:00
|
|
|
}.bind(this));
|
|
|
|
},
|
2017-08-31 17:05:22 -04:00
|
|
|
getSharedByFollowing: function (username, token) {
|
2016-06-06 10:09:01 -04:00
|
|
|
api({
|
2017-08-31 17:05:22 -04:00
|
|
|
uri: '/users/' + username + '/following/users/projects',
|
2017-07-17 12:19:49 -04:00
|
|
|
authentication: token
|
2015-10-21 15:12:41 -04:00
|
|
|
}, function (err, body) {
|
2016-09-08 17:23:49 -04:00
|
|
|
if (!body) return log.error('No response body');
|
2017-07-13 15:35:18 -04:00
|
|
|
if (!err) return this.setState({sharedByFollowing: body});
|
|
|
|
}.bind(this));
|
|
|
|
},
|
2017-08-31 17:05:22 -04:00
|
|
|
getInStudiosFollowing: function (username, token) {
|
2017-07-13 15:35:18 -04:00
|
|
|
api({
|
2017-08-31 17:05:22 -04:00
|
|
|
uri: '/users/' + username + '/following/studios/projects',
|
2017-07-17 12:19:49 -04:00
|
|
|
authentication: token
|
2017-07-13 15:35:18 -04:00
|
|
|
}, function (err, body) {
|
|
|
|
if (!body) return log.error('No response body');
|
|
|
|
if (!err) return this.setState({inStudiosFollowing: body});
|
|
|
|
}.bind(this));
|
|
|
|
},
|
2017-08-31 17:05:22 -04:00
|
|
|
getLovedByFollowing: function (username, token) {
|
2017-07-13 15:35:18 -04:00
|
|
|
api({
|
2017-08-31 17:05:22 -04:00
|
|
|
uri: '/users/' + username + '/following/users/loves',
|
2017-07-19 11:25:39 -04:00
|
|
|
authentication: token
|
2017-07-13 15:35:18 -04:00
|
|
|
}, function (err, body) {
|
2017-07-19 11:25:39 -04:00
|
|
|
if (!body) return log.error('No response body');
|
|
|
|
if (!err) return this.setState({lovedByFollowing: body});
|
2015-10-21 15:12:41 -04:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
getNews: function () {
|
2016-06-06 10:09:01 -04:00
|
|
|
api({
|
2015-10-21 15:12:41 -04:00
|
|
|
uri: '/news?limit=3'
|
|
|
|
}, function (err, body) {
|
2016-09-08 17:23:49 -04:00
|
|
|
if (!body) return log.error('No response body');
|
|
|
|
if (!err) return this.setState({news: body});
|
2015-10-20 12:25:30 -04:00
|
|
|
}.bind(this));
|
|
|
|
},
|
2015-10-21 14:12:34 -04:00
|
|
|
getProjectCount: function () {
|
2016-06-06 10:09:01 -04:00
|
|
|
api({
|
2015-10-21 14:12:34 -04:00
|
|
|
uri: '/projects/count/all'
|
|
|
|
}, function (err, body) {
|
2016-09-08 17:23:49 -04:00
|
|
|
if (!body) return log.error('No response body');
|
|
|
|
if (!err) return this.setState({projectCount: body.count});
|
2015-10-21 14:12:34 -04:00
|
|
|
}.bind(this));
|
|
|
|
},
|
2015-10-28 08:57:05 -04:00
|
|
|
refreshHomepageCache: function () {
|
2016-06-06 10:09:01 -04:00
|
|
|
api({
|
2015-10-30 09:14:49 -04:00
|
|
|
host: '',
|
2015-10-28 08:57:05 -04:00
|
|
|
uri: '/scratch_admin/homepage/clear-cache/',
|
|
|
|
method: 'post',
|
|
|
|
useCsrf: true
|
|
|
|
}, function (err, body) {
|
|
|
|
if (err) return this.setState({refreshCacheStatus: 'fail'});
|
2016-09-08 17:23:49 -04:00
|
|
|
if (!body) return log.error('No response body');
|
2015-10-28 08:57:05 -04:00
|
|
|
if (!body.success) return this.setState({refreshCacheStatus: 'inprogress'});
|
|
|
|
return this.setState({refreshCacheStatus: 'pass'});
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
getHomepageRefreshStatus: function () {
|
|
|
|
var status = {
|
|
|
|
status: this.state.refreshCacheStatus,
|
|
|
|
disabled: false,
|
|
|
|
content: 'Refresh'
|
|
|
|
};
|
|
|
|
if (this.state.refreshCacheStatus === 'inprogress') {
|
|
|
|
status.disabled = true;
|
|
|
|
status.content = 'In Progress';
|
|
|
|
} else if (this.state.refreshCacheStatus === 'pass') {
|
|
|
|
status.disabled = true;
|
|
|
|
status.content = 'Requested';
|
|
|
|
} else if (this.state.refreshCacheStatus == 'fail') {
|
|
|
|
status.disabled = false;
|
|
|
|
status.content = 'Error';
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
},
|
2015-10-22 18:40:04 -04:00
|
|
|
showEmailConfirmationModal: function () {
|
2015-10-23 19:02:00 -04:00
|
|
|
this.setState({emailConfirmationModalOpen: true});
|
2015-10-22 18:40:04 -04:00
|
|
|
},
|
|
|
|
hideEmailConfirmationModal: function () {
|
2015-10-23 19:02:00 -04:00
|
|
|
this.setState({emailConfirmationModalOpen: false});
|
2015-10-22 18:40:04 -04:00
|
|
|
},
|
2015-10-22 17:17:27 -04:00
|
|
|
handleDismiss: function (cue) {
|
2017-04-03 14:32:38 -04:00
|
|
|
api({
|
|
|
|
host: '',
|
|
|
|
uri: '/site-api/users/set-template-cue/',
|
|
|
|
method: 'post',
|
|
|
|
useCsrf: true,
|
|
|
|
json: {cue: cue, value: false}
|
|
|
|
}, function (err) {
|
|
|
|
if (!err) this.props.dispatch(sessionActions.refreshSession());
|
|
|
|
}.bind(this));
|
2015-10-22 12:21:47 -04:00
|
|
|
},
|
2015-10-24 12:05:18 -04:00
|
|
|
shouldShowWelcome: function () {
|
2017-07-17 12:19:49 -04:00
|
|
|
if (!this.props.user || !this.props.flags.show_welcome) return false;
|
2015-10-24 12:05:18 -04:00
|
|
|
return (
|
2017-07-17 12:19:49 -04:00
|
|
|
new Date(this.props.user.dateJoined) >
|
2015-10-24 12:05:18 -04:00
|
|
|
new Date(new Date - 2*7*24*60*60*1000) // Two weeks ago
|
|
|
|
);
|
|
|
|
},
|
2015-10-25 12:05:52 -04:00
|
|
|
shouldShowEmailConfirmation: function () {
|
|
|
|
return (
|
2017-07-17 12:19:49 -04:00
|
|
|
this.props.user && this.props.flags.has_outstanding_email_confirmation &&
|
|
|
|
this.props.flags.confirm_email_banner);
|
2015-10-20 13:18:57 -04:00
|
|
|
},
|
2015-09-02 15:08:58 -04:00
|
|
|
render: function () {
|
2017-07-17 12:19:49 -04:00
|
|
|
var showEmailConfirmation = this.shouldShowEmailConfirmation() || false;
|
|
|
|
var showWelcome = this.shouldShowWelcome();
|
|
|
|
var homepageRefreshStatus = this.getHomepageRefreshStatus();
|
2016-01-07 09:57:34 -05:00
|
|
|
|
2015-09-02 15:08:58 -04:00
|
|
|
return (
|
2017-07-17 12:19:49 -04:00
|
|
|
<SplashPresentation
|
|
|
|
sessionStatus={this.props.sessionStatus}
|
|
|
|
user={this.props.user}
|
|
|
|
isEducator={this.props.isEducator}
|
|
|
|
isAdmin={this.props.isAdmin}
|
|
|
|
handleDismiss={this.handleDismiss}
|
|
|
|
refreshHomepageCache={this.refreshHomepageCache}
|
|
|
|
shouldShowEmailConfirmation={showEmailConfirmation}
|
|
|
|
emailConfirmationModalOpen={this.state.emailConfirmationModalOpen}
|
|
|
|
showEmailConfirmationModal={this.showEmailConfirmationModal}
|
|
|
|
hideEmailConfirmationModal={this.hideEmailConfirmationModal}
|
|
|
|
shouldShowWelcome={showWelcome}
|
|
|
|
projectCount={this.state.projectCount}
|
|
|
|
activity={this.state.activity}
|
|
|
|
news={this.state.news}
|
|
|
|
sharedByFollowing={this.state.sharedByFollowing}
|
|
|
|
lovedByFollowing={this.state.lovedByFollowing}
|
|
|
|
inStudiosFollowing={this.state.inStudiosFollowing}
|
|
|
|
featuredGlobal={this.state.featuredGlobal}
|
|
|
|
refreshCacheStatus={homepageRefreshStatus}
|
|
|
|
/>
|
2015-09-02 15:08:58 -04:00
|
|
|
);
|
|
|
|
}
|
2015-10-20 14:42:17 -04:00
|
|
|
}));
|
2015-09-02 15:08:58 -04:00
|
|
|
|
2016-03-18 11:51:22 -04:00
|
|
|
var mapStateToProps = function (state) {
|
|
|
|
return {
|
2017-07-17 12:19:49 -04:00
|
|
|
sessionStatus: state.session.status,
|
|
|
|
user: state.session.session.user,
|
|
|
|
flags: state.session.session.flags,
|
|
|
|
isEducator: state.permissions.educator,
|
|
|
|
isAdmin: state.permissions.admin
|
2016-03-18 11:51:22 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
var ConnectedSplash = connect(mapStateToProps)(Splash);
|
|
|
|
|
|
|
|
render(<Page><ConnectedSplash /></Page>, document.getElementById('app'));
|