Skip to content

Commit

Permalink
Merge pull request #1 from ericelliott/component-routes
Browse files Browse the repository at this point in the history
Component routes
  • Loading branch information
ericelliott committed Jan 6, 2016
2 parents ead4f18 + 033800a commit c086cb6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion fields/types/cloudinaryimage/CloudinaryImageField.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ module.exports = Field.create({
var className = ['image-preview'];

if (this.hasLocal()) {
iconClassName = clasnames(iconClassUploadPending);
iconClassName = classnames(iconClassUploadPending);
} else if (this.state.removeExisting) {
className.push(' removed');
iconClassName = classnames(iconClassDeletePending);
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"bytes": "2.2.0",
"caller-id": "0.1.0",
"chalk": "1.1.1",
"classnames": "2.2.1",
"classnames": "2.2.3",
"cloudinary": "1.2.6",
"codemirror": "5.10.0",
"color": "0.11.0",
"color": "0.11.1",
"compression": "1.6.0",
"connect-flash": "0.1.1",
"cookie-parser": "1.4.0",
Expand Down Expand Up @@ -87,15 +87,15 @@
"codeclimate-test-reporter": "0.1.1",
"disc": "1.3.2",
"eslint": "1.10.3",
"eslint-plugin-react": "3.13.1",
"eslint-plugin-react": "3.14.0",
"gulp": "3.9.0",
"gulp-git": "1.6.1",
"gulp-streamify": "1.0.2",
"gulp-uglify": "1.5.1",
"istanbul": "0.4.1",
"mocha": "2.3.4",
"must": "0.13.1",
"react-engine": "2.6.1",
"react-engine": "2.6.2",
"rimraf": "2.5.0",
"sinon": "1.17.2",
"supertest": "1.1.0",
Expand Down
18 changes: 13 additions & 5 deletions server/createApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var favicon = require('serve-favicon');
var methodOverride = require('method-override');
var morgan = require('morgan');

var createComponentRouter = require('./createComponentRouter');

module.exports = function createApp (keystone, express) {

if (!keystone.app) {
Expand All @@ -28,7 +30,7 @@ module.exports = function createApp (keystone, express) {
}

// Pre static config
if ('function' === typeof keystone.get('pre:static')) {
if (typeof keystone.get('pre:static') === 'function') {
keystone.get('pre:static')(app);
}
app.use(function(req, res, next) {
Expand Down Expand Up @@ -64,7 +66,7 @@ module.exports = function createApp (keystone, express) {
}

// Pre bodyparser middleware
if ('function' === typeof keystone.get('pre:bodyparser')) {
if (typeof keystone.get('pre:bodyparser') === 'function') {
keystone.get('pre:bodyparser')(app);
}
app.use(function(req, res, next) {
Expand All @@ -80,22 +82,28 @@ module.exports = function createApp (keystone, express) {
}

// Pre route config
if ('function' === typeof keystone.get('pre:routes')) {
if (typeof keystone.get('pre:routes') === 'function') {
keystone.get('pre:routes')(app);
}
app.use(function(req, res, next) {
keystone.callHook('pre:routes', req, res, next);
});

// Configure component routes
if (keystone.get('component routes')) {
app.use('/', createComponentRouter(keystone.get('component routes')));
}

// Configure application routes
if ('function' === typeof keystone.get('routes')) {
if (typeof keystone.get('routes') === 'function') {
keystone.get('routes')(app);
}


require('./bindRedirectsHandler')(keystone, app);

// Error config
if ('function' === typeof keystone.get('pre:error')) {
if (typeof keystone.get('pre:error') === 'function') {
keystone.get('pre:error')(app);
}
app.use(function(req, res, next) {
Expand Down
29 changes: 29 additions & 0 deletions server/createComponentRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var React = require('react');
var renderToString = require('react-dom/server').renderToString;
var ReactRouter = require('react-router');

var match = ReactRouter.match;
var RoutingContext = ReactRouter.RoutingContext;

module.exports = function createComponentRouter (routes) {
return function componentRouter (req, res, next) {
match({ routes: routes, location: req.url },
function (error, redirectLocation, renderProps) {

if (error) return res.status(500).send(error.message);

if (redirectLocation) {
return res.redirect(302,
redirectLocation.pathname + redirectLocation.search);
}

if (renderProps) {
return res.render('default', {
content: renderToString(React.createElement(RoutingContext, renderProps))
});
}

next(null);
});
};
};

0 comments on commit c086cb6

Please sign in to comment.