Skip to content

Commit

Permalink
Save preloaded state
Browse files Browse the repository at this point in the history
  • Loading branch information
emilong committed May 23, 2017
1 parent 317b577 commit 92635de
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 0 additions & 1 deletion src/Brand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 === "") {
Expand Down
3 changes: 2 additions & 1 deletion src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 8 additions & 0 deletions src/savePreloadedState.js
Original file line number Diff line number Diff line change
@@ -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.