Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
add persist state in localStorage. closes #467
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Feb 4, 2018
1 parent aa9e4f9 commit 9acedaf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
20 changes: 20 additions & 0 deletions app/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { basename } from 'path'

const dats = {}

const { localStorage } = window
const stat = promisify(fs.stat)

export const shareDat = key => ({ type: 'DIALOGS_LINK_OPEN', key })
Expand Down Expand Up @@ -44,7 +45,17 @@ export const addDat = ({ key, path }) => dispatch => {
}
})

dat.path = path
dats[key] = dat
localStorage.setItem(
'state',
JSON.stringify(
Object.keys(dats).map(key => ({
key,
path: dats[key].path
}))
)
)

dispatch({ type: 'ADD_DAT_SUCCESS', key })
dispatch({ type: 'DAT_WRITABLE', key, writable: dat.writable })
Expand Down Expand Up @@ -192,3 +203,12 @@ export const dropFolder = folder => async dispatch => {
if (!isDirectory) return
addDat({ path: folder.path })(dispatch)
}

export const loadFromLocalStorage = () => dispatch => {
const blob = localStorage.getItem('state')
if (!blob) return
const dats = JSON.parse(blob)
for (const dat of dats) {
addDat(dat)(dispatch)
}
}
9 changes: 4 additions & 5 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ import { createStore, applyMiddleware, compose } from 'redux'
import redatApp from './reducers'
import App from './components/app'
import logger from 'redux-logger'
// import persistState from 'redux-localstorage'
import thunk from 'redux-thunk'
import { ipcRenderer as ipc } from 'electron'
import { loadFromLocalStorage } from './actions'

const store = createStore(
redatApp,
compose(/* persistState(), */ applyMiddleware(thunk, logger))
)
const store = createStore(redatApp, compose(applyMiddleware(thunk, logger)))

render(
<Provider store={store}>
Expand All @@ -23,4 +20,6 @@ render(
document.querySelector('div')
)

store.dispatch(loadFromLocalStorage())

ipc.on('log', (_, str) => console.log(str))

0 comments on commit 9acedaf

Please sign in to comment.