Skip to content

Commit

Permalink
feat: redux persistence and error handling (#48)
Browse files Browse the repository at this point in the history
* feat: redux persistence and error handling

* chore: update version
  • Loading branch information
Daniel Belohlavek authored and cazala committed Jul 22, 2019
1 parent 5ea19a9 commit 6013ded
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 52 deletions.
119 changes: 91 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
"@types/react-intl": "^2.3.8",
"@types/react-redux": "^6.0.4",
"@types/react-router-redux": "^5.0.14",
"@types/redux-storage": "^4.0.11",
"@types/redux-storage-engine-localstorage": "^1.1.0",
"@types/segment-analytics": "0.0.28",
"@types/uuid": "^3.4.3",
"axios": "^0.19.0",
"date-fns": "^1.29.0",
"flat": "^4.1.0",
"react-intl": "^2.4.0",
"redux-storage": "^4.1.2",
"redux-persistence": "^1.1.0",
"redux-storage-decorator-filter": "^1.1.8",
"redux-storage-engine-localstorage": "^1.1.4",
"tslint": "^5.7.0",
Expand Down
2 changes: 1 addition & 1 deletion src/modules/storage/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LOAD, SAVE } from 'redux-storage'
import { LOAD, SAVE } from 'redux-persistence'

export const STORAGE_LOAD = LOAD
export const STORAGE_SAVE = SAVE
44 changes: 25 additions & 19 deletions src/modules/storage/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Store } from 'redux'
import * as storage from 'redux-storage'
import * as storage from 'redux-persistence'
import createStorageEngine from 'redux-storage-engine-localstorage'
import filter from 'redux-storage-decorator-filter'
import { hasLocalStorage, migrateStorage } from '../../lib/localStorage'
Expand Down Expand Up @@ -54,24 +54,30 @@ export function createStorageMiddleware<T>(options: StorageMiddleware<T>) {
['storage', 'version'],
...paths
])
const storageMiddleware: any = storage.createMiddleware(
storageEngine,
[],
[
CHANGE_LOCALE,
FETCH_TRANSLATIONS_SUCCESS,
FETCH_TRANSACTION_REQUEST,
FETCH_TRANSACTION_SUCCESS,
FETCH_TRANSACTION_FAILURE,
UPDATE_TRANSACTION_STATUS,
UPDATE_TRANSACTION_NONCE,
REPLACE_TRANSACTION_SUCCESS,
FIX_REVERTED_TRANSACTION,
CLEAR_TRANSACTIONS,
CLEAR_TRANSACTION,
...actions
]
)

const whitelist = [
CHANGE_LOCALE,
FETCH_TRANSLATIONS_SUCCESS,
FETCH_TRANSACTION_REQUEST,
FETCH_TRANSACTION_SUCCESS,
FETCH_TRANSACTION_FAILURE,
UPDATE_TRANSACTION_STATUS,
UPDATE_TRANSACTION_NONCE,
REPLACE_TRANSACTION_SUCCESS,
FIX_REVERTED_TRANSACTION,
CLEAR_TRANSACTIONS,
CLEAR_TRANSACTION,
...actions
]

const storageMiddleware: any = storage.createMiddleware(storageEngine, {
filterAction: (action: any) => {
return whitelist.includes(action.type)
},
transform: options.transform,
onError: options.onError
})

const load = (store: Store<any>) => {
if (setItemFailure) {
const unsubscribe = store.subscribe(() => {
Expand Down
5 changes: 3 additions & 2 deletions src/modules/storage/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AnyAction, Reducer } from 'redux'
import * as storage from 'redux-storage'
import * as storage from 'redux-persistence'
import { StateMerger } from 'redux-persistence/dist/types'
import { STORAGE_LOAD } from './actions'

export type StorageState = {
Expand All @@ -14,7 +15,7 @@ export const INITIAL_STATE: StorageState = {

export function storageReducerWrapper(
reducer: any,
merger?: storage.StateMerger
merger?: StateMerger
): Reducer<{} | undefined> {
return storage.reducer(reducer, merger)
}
Expand Down
2 changes: 2 additions & 0 deletions src/modules/storage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export interface StorageMiddleware<T> {
paths?: (string | string[])[]
actions?: string[]
migrations?: Migrations<T>
transform?: <T>(state: T) => T
onError?: (err: Error) => void
}

0 comments on commit 6013ded

Please sign in to comment.