Adds cookie permissions to localStorage/redux state
This commit is contained in:
parent
7d887d1e60
commit
a4b92ca4e1
|
@ -8,6 +8,7 @@ import { store } from '~/store'
|
||||||
import loadSafesFromStorage from '~/routes/safe/store/actions/loadSafesFromStorage'
|
import loadSafesFromStorage from '~/routes/safe/store/actions/loadSafesFromStorage'
|
||||||
import loadActiveTokens from '~/logic/tokens/store/actions/loadActiveTokens'
|
import loadActiveTokens from '~/logic/tokens/store/actions/loadActiveTokens'
|
||||||
import loadDefaultSafe from '~/routes/safe/store/actions/loadDefaultSafe'
|
import loadDefaultSafe from '~/routes/safe/store/actions/loadDefaultSafe'
|
||||||
|
import loadCookiesFromStorage from '~/logic/cookies/store/actions/loadCookiesFromStorage'
|
||||||
|
|
||||||
if (process.env.NODE_ENV !== 'production') {
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
|
@ -18,5 +19,6 @@ if (process.env.NODE_ENV !== 'production') {
|
||||||
store.dispatch(loadActiveTokens())
|
store.dispatch(loadActiveTokens())
|
||||||
store.dispatch(loadSafesFromStorage())
|
store.dispatch(loadSafesFromStorage())
|
||||||
store.dispatch(loadDefaultSafe())
|
store.dispatch(loadDefaultSafe())
|
||||||
|
store.dispatch(loadCookiesFromStorage())
|
||||||
|
|
||||||
ReactDOM.render(<Root />, document.getElementById('root'))
|
ReactDOM.render(<Root />, document.getElementById('root'))
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
// @flow
|
||||||
|
import type { Dispatch as ReduxDispatch } from 'redux'
|
||||||
|
import type { GlobalState } from '~/store'
|
||||||
|
import { loadFromStorage } from '~/utils/storage'
|
||||||
|
import { COOKIES_KEY } from '~/logic/cookies/utils/cookiesStorage'
|
||||||
|
import type { CookiesProps } from '~/logic/cookies/store/model/cookie'
|
||||||
|
import { setCookiesPermissions } from '~/logic/cookies/store/actions/setCookiesPermissions'
|
||||||
|
|
||||||
|
const loadCookiesFromStorage = () => async (dispatch: ReduxDispatch<GlobalState>) => {
|
||||||
|
try {
|
||||||
|
const cookies: ?{ [string]: CookiesProps } = await loadFromStorage(COOKIES_KEY)
|
||||||
|
|
||||||
|
if (cookies) {
|
||||||
|
dispatch(setCookiesPermissions(cookies))
|
||||||
|
} else {
|
||||||
|
dispatch(setCookiesPermissions({ acceptedAnalytics: false, acceptedNecessary: false }))
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
// eslint-disable-next-line
|
||||||
|
console.error('Error while getting cookies from storage:', err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve()
|
||||||
|
}
|
||||||
|
|
||||||
|
export default loadCookiesFromStorage
|
|
@ -0,0 +1,21 @@
|
||||||
|
// @flow
|
||||||
|
import type { Dispatch as ReduxDispatch } from 'redux'
|
||||||
|
import type { GlobalState } from '~/store'
|
||||||
|
import type { CookiesProps } from '~/logic/cookies/store/model/cookie'
|
||||||
|
import { setCookiesPermissions } from '~/logic/cookies/store/actions/setCookiesPermissions'
|
||||||
|
import { saveToStorage } from '~/utils/storage'
|
||||||
|
import { COOKIES_KEY } from '~/logic/cookies/utils/cookiesStorage'
|
||||||
|
|
||||||
|
const saveCookiesToStorage = (cookies: CookiesProps) => async (dispatch: ReduxDispatch<GlobalState>) => {
|
||||||
|
try {
|
||||||
|
await saveToStorage(COOKIES_KEY, cookies)
|
||||||
|
dispatch(setCookiesPermissions(cookies))
|
||||||
|
} catch (err) {
|
||||||
|
// eslint-disable-next-line
|
||||||
|
console.error('Error saving cookies to the localStorage:', err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve()
|
||||||
|
}
|
||||||
|
|
||||||
|
export default saveCookiesToStorage
|
|
@ -0,0 +1,2 @@
|
||||||
|
// @flow
|
||||||
|
export const COOKIES_KEY = 'COOKIES'
|
|
@ -19,6 +19,8 @@ import notifications, {
|
||||||
type NotificationReducerState as NotificationsState,
|
type NotificationReducerState as NotificationsState,
|
||||||
} from '~/logic/notifications/store/reducer/notifications'
|
} from '~/logic/notifications/store/reducer/notifications'
|
||||||
|
|
||||||
|
import cookies, { COOKIE_REDUCER_ID, CookieReducerState as CookiesState } from '../logic/cookies/store/reducer/cookies'
|
||||||
|
|
||||||
export const history = createBrowserHistory()
|
export const history = createBrowserHistory()
|
||||||
|
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
|
@ -33,6 +35,7 @@ export type GlobalState = {
|
||||||
tokens: TokensState,
|
tokens: TokensState,
|
||||||
transactions: TransactionsState,
|
transactions: TransactionsState,
|
||||||
notifications: NotificationsState,
|
notifications: NotificationsState,
|
||||||
|
cookies: CookiesState,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type GetState = () => GlobalState
|
export type GetState = () => GlobalState
|
||||||
|
@ -44,8 +47,10 @@ const reducers: Reducer<GlobalState> = combineReducers({
|
||||||
[TOKEN_REDUCER_ID]: tokens,
|
[TOKEN_REDUCER_ID]: tokens,
|
||||||
[TRANSACTIONS_REDUCER_ID]: transactions,
|
[TRANSACTIONS_REDUCER_ID]: transactions,
|
||||||
[NOTIFICATIONS_REDUCER_ID]: notifications,
|
[NOTIFICATIONS_REDUCER_ID]: notifications,
|
||||||
|
[COOKIE_REDUCER_ID]: cookies,
|
||||||
})
|
})
|
||||||
|
|
||||||
export const store: Store<GlobalState> = createStore(reducers, finalCreateStore)
|
export const store: Store<GlobalState> = createStore(reducers, finalCreateStore)
|
||||||
|
|
||||||
|
// eslint-disable-next-line max-len
|
||||||
export const aNewStore = (localState?: Object): Store<GlobalState> => createStore(reducers, localState, finalCreateStore)
|
export const aNewStore = (localState?: Object): Store<GlobalState> => createStore(reducers, localState, finalCreateStore)
|
||||||
|
|
Loading…
Reference in New Issue