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

80 lines
2.9 KiB
React
Raw Normal View History

2017-02-06 14:33:34 +08:00
import React from 'react';
import { ApolloProvider } from 'react-apollo';
import { applyRouterMiddleware } from 'react-router';
import { useScroll } from 'react-router-scroll';
import { Meteor } from 'meteor/meteor';
import {
Components,
addRoute,
Routes, populateComponentsApp, populateRoutesApp, runCallbacks, initializeFragments,
2017-02-08 04:36:57 +08:00
getRenderContext,
} from 'meteor/vulcan:lib';
2017-02-06 14:33:34 +08:00
import { RouterClient } from './router.jsx';
Meteor.startup(() => {
// note: route defined here because it "shouldn't be removable"
addRoute({name:"app.notfound", path:"*", componentName: 'Error404'});
// init the application components and routes, including components & routes from 3rd-party packages
2017-06-18 10:14:34 +09:00
initializeFragments();
2017-02-06 14:33:34 +08:00
populateComponentsApp();
populateRoutesApp();
const indexRoute = _.filter(Routes, route => route.path === '/')[0];
const childRoutes = _.reject(Routes, route => route.path === '/');
const indexRouteWithoutPath = _.clone(indexRoute);
delete indexRouteWithoutPath.path; // delete path to avoid warning
2017-02-06 14:33:34 +08:00
const AppRoutes = {
path: '/',
component: Components.App,
indexRoute: indexRouteWithoutPath,
2017-02-06 14:33:34 +08:00
childRoutes,
};
const options = {
rehydrateHook(data) {
const initialState = data;
2017-02-08 04:36:57 +08:00
const context = getRenderContext();
2017-02-06 14:33:34 +08:00
context.initialState = initialState;
const apolloClientReducer = (state = {}, action) => {
if (initialState && initialState.apollo && !_.isEmpty(initialState.apollo.data) && _.isEmpty(state.data)) {
state = initialState.apollo
}
const newState = context.apolloClient.reducer()(state, action);
return newState;
}
2017-02-12 22:00:13 +08:00
context.addReducer({ apollo: apolloClientReducer });
context.store.reload();
context.store.dispatch({ type: '@@nova/INIT' }) // the first dispatch will generate a newDispatch function from middleware
runCallbacks('router.client.rehydrate', { initialState, store: context.store});
2017-02-06 14:33:34 +08:00
},
historyHook(newHistory) {
let { history } = getRenderContext();
history = runCallbacks('router.client.history', history, { newHistory });
2017-02-06 14:33:34 +08:00
return history;
},
wrapperHook(appGenerator) {
2017-02-08 04:36:57 +08:00
const { apolloClient, store } = getRenderContext();
const app = runCallbacks('router.client.wrapper', appGenerator({
2017-02-06 14:33:34 +08:00
onUpdate: () => {
2017-03-23 16:27:59 +09:00
// the first argument is an item to iterate on, needed by vulcan:lib/callbacks
2017-02-06 14:33:34 +08:00
// note: this item is not used in this specific callback: router.onUpdate
// runCallbacks('router.onUpdate', {}, store, apolloClient);
2017-02-06 14:33:34 +08:00
},
render: applyRouterMiddleware(useScroll((prevRouterProps, nextRouterProps) => {
// if the action is REPLACE, return false so that we don't jump back to top of page
return !(nextRouterProps.location.action === 'REPLACE');
}))
}));
2017-02-06 14:33:34 +08:00
return <ApolloProvider store={store} client={apolloClient}>{app}</ApolloProvider>;
},
};
RouterClient.run(AppRoutes, options);
});