Permalink
Browse files

Save preloaded state

  • Loading branch information...
emilong committed May 23, 2017
1 parent 317b577 commit 92635de9aeba7df0e21319a1af218ee07612c05c
Showing with 15 additions and 3 deletions.
  1. +5 −1 src/App.js
  2. +0 −1 src/Brand.js
  3. +2 −1 src/api.js
  4. +8 −0 src/savePreloadedState.js
@@ -15,10 +15,14 @@ import injectTapEventPlugin from "react-tap-event-plugin";
import Brand from "./Brand";
import BRANDS from "./brands";
import reducer from "./api";
import savePreloadedState from "./savePreloadedState";

injectTapEventPlugin();

const store = createStore(reducer, composeWithDevTools(applyMiddleware(thunk)));
const store = createStore(
reducer,
composeWithDevTools(applyMiddleware(thunk, savePreloadedState))
);

export default function App() {
return (
@@ -21,7 +21,6 @@ class Brand extends Component {
populateProducts = (props = this.props) => {
const { history, brand, hasFetched, fetchProducts } = props;

console.log({ hasFetched, brand });
if (!hasFetched) {
fetchProducts();
} else if (brand === "") {
@@ -2,9 +2,10 @@ const START_BRAND_FETCH = "makeup/api/START_BRAND_FETCH";
const STORE_BRAND_PRODUCTS = "makeup/api/STORE_BRAND_PRODUCTS";

const EMPTY_STATE = { brandsBeingFetched: [], brandProducts: {} };
const INITIAL_STATE = window.__PRELOADED_STATE__ || EMPTY_STATE;

// Reducer
export default function reducer(state = EMPTY_STATE, action = {}) {
export default function reducer(state = INITIAL_STATE, action = {}) {
const { type, brand, products } = action;
const { brandsBeingFetched = [], brandProducts = {} } = state;

@@ -0,0 +1,8 @@
export default function savePreloadedState({ getState }) {
return next => action => {
const returnValue = next(action);
window.__PRELOADED_STATE__ = getState();

return returnValue;
};
}

0 comments on commit 92635de

Please sign in to comment.