var connect = require('react-redux').connect;
var injectIntl = require('react-intl').injectIntl;
var React = require('react');
var api = require('../../lib/api');
var log = require('../../lib/log');
var render = require('../../lib/render.jsx');
var sessionActions = require('../../redux/session.js');
var shuffle = require('../../lib/shuffle.js').shuffle;
var Activity = require('../../components/activity/activity.jsx');
var AdminPanel = require('../../components/adminpanel/adminpanel.jsx');
var DropdownBanner = require('../../components/dropdown-banner/banner.jsx');
var Box = require('../../components/box/box.jsx');
var Button = require('../../components/forms/button.jsx');
var Carousel = require('../../components/carousel/carousel.jsx');
var Intro = require('../../components/intro/intro.jsx');
var IframeModal = require('../../components/modal/iframe/modal.jsx');
var News = require('../../components/news/news.jsx');
var Page = require('../../components/page/www/page.jsx');
var TeacherBanner = require('../../components/teacher-banner/teacher-banner.jsx');
var TTTModal = require('../../components/modal/ttt/modal.jsx');
var Welcome = require('../../components/welcome/welcome.jsx');
var MediaQuery = require('react-responsive');
var frameless = require('../../lib/frameless');
require('./splash.scss');
var Splash = injectIntl(React.createClass({
type: 'Splash',
getInitialState: function () {
return {
projectCount: 14000000, // gets the shared project count
activity: [], // recent social actions taken by users someone is following
news: [], // gets news posts from the scratch Tumblr
featuredCustom: {}, // custom homepage rows, such as "Projects by Scratchers I'm Following"
featuredGlobal: {}, // global homepage rows, such as "Featured Projects"
showEmailConfirmationModal: true, // flag that determines whether to show banner to request email conf.
refreshCacheStatus: 'notrequested'
};
},
getDefaultProps: function () {
return {
session: {},
permissions: {}
};
},
componentDidUpdate: function (prevProps) {
if (this.props.session.session.user != prevProps.session.session.user) {
if (this.props.session.session.user) {
this.getActivity();
this.getFeaturedCustom();
this.getNews();
} else {
this.setState({featuredCustom: []});
this.setState({activity: []});
this.setState({news: []});
this.getProjectCount();
}
if (this.shouldShowEmailConfirmation()) {
window.addEventListener('message', this.onMessage);
} else {
window.removeEventListener('message', this.onMessage);
}
}
},
componentDidMount: function () {
this.getFeaturedGlobal();
if (this.props.session.session.user) {
this.getActivity();
this.getFeaturedCustom();
this.getNews();
} else {
this.getProjectCount();
}
if (this.shouldShowEmailConfirmation()) window.addEventListener('message', this.onMessage);
},
componentWillUnmount: function () {
window.removeEventListener('message', this.onMessage);
},
onMessage: function (e) {
if (e.origin != window.location.origin) return;
if (e.source != this.refs.emailConfirmationiFrame.contentWindow) return;
if (e.data == 'resend-done') {
this.hideEmailConfirmationModal();
} else {
var data = JSON.parse(e.data);
if (data['action'] === 'leave-page') {
window.location.href = data['uri'];
}
}
},
getActivity: function () {
api({
uri: '/proxy/users/' + this.props.session.session.user.username + '/activity?limit=5'
}, function (err, body) {
if (!body) return log.error('No response body');
if (!err) return this.setState({activity: body});
}.bind(this));
},
getFeaturedGlobal: function () {
api({
uri: '/proxy/featured'
}, function (err, body) {
if (!body) return log.error('No response body');
if (!err) return this.setState({featuredGlobal: body});
}.bind(this));
},
getFeaturedCustom: function () {
api({
uri: '/proxy/users/' + this.props.session.session.user.id + '/featured'
}, function (err, body) {
if (!body) return log.error('No response body');
if (!err) return this.setState({featuredCustom: body});
}.bind(this));
},
getNews: function () {
api({
uri: '/news?limit=3'
}, function (err, body) {
if (!body) return log.error('No response body');
if (!err) return this.setState({news: body});
}.bind(this));
},
getProjectCount: function () {
api({
uri: '/projects/count/all'
}, function (err, body) {
if (!body) return log.error('No response body');
if (!err) return this.setState({projectCount: body.count});
}.bind(this));
},
refreshHomepageCache: function () {
api({
host: '',
uri: '/scratch_admin/homepage/clear-cache/',
method: 'post',
useCsrf: true
}, function (err, body) {
if (err) return this.setState({refreshCacheStatus: 'fail'});
if (!body) return log.error('No response body');
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;
},
showEmailConfirmationModal: function () {
this.setState({emailConfirmationModalOpen: true});
},
hideEmailConfirmationModal: function () {
this.setState({emailConfirmationModalOpen: false});
},
handleDismiss: function (cue) {
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));
},
shouldShowWelcome: function () {
if (!this.props.session.session.user || !this.props.session.session.flags.show_welcome) return false;
return (
new Date(this.props.session.session.user.dateJoined) >
new Date(new Date - 2*7*24*60*60*1000) // Two weeks ago
);
},
shouldShowEmailConfirmation: function () {
return (
this.props.session.session.user && this.props.session.session.flags.has_outstanding_email_confirmation &&
this.props.session.session.flags.confirm_email_banner);
},
renderHomepageRows: function () {
var formatMessage = this.props.intl.formatMessage;
var rows = [