Vulcan/packages/nova-routing/lib/routing-client.jsx

80 lines
2.5 KiB
React
Raw Normal View History

2016-04-22 10:46:09 +09:00
import React from 'react';
import ApolloClient from 'apollo-client';
import { ApolloProvider } from 'react-apollo';
2017-01-18 15:09:21 +09:00
import { applyRouterMiddleware } from 'react-router';
import { useScroll } from 'react-router-scroll';
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';
Meteor.startup(function initNovaRoutesAndApollo() {
// note: route defined here because it "shouldn't be removable"
addRoute({name:"app.notfound", path:"*", componentName: 'Error404'});
// uncomment for debug
// console.log('// --> starting routing');
// init the application components and routes, including components & routes from 3rd-party packages
populateComponentsApp();
populateRoutesApp();
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
const AppRoutes = {
path: '/',
component: Components.App,
indexRoute,
childRoutes,
};
2016-06-14 10:01:44 +09:00
/*
Hooks client side definition
*/
let history;
let initialState;
let client;
const clientOptions = {
rehydrateHook: state => {
// console.log('rehydrated state', state);
initialState = state
// configure apollo
client = new ApolloClient(meteorClientConfig());
var apolloClientReducer = (state = initialState && initialState.apollo, action) => {
2017-01-31 10:22:02 +08:00
return client.reducer()(state, action);
};
addReducer({apollo: apolloClientReducer});
addMiddleware(client.middleware());
// configure the redux store
2017-01-31 11:19:43 +08:00
store.reload();
},
historyHook(newHistory) {
// Use history hook to get a reference to the history object
history = newHistory
return history;
},
props: {
onUpdate: () => {
// 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);
},
2017-01-18 15:09:21 +09:00
render: applyRouterMiddleware(useScroll())
},
wrapperHook(app, loginToken) {
// console.log('wrapper hook initial state', initialState);
return <ApolloProvider store={store} client={client}>{app}</ApolloProvider>
},
};
ReactRouterSSR.Run(AppRoutes, clientOptions, {});
2016-12-12 11:00:25 +00:00
});