import { Router } from 'express';
import { match } from 'react-router';
import createHistory from 'history/lib/createMemoryHistory';
import createRoutes from '../../common/routes';
import createPage from './page';
import createStore from '../../common/store';

const router = Router();

router.use((req, res) => {

  const history = createHistory();
  const routes = createRoutes(history);
  const location = history.createLocation(req.url);

  match({routes, location}, (error, redirectLocation, renderProps) => {

    if (error) {
      res.status(500).send(error.message);
    }

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


    if (renderProps) {

      const store = createStore();

      renderProps.components
        .filter( component => typeof component !== 'undefined' && component.fetchData )
        .map( component => component.fetchData(store.dispatch) )
        .then(() => {
          res.status(200).end(createPage(store, renderProps));
        })
    } else {
      res.status(404).send('Not Found');
    }
  });

});

export default router;

@@ -893,16 +893,28 @@
 onent =%3E
+ %7B%0A         
  compone
@@ -941,19 +941,21 @@
 ispatch)
- )%0A
+%0A    
         
@@ -964,24 +964,62 @@
 hen(() =%3E %7B%0A
+              console.log(store);%0A    
           re
@@ -1077,18 +1077,34 @@
         
-%7D)
+    %7D)%0A        %7D)%0A
 %0A    %7D e

fix not function error