Skip to content

Commit db61cb3

Browse files
author
Balthazar Gronon
committed
persist ui reducer
1 parent c5bcb83 commit db61cb3

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"react-redux": "^5.0.5",
2828
"redux": "^3.7.1",
2929
"redux-actions": "^2.0.3",
30+
"redux-thunk": "^2.2.0",
3031
"serialize-javascript": "^1.3.0",
3132
"styled-components": "^2.2.4"
3233
},

src/actions/ui.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@
2020

2121
import { createAction } from 'redux-actions'
2222

23+
import { setStorage } from 'store'
24+
2325
export const selectTab = createAction('SELECT_TAB')
2426
export const selectItem = createAction('SELECT_ITEM')
2527
export const goto = createAction('GOTO')
26-
export const toggleActive = createAction('TOGGLE_ACTIVE')
28+
29+
export const toggleActive = () => (dispatch, getState) => {
30+
dispatch({ type: 'TOGGLE_ACTIVE' })
31+
32+
const { ui } = getState()
33+
setStorage({ ui })
34+
}

src/reducers/ui.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ export default handleActions(
5858

5959
RESET: () => initialState,
6060
},
61-
initialState
61+
initialState,
6262
)

src/store.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,28 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21-
import { createStore, compose } from 'redux'
21+
import { createStore, applyMiddleware, compose } from 'redux'
2222
import serialize from 'serialize-javascript'
23+
import thunk from 'redux-thunk'
2324
import has from 'lodash/has'
2425

2526
import reducer from 'reducers'
2627

27-
const devTools = (process.env.__BROWSER__ && window.devToolsExtension)
28-
? window.devToolsExtension()
29-
: f => f
28+
const devTools =
29+
process.env.__BROWSER__ && window.devToolsExtension ? window.devToolsExtension() : f => f
3030

3131
const hasChromeStorage = has(window, 'chrome.storage.sync.get')
3232

33-
export const getStorage = cb => hasChromeStorage
34-
? chrome.storage.sync.get(cb)
35-
: cb(JSON.parse(localStorage.getItem('state')))
33+
export const getStorage = cb =>
34+
hasChromeStorage ? chrome.storage.sync.get(cb) : cb(JSON.parse(localStorage.getItem('state')))
3635

37-
export const setStorage = (data, cb) => hasChromeStorage
38-
? chrome.storage.sync.set(data, cb)
39-
: cb(localStorage.setItem('state', serialize(data)))
36+
export const setStorage = (data, cb = f => f) =>
37+
hasChromeStorage
38+
? chrome.storage.sync.set(data, cb)
39+
: cb(localStorage.setItem('state', serialize(data)))
4040

4141
export default cb => {
42-
43-
const enhancers = compose(
44-
devTools,
45-
)
42+
const enhancers = compose(applyMiddleware(thunk), devTools)
4643

4744
getStorage(initialState => {
4845
const store = createStore(reducer, initialState || {}, enhancers)
@@ -55,5 +52,4 @@ export default cb => {
5552

5653
cb(store)
5754
})
58-
5955
}

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5464,6 +5464,10 @@ redux-actions@^2.0.3:
54645464
lodash-es "^4.17.4"
54655465
reduce-reducers "^0.1.0"
54665466

5467+
redux-thunk@^2.2.0:
5468+
version "2.2.0"
5469+
resolved "https://unpm.uberinternal.com/redux-thunk/-/redux-thunk-2.2.0.tgz#e615a16e16b47a19a515766133d1e3e99b7852e5"
5470+
54675471
redux@^3.7.1:
54685472
version "3.7.1"
54695473
resolved "https://registry.yarnpkg.com/redux/-/redux-3.7.1.tgz#bfc535c757d3849562ead0af18ac52122cd7268e"

0 commit comments

Comments
 (0)