import React from 'react';
import { RoutingContext } from 'react-router';
import Fetchr from 'fetchr';
import { createLocation } from 'history';
import debugFactory from 'debug';
import { app$ } from '../../common/app';
import { RenderToString } from 'thundercats-react';

const debug = debugFactory('freecc:react-server');

// add routes here as they slowly get reactified
// remove their individual controllers
const routes = [
  '/hikes',
  '/hikes/*',
  '/jobs',
  '/jobs/*'
];

export default function reactSubRouter(app) {
  var router = app.loopback.Router();

  if (process.env.NODE_ENV === 'development') {
    routes.forEach(function(route) {
      router.get(route, serveReactApp);
    });
  }

  app.use(router);

  function serveReactApp(req, res, next) {
    const services = new Fetchr({ req });
    const location = createLocation(req.path);

    // returns a router wrapped app
    app$({ location })
      // if react-router does not find a route send down the chain
      .filter(function({ props, nextLocation }) {
        console.log('foo', nextLocation);
        if (!props) {
          debug('react tried to find %s but got 404', location.pathname);
          return next();
        }
        return !!props;
      })
      .flatMap(function({ props, AppCat }) {
        // call thundercats renderToString
        // prefetches data and sets up it up for current state
        debug('rendering to string');
        return RenderToString(
          AppCat(null, services),
          React.createElement(RoutingContext, props)
        );
      })
      // makes sure we only get one onNext and closes subscription
      .flatMap(function({ data, markup }) {
        debug('react rendered');
        const { title } = data.AppStore;
        res.expose(data, 'data');
        // now render jade file with markup injected from react
        return res.render$(
          'layout-react',
          { markup, title }
        );
      })
      .subscribe(
        function(markup) {
          debug('jade rendered');
          res.send(markup);
        },
        next
      );
  }
}

@@ -431,20 +431,19 @@
   '/
-hike
+job
 s',%0A  '/
 hike
@@ -442,33 +442,56 @@
   '/
-hikes/*',
+jobs/*'%0A%5D;%0A%0Aconst devRoutes = %5B
 %0A  '/
-job
+hike
 s',%0A  '/
 jobs
@@ -478,35 +478,36 @@
   '/hikes',%0A  '/
-job
+hike
 s/*'%0A%5D;%0A%0Aexport 
@@ -584,16 +584,127 @@
 ter();%0A%0A
+  if (process.env.BETA) %7B%0A    routes.forEach((route) =%3E %7B%0A      router.get(route, serveReactApp);%0A    %7D);%0A  %7D%0A%0A
   if (pr
@@ -739,33 +739,36 @@
 lopment') %7B%0A    
-r
+devR
 outes.forEach(fu
@@ -1149,69 +1149,13 @@
 rops
-, nextLocation %7D) %7B%0A        console.log('foo', nextLocation);
+ %7D) %7B
 %0A   

Add jobs behind beta flag