Created
July 30, 2015 04:36
-
-
Save vicentedealencar/e176aef2ea91d6a60357 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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