Skip to content

Instantly share code, notes, and snippets.

@vicentedealencar
Created July 30, 2015 04:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vicentedealencar/e176aef2ea91d6a60357 to your computer and use it in GitHub Desktop.
Save vicentedealencar/e176aef2ea91d6a60357 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import CheckoutContainer from './containers/CheckoutContainer';
import { createStore, applyMiddleware, combineReducers } from 'redux';
import thunk from 'redux-thunk';
import { Provider } from 'react-redux';
import * as reducers from './reducers';
import { pouchMiddleware, getState } from './redux-pouchdb';
getState().then(initialState =>{
const createStoreWithMiddleware = applyMiddleware(thunk, pouchMiddleware)(createStore);
const reducer = combineReducers(reducers);
const store = createStoreWithMiddleware(reducer, initialState);
const element = (
<Provider store={store}>
{() => <CheckoutContainer />}
</Provider>);
React.render(element, document.body);
});
import PouchDB from 'pouchdb';
var db = new PouchDB('app');
const docId = 'redux-store';
const emptyDoc = {_id: docId};
export const getState = () => {
return db.get(docId).catch(error => {
return db.put(emptyDoc).then(() => emptyDoc);
});
}
export const pouchMiddleware = ({ dispatch, getState }) => {
return next => action => {
const ret = next(action);
db.get(docId).then(doc => {
db.put({
...doc,
...getState()
}).catch(error => {
console.error(error);
});
});
return ret;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment