forked from Swizec/jamcommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-browser.js
52 lines (44 loc) · 1.45 KB
/
gatsby-browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import React from 'react';
import PropTypes from 'prop-types';
import { Router } from 'react-router-dom';
import { Provider } from 'react-redux';
import { StripeProvider } from 'react-stripe-elements';
import GoCommerce from 'gocommerce-js';
import devTools from 'remote-redux-devtools';
import { routeUpdated } from './src/features/redux';
import storeFactory from './src/storeFactory.js';
exports.replaceRouterComponent = ({ history }) => {
const win = typeof window !== 'undefined' ? window : {};
// const doc = win.document ? win.document : {};
const commerce = new GoCommerce({
APIUrl: process.env.GOCOMMERCE_URI,
});
const store = storeFactory({
epicDependencies: {
commerce,
localStorage: win.localStorage,
},
enhancer: devTools({ realtime: !!localStorage.__ENABLE_DEVTOOLS }),
});
store.dispatch(routeUpdated(history.location));
history.listen(location => store.dispatch(routeUpdated(location)));
const ConnectedRouterWrapper = ({ children }) => {
const withoutStripe = (
<Router history={history}>
{children}
</Router>
);
const withStripe = (
<StripeProvider apiKey={process.env.STRIPE_API_KEY}>
{withoutStripe}
</StripeProvider>
);
return (
<Provider store={store}>
{window.Stripe ? withStripe : withoutStripe}
</Provider>
);
};
ConnectedRouterWrapper.propTypes = { children: PropTypes.any };
return ConnectedRouterWrapper;
};