2015-07-03 17:40:12 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import React from 'react';
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import Router from 'react-router';
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-24 17:52:07 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import Fetchr from 'fetchr';
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-03 17:40:12 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import Location from 'react-router/lib/Location';
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 12:01:56 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import debugFactory from 'debug';
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-02 23:44:34 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import { app$ } from '../../common/app';
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-13 00:25:01 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import { RenderToString } from 'thundercats-react';
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-24 17:52:07 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const debug = debugFactory('freecc:react-server');
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-01 15:14:10 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// add routes here as they slowly get reactified
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// remove their individual controllers
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 12:01:56 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const routes = [
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-13 11:09:34 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  '/hikes',
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  '/hikes/*',
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  '/jobs'
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 12:01:56 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								export default function reactSubRouter(app) {
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-02 23:44:34 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  var router = app.loopback.Router();
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-13 11:09:34 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  if (process.env.NODE_ENV === 'development') {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    routes.forEach(function(route) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      router.get(route, serveReactApp);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    });
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  app.use(router);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  function serveReactApp(req, res, next) {
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-24 17:52:07 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const services = new Fetchr({ req });
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-08 22:53:26 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const location = new Location(req.path, req.query);
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    // returns a router wrapped app
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-03 17:40:12 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    app$(location)
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      // if react-router does not find a route send down the chain
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-13 00:25:01 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      .filter(function({ initialState }) {
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-03 21:46:22 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if (!initialState) {
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-15 21:00:51 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          debug('react tried to find %s but got 404', location.pathname);
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-03 21:46:22 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          return next();
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        }
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-03 21:46:22 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return !!initialState;
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      })
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-13 00:25:01 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      .flatMap(function({ initialState, AppCat }) {
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        // call thundercats renderToString
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        // prefetches data and sets up it up for current state
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-14 19:33:51 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        debug('rendering to string');
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-13 00:25:01 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return RenderToString(
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-24 17:52:07 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          AppCat(null, services),
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-03 21:46:22 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          React.createElement(Router, initialState)
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-03 17:40:12 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        );
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      })
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      // makes sure we only get one onNext and closes subscription
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-03 17:40:12 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      .flatMap(function({ data, markup }) {
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        debug('react rendered');
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-29 10:16:48 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        const { title } = data.AppStore;
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-03 17:40:12 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        res.expose(data, 'data');
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        // now render jade file with markup injected from react
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-29 10:16:48 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return res.render$(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          'layout-react',
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          { markup, title }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        );
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      })
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      .subscribe(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        function(markup) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          debug('jade rendered');
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          res.send(markup);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        },
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        next
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      );
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-29 12:01:56 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}
							 |