2016-04-22 10:46:09 +09:00
|
|
|
import React from 'react';
|
2016-11-16 16:17:12 +01:00
|
|
|
import ApolloClient from 'apollo-client';
|
2017-01-26 13:48:47 +08:00
|
|
|
import { ApolloProvider } from 'react-apollo';
|
2017-01-18 15:09:21 +09:00
|
|
|
import { applyRouterMiddleware } from 'react-router';
|
|
|
|
import { useScroll } from 'react-router-scroll';
|
2016-10-19 10:32:24 +02:00
|
|
|
|
2017-01-26 13:48:47 +08:00
|
|
|
import { ReactRouterSSR } from 'meteor/reactrouter:react-router-ssr';
|
|
|
|
|
|
|
|
import { meteorClientConfig } from 'meteor/nova:apollo';
|
2017-01-31 10:22:02 +08:00
|
|
|
import { Components, populateComponentsApp, runCallbacks, addRoute, Routes, populateRoutesApp, store, addReducer, addMiddleware } from 'meteor/nova:core';
|
2017-01-26 13:48:47 +08:00
|
|
|
|
2016-11-21 16:18:08 +09:00
|
|
|
Meteor.startup(function initNovaRoutesAndApollo() {
|
2017-01-26 12:41:02 +08:00
|
|
|
|
2017-01-18 12:51:10 +01:00
|
|
|
// note: route defined here because it "shouldn't be removable"
|
|
|
|
addRoute({name:"app.notfound", path:"*", componentName: 'Error404'});
|
2017-01-26 12:41:02 +08:00
|
|
|
|
2017-01-18 12:51:10 +01:00
|
|
|
// uncomment for debug
|
|
|
|
// console.log('// --> starting routing');
|
2017-01-26 12:41:02 +08:00
|
|
|
|
2017-01-18 12:51:10 +01:00
|
|
|
// init the application components and routes, including components & routes from 3rd-party packages
|
2017-01-18 15:11:31 +01:00
|
|
|
populateComponentsApp();
|
|
|
|
populateRoutesApp();
|
2016-12-12 11:12:41 +09:00
|
|
|
|
|
|
|
const indexRoute = _.filter(Routes, route => route.path === '/')[0];
|
|
|
|
const childRoutes = _.reject(Routes, route => route.path === '/');
|
|
|
|
delete indexRoute.path; // delete the '/' path to avoid warning
|
2016-06-13 16:02:27 +09:00
|
|
|
|
2016-06-15 11:07:30 +09:00
|
|
|
const AppRoutes = {
|
|
|
|
path: '/',
|
2016-12-06 18:06:29 +01:00
|
|
|
component: Components.App,
|
2016-12-12 11:12:41 +09:00
|
|
|
indexRoute,
|
|
|
|
childRoutes,
|
2016-10-19 10:32:24 +02:00
|
|
|
};
|
2016-06-14 10:01:44 +09:00
|
|
|
|
2016-11-01 11:45:44 +01:00
|
|
|
/*
|
2017-01-26 13:48:47 +08:00
|
|
|
Hooks client side definition
|
2016-11-01 11:45:44 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
let history;
|
|
|
|
let initialState;
|
2016-11-16 16:17:12 +01:00
|
|
|
let client;
|
2017-01-03 16:42:53 +08:00
|
|
|
|
2016-08-17 18:13:19 +02:00
|
|
|
const clientOptions = {
|
2016-11-16 16:17:12 +01:00
|
|
|
rehydrateHook: state => {
|
2016-11-16 16:26:53 +01:00
|
|
|
// console.log('rehydrated state', state);
|
2016-11-16 16:17:12 +01:00
|
|
|
initialState = state
|
2017-01-26 13:48:47 +08:00
|
|
|
|
2017-01-04 14:31:38 +01:00
|
|
|
// configure apollo
|
2017-01-26 13:48:47 +08:00
|
|
|
client = new ApolloClient(meteorClientConfig());
|
2017-01-31 10:22:02 +08:00
|
|
|
var apolloClientReducer = (state = initialState.apollo, action) => {
|
|
|
|
return client.reducer()(state, action);
|
|
|
|
};
|
|
|
|
addReducer({apollo: apolloClientReducer});
|
|
|
|
addMiddleware(client.middleware());
|
2017-01-26 12:41:02 +08:00
|
|
|
|
2017-01-04 14:31:38 +01:00
|
|
|
// configure the redux store
|
2017-01-31 10:22:02 +08:00
|
|
|
store.reload(initialState);
|
2017-01-26 13:48:47 +08:00
|
|
|
},
|
|
|
|
historyHook(newHistory) {
|
|
|
|
// Use history hook to get a reference to the history object
|
|
|
|
history = newHistory
|
|
|
|
return history;
|
2016-11-01 11:45:44 +01:00
|
|
|
},
|
2016-08-17 18:13:19 +02:00
|
|
|
props: {
|
|
|
|
onUpdate: () => {
|
2017-01-26 09:56:53 +01:00
|
|
|
// the first argument is an item to iterate on, needed by nova:lib/callbacks
|
|
|
|
// note: this item is not used in this specific callback: router.onUpdate
|
|
|
|
runCallbacks('router.onUpdate', {}, store, client);
|
2016-10-31 17:13:14 +01:00
|
|
|
},
|
2017-01-18 15:09:21 +09:00
|
|
|
render: applyRouterMiddleware(useScroll())
|
2016-10-31 17:13:14 +01:00
|
|
|
},
|
2017-01-26 13:48:47 +08:00
|
|
|
wrapperHook(app, loginToken) {
|
|
|
|
// console.log('wrapper hook initial state', initialState);
|
|
|
|
return <ApolloProvider store={store} client={client}>{app}</ApolloProvider>
|
2016-11-16 16:17:12 +01:00
|
|
|
},
|
2016-08-17 18:13:19 +02:00
|
|
|
};
|
2017-01-03 16:42:53 +08:00
|
|
|
|
2017-01-26 13:48:47 +08:00
|
|
|
ReactRouterSSR.Run(AppRoutes, clientOptions, {});
|
2016-12-12 11:00:25 +00:00
|
|
|
});
|