Adds cookie permissions to localStorage/redux state

This commit is contained in:
apane 2019-11-19 16:32:54 -03:00
parent 7d887d1e60
commit a4b92ca4e1
5 changed files with 56 additions and 0 deletions

View File

@ -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'))

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,2 @@
// @flow
export const COOKIES_KEY = 'COOKIES'

View File

@ -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)