mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-27 09:35:56 -05:00
e44e958fe0
Instead of retrieving the permission from the session cookie, store permission data from /session/, and cache it in a cookie. On subsequent page loads, the permission information will be retrieved from the cookie more quickly than the /session/ endpoint returns. When the session changes, the cookie and permissions state is updated to reflect the new state.
62 lines
1.6 KiB
JavaScript
62 lines
1.6 KiB
JavaScript
var keyMirror = require('keymirror');
|
|
var jar = require('../lib/jar.js');
|
|
|
|
var Types = keyMirror({
|
|
SET_PERMISSIONS: null,
|
|
SET_PERMISSIONS_ERROR: null
|
|
});
|
|
|
|
module.exports.permissionsReducer = function (state, action) {
|
|
if (typeof state === 'undefined') {
|
|
state = '';
|
|
}
|
|
switch (action.type) {
|
|
case Types.SET_PERMISSIONS:
|
|
return action.permissions;
|
|
case Types.SET_PERMISSIONS_ERROR:
|
|
return state;
|
|
default:
|
|
return state;
|
|
}
|
|
};
|
|
|
|
module.exports.storePermissions = function (permissions) {
|
|
permissions = permissions || {};
|
|
return function (dispatch) {
|
|
jar.set('permissions', permissions, {
|
|
encode: function (value) {
|
|
return encodeURIComponent(JSON.stringify(value));
|
|
}
|
|
});
|
|
return dispatch(module.exports.setPermissions(permissions));
|
|
};
|
|
};
|
|
|
|
module.exports.getPermissions = function () {
|
|
return function (dispatch) {
|
|
jar.get('permissions', function (err, value) {
|
|
if (err) return dispatch(module.exports.setPermissionsError(err));
|
|
|
|
try {
|
|
value = JSON.parse(decodeURIComponent(value)) || {};
|
|
} catch (e) {
|
|
value = {};
|
|
}
|
|
return dispatch(module.exports.setPermissions(value));
|
|
});
|
|
};
|
|
};
|
|
|
|
module.exports.setPermissions = function (permissions) {
|
|
return {
|
|
type: Types.SET_PERMISSIONS,
|
|
permissions: permissions
|
|
};
|
|
};
|
|
|
|
module.exports.setPermissionsError = function (error) {
|
|
return {
|
|
type: Types.SET_PERMISSIONS_ERROR,
|
|
error: error
|
|
};
|
|
};
|