Add routing middleware
This commit is contained in:
@@ -1,23 +1,11 @@
|
|||||||
import createHistory from 'history/createBrowserHistory';
|
|
||||||
|
|
||||||
export const NAVIGATE = 'navigate';
|
export const NAVIGATE = 'navigate';
|
||||||
|
|
||||||
let history;
|
|
||||||
if (process.env.RENDER_MODE !== 'server') {
|
|
||||||
history = createHistory();
|
|
||||||
|
|
||||||
const unlisten = history.listen((location, action) => {
|
|
||||||
// location is an object like window.location
|
|
||||||
console.log(action, location.pathname, location.state)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const actions = {
|
const actions = {
|
||||||
navigate: (pageName) => {
|
navigate: (pageName, pushToHistory = true) => {
|
||||||
history.push(`${pageName}.html`);
|
|
||||||
return {
|
return {
|
||||||
type: NAVIGATE,
|
type: NAVIGATE,
|
||||||
pageName,
|
pageName,
|
||||||
|
pushToHistory,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
42
js/client.js
42
js/client.js
@@ -1,10 +1,38 @@
|
|||||||
import React from 'react'
|
import React from 'react';
|
||||||
import { render } from 'react-dom'
|
import { render } from 'react-dom';
|
||||||
import { createStore } from 'redux'
|
import { createStore, applyMiddleware } from 'redux';
|
||||||
import { Provider } from 'react-redux'
|
import { Provider } from 'react-redux';
|
||||||
import App from './components/App'
|
import createHistory from 'history/createBrowserHistory';
|
||||||
import appReducer from './reducer'
|
import App from './components/App';
|
||||||
|
import appReducer from './reducer';
|
||||||
|
import actions, { NAVIGATE } from './actions';
|
||||||
|
|
||||||
|
// Remove .html and map / to index
|
||||||
|
const getPageNameFromPath = (path) => {
|
||||||
|
if (path === '/') {
|
||||||
|
return 'index';
|
||||||
|
}
|
||||||
|
return path.substring(1, path.length - 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
const historyManager = store => {
|
||||||
|
const history = createHistory();
|
||||||
|
|
||||||
|
// If browser-back button is pressed, provide new pageName to store
|
||||||
|
history.listen((location, action) => {
|
||||||
|
if (action === 'POP') {
|
||||||
|
const pageName = getPageNameFromPath(location.pathname);
|
||||||
|
store.dispatch(actions.navigate(pageName, false))
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return next => action => {
|
||||||
|
if(action.type === NAVIGATE && action.pushToHistory === true) {
|
||||||
|
history.push(`${action.pageName}.html`);
|
||||||
|
}
|
||||||
|
return next(action);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Grab the state from a global variable injected into the server-generated HTML
|
// Grab the state from a global variable injected into the server-generated HTML
|
||||||
const preloadedState = window.__TECHRADAR__;
|
const preloadedState = window.__TECHRADAR__;
|
||||||
@@ -13,7 +41,7 @@ const preloadedState = window.__TECHRADAR__;
|
|||||||
delete window.__TECHRADAR__;
|
delete window.__TECHRADAR__;
|
||||||
|
|
||||||
// Create Redux store with initial state
|
// Create Redux store with initial state
|
||||||
const store = createStore(appReducer, preloadedState)
|
const store = createStore(appReducer, preloadedState, applyMiddleware(historyManager))
|
||||||
|
|
||||||
render(
|
render(
|
||||||
<Provider store={store}>
|
<Provider store={store}>
|
||||||
|
|||||||
Reference in New Issue
Block a user