mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2025-02-17 08:31:23 -05:00
change to more extensible session
This commit is contained in:
parent
10a4dc656e
commit
4d65d478ed
4 changed files with 45 additions and 34 deletions
|
@ -19,8 +19,8 @@ var AdminPanel = React.createClass({
|
|||
render: function () {
|
||||
// make sure user is present before checking if they're an admin. Don't show anything if user not an admin.
|
||||
var showAdmin = false;
|
||||
if (this.props.session.user) {
|
||||
showAdmin = this.props.session.permissions.admin;
|
||||
if (this.props.session.results.user) {
|
||||
showAdmin = this.props.session.results.permissions.admin;
|
||||
}
|
||||
|
||||
if (!showAdmin) return false;
|
||||
|
|
|
@ -44,19 +44,19 @@ var Navigation = React.createClass({
|
|||
};
|
||||
},
|
||||
componentDidMount: function () {
|
||||
if (this.props.session.user) {
|
||||
if (this.props.session.results.user) {
|
||||
this.getMessageCount();
|
||||
var intervalId = setInterval(this.getMessageCount, 120000); // check for new messages every 2 mins.
|
||||
this.setState({'messageCountIntervalId': intervalId});
|
||||
}
|
||||
},
|
||||
componentDidUpdate: function (prevProps) {
|
||||
if (prevProps.session.user != this.props.session.user) {
|
||||
if (prevProps.session.results.user != this.props.session.results.user) {
|
||||
this.setState({
|
||||
'loginOpen': false,
|
||||
'accountNavOpen': false
|
||||
});
|
||||
if (this.props.session.user) {
|
||||
if (this.props.session.results.user) {
|
||||
this.getMessageCount();
|
||||
var intervalId = setInterval(this.getMessageCount, 120000);
|
||||
this.setState({'messageCountIntervalId': intervalId});
|
||||
|
@ -81,13 +81,13 @@ var Navigation = React.createClass({
|
|||
}
|
||||
},
|
||||
getProfileUrl: function () {
|
||||
if (!this.props.session.user) return;
|
||||
return '/users/' + this.props.session.user.username + '/';
|
||||
if (!this.props.session.results.user) return;
|
||||
return '/users/' + this.props.session.results.user.username + '/';
|
||||
},
|
||||
getMessageCount: function () {
|
||||
this.api({
|
||||
method: 'get',
|
||||
uri: '/users/' + this.props.session.user.username + '/messages/count'
|
||||
uri: '/users/' + this.props.session.results.user.username + '/messages/count'
|
||||
}, function (err, body) {
|
||||
if (err) return this.setState({'unreadMessageCount': 0});
|
||||
if (body) {
|
||||
|
@ -175,14 +175,14 @@ var Navigation = React.createClass({
|
|||
},
|
||||
render: function () {
|
||||
var classes = classNames({
|
||||
'logged-in': this.props.session.user
|
||||
'logged-in': this.props.session.results.user
|
||||
});
|
||||
var messageClasses = classNames({
|
||||
'message-count': true,
|
||||
'show': this.state.unreadMessageCount > 0
|
||||
});
|
||||
var formatMessage = this.props.intl.formatMessage;
|
||||
var createLink = this.props.session.user ? '/projects/editor/' : '/projects/editor/?tip_bar=home';
|
||||
var createLink = this.props.session.results.user ? '/projects/editor/' : '/projects/editor/?tip_bar=home';
|
||||
return (
|
||||
<NavigationBox className={classes}>
|
||||
<ul>
|
||||
|
@ -225,8 +225,8 @@ var Navigation = React.createClass({
|
|||
<Input type="hidden" name="sort_by" value="datetime_shared" />
|
||||
</form>
|
||||
</li>
|
||||
{this.props.session.loaded ? (
|
||||
this.props.session.user ? [
|
||||
{this.props.session.status == sessionActions.Status.FETCHED ? (
|
||||
this.props.session.results.user ? [
|
||||
<li className="link right messages" key="messages">
|
||||
<a
|
||||
href="/messages/"
|
||||
|
@ -246,8 +246,8 @@ var Navigation = React.createClass({
|
|||
</li>,
|
||||
<li className="link right account-nav" key="account-nav">
|
||||
<a className="user-info" href="#" onClick={this.handleAccountNavClick}>
|
||||
<Avatar src={this.props.session.user.thumbnailUrl} alt="" />
|
||||
{this.props.session.user.username}
|
||||
<Avatar src={this.props.session.results.user.thumbnailUrl} alt="" />
|
||||
{this.props.session.results.user.username}
|
||||
</a>
|
||||
<Dropdown
|
||||
as="ul"
|
||||
|
@ -263,16 +263,16 @@ var Navigation = React.createClass({
|
|||
<FormattedMessage id="general.myStuff" />
|
||||
</a>
|
||||
</li>
|
||||
{this.props.session.permissions.educator ? [
|
||||
{this.props.session.results.permissions.educator ? [
|
||||
<li>
|
||||
<a href="/educators/classes/">
|
||||
<FormattedMessage id="general.myClasses" />
|
||||
</a>
|
||||
</li>
|
||||
] : []}
|
||||
{this.props.session.permissions.student ? [
|
||||
{this.props.session.results.permissions.student ? [
|
||||
<li>
|
||||
<a href={'/classes/' + this.props.session.user.classroomId + '/'}>
|
||||
<a href={'/classes/' + this.props.session.results.user.classroomId + '/'}>
|
||||
<FormattedMessage id="general.myClass" />
|
||||
</a>
|
||||
</li>
|
||||
|
|
|
@ -8,10 +8,20 @@ var Types = keyMirror({
|
|||
SET_SESSION_ERROR: null
|
||||
});
|
||||
|
||||
module.exports.Status = keyMirror({
|
||||
FETCHED: null,
|
||||
NOT_FETCHED: null,
|
||||
FETCHING: null
|
||||
});
|
||||
|
||||
module.exports.getInitialState = function (){
|
||||
return {'status': module.exports.Status.NOT_FETCHED, 'results':{}};
|
||||
};
|
||||
|
||||
module.exports.sessionReducer = function (state, action) {
|
||||
// Reducer for handling changes to session state
|
||||
if (typeof state === 'undefined') {
|
||||
state = {};
|
||||
state = module.exports.getInitialState();
|
||||
}
|
||||
switch (action.type) {
|
||||
case Types.SET_SESSION:
|
||||
|
@ -31,20 +41,21 @@ module.exports.setSessionError = function (error) {
|
|||
};
|
||||
};
|
||||
|
||||
module.exports.setSession = function (session) {
|
||||
session.loaded = true;
|
||||
module.exports.setSession = function (status, results) {
|
||||
return {
|
||||
type: Types.SET_SESSION,
|
||||
session: session
|
||||
session: {'status': status,'results': results}
|
||||
};
|
||||
};
|
||||
|
||||
module.exports.refreshSession = function () {
|
||||
return function (dispatch) {
|
||||
dispatch(module.exports.setSession(module.exports.Status.NOT_FETCHED, {}));
|
||||
api({
|
||||
host: '',
|
||||
uri: '/session/'
|
||||
}, function (err, body) {
|
||||
dispatch(module.exports.setSession(module.exports.Status.FETCHING, {}));
|
||||
if (err) return dispatch(module.exports.setSessionError(err));
|
||||
|
||||
if (typeof body !== 'undefined') {
|
||||
|
@ -52,7 +63,7 @@ module.exports.refreshSession = function () {
|
|||
return window.location = body.url;
|
||||
} else {
|
||||
dispatch(tokenActions.getToken());
|
||||
dispatch(module.exports.setSession(body));
|
||||
dispatch(module.exports.setSession(module.exports.Status.FETCHED, body));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,8 +45,8 @@ var Splash = injectIntl(React.createClass({
|
|||
};
|
||||
},
|
||||
componentDidUpdate: function (prevProps) {
|
||||
if (this.props.session.user != prevProps.session.user) {
|
||||
if (this.props.session.user) {
|
||||
if (this.props.session.results.user != prevProps.session.results.user) {
|
||||
if (this.props.session.results.user) {
|
||||
this.getActivity();
|
||||
this.getFeaturedCustom();
|
||||
this.getNews();
|
||||
|
@ -65,7 +65,7 @@ var Splash = injectIntl(React.createClass({
|
|||
},
|
||||
componentDidMount: function () {
|
||||
this.getFeaturedGlobal();
|
||||
if (this.props.session.user) {
|
||||
if (this.props.session.results.user) {
|
||||
this.getActivity();
|
||||
this.getFeaturedCustom();
|
||||
this.getNews();
|
||||
|
@ -91,7 +91,7 @@ var Splash = injectIntl(React.createClass({
|
|||
},
|
||||
getActivity: function () {
|
||||
this.api({
|
||||
uri: '/proxy/users/' + this.props.session.user.username + '/activity?limit=5'
|
||||
uri: '/proxy/users/' + this.props.session.results.user.username + '/activity?limit=5'
|
||||
}, function (err, body) {
|
||||
if (!err) this.setState({activity: body});
|
||||
}.bind(this));
|
||||
|
@ -105,7 +105,7 @@ var Splash = injectIntl(React.createClass({
|
|||
},
|
||||
getFeaturedCustom: function () {
|
||||
this.api({
|
||||
uri: '/proxy/users/' + this.props.session.user.id + '/featured'
|
||||
uri: '/proxy/users/' + this.props.session.results.user.id + '/featured'
|
||||
}, function (err, body) {
|
||||
if (!err) this.setState({featuredCustom: body});
|
||||
}.bind(this));
|
||||
|
@ -172,16 +172,16 @@ var Splash = injectIntl(React.createClass({
|
|||
});
|
||||
},
|
||||
shouldShowWelcome: function () {
|
||||
if (!this.props.session.user || !this.props.session.flags.show_welcome) return false;
|
||||
if (!this.props.session.results.user || !this.props.session.results.flags.show_welcome) return false;
|
||||
return (
|
||||
new Date(this.props.session.user.dateJoined) >
|
||||
new Date(this.props.session.results.user.dateJoined) >
|
||||
new Date(new Date - 2*7*24*60*60*1000) // Two weeks ago
|
||||
);
|
||||
},
|
||||
shouldShowEmailConfirmation: function () {
|
||||
return (
|
||||
this.props.session.user && this.props.session.flags.has_outstanding_email_confirmation &&
|
||||
this.props.session.flags.confirm_email_banner);
|
||||
this.props.session.results.user && this.props.session.results.flags.has_outstanding_email_confirmation &&
|
||||
this.props.session.results.flags.confirm_email_banner);
|
||||
},
|
||||
renderHomepageRows: function () {
|
||||
var formatMessage = this.props.intl.formatMessage;
|
||||
|
@ -240,7 +240,7 @@ var Splash = injectIntl(React.createClass({
|
|||
);
|
||||
}
|
||||
|
||||
if (this.props.session.user &&
|
||||
if (this.props.session.results.user &&
|
||||
this.state.featuredGlobal.community_newest_projects &&
|
||||
this.state.featuredGlobal.community_newest_projects.length > 0) {
|
||||
|
||||
|
@ -373,8 +373,8 @@ var Splash = injectIntl(React.createClass({
|
|||
] : []}
|
||||
<CNBanner />
|
||||
<div key="inner" className="inner">
|
||||
{this.props.session.loaded ? (
|
||||
this.props.session.user ? [
|
||||
{this.props.session.status == sessionActions.Status.FETCHED ? (
|
||||
this.props.session.results.user ? [
|
||||
<div key="header" className="splash-header">
|
||||
{this.shouldShowWelcome() ? [
|
||||
<Welcome key="welcome"
|
||||
|
|
Loading…
Reference in a new issue