Adds cookies utils

Replaces localStorage with cookies
Adds js-cookie
This commit is contained in:
apane 2019-12-02 10:44:29 -03:00
parent fe422cba9f
commit bc994dc355
4 changed files with 58 additions and 715 deletions

View File

@ -49,6 +49,7 @@
"history": "4.10.1", "history": "4.10.1",
"immortal-db": "^1.0.2", "immortal-db": "^1.0.2",
"immutable": "^4.0.0-rc.9", "immutable": "^4.0.0-rc.9",
"js-cookie": "^2.2.1",
"material-ui-search-bar": "^1.0.0-beta.13", "material-ui-search-bar": "^1.0.0-beta.13",
"notistack": "https://github.com/gnosis/notistack.git#v0.9.4", "notistack": "https://github.com/gnosis/notistack.git#v0.9.4",
"optimize-css-assets-webpack-plugin": "5.0.3", "optimize-css-assets-webpack-plugin": "5.0.3",

View File

@ -9,9 +9,9 @@ import Link from '~/components/layout/Link'
import { WELCOME_ADDRESS } from '~/routes/routes' import { WELCOME_ADDRESS } from '~/routes/routes'
import Button from '~/components/layout/Button' import Button from '~/components/layout/Button'
import { primary, mainFontFamily } from '~/theme/variables' import { primary, mainFontFamily } from '~/theme/variables'
import { loadFromStorage, saveToStorage } from '~/utils/storage'
import type { CookiesProps } from '~/logic/cookies/model/cookie' import type { CookiesProps } from '~/logic/cookies/model/cookie'
import { COOKIES_KEY } from '~/logic/cookies/model/cookie' import { COOKIES_KEY } from '~/logic/cookies/model/cookie'
import { loadFromCookie, saveCookie } from '~/utils/cookies'
const useStyles = makeStyles({ const useStyles = makeStyles({
container: { container: {
@ -76,7 +76,7 @@ const CookiesBanner = () => {
useEffect(() => { useEffect(() => {
async function fetchCookiesFromStorage() { async function fetchCookiesFromStorage() {
const cookiesState: CookiesProps = await loadFromStorage(COOKIES_KEY) const cookiesState: CookiesProps = await loadFromCookie(COOKIES_KEY)
if (cookiesState) { if (cookiesState) {
const { acceptedNecessary, acceptedAnalytics } = cookiesState const { acceptedNecessary, acceptedAnalytics } = cookiesState
setLocalAnalytics(acceptedAnalytics) setLocalAnalytics(acceptedAnalytics)
@ -94,7 +94,7 @@ const CookiesBanner = () => {
acceptedNecessary: true, acceptedNecessary: true,
acceptedAnalytics: true, acceptedAnalytics: true,
} }
await saveToStorage(COOKIES_KEY, newState) await saveCookie(COOKIES_KEY, newState, 365)
setShowBanner(false) setShowBanner(false)
} }
@ -103,7 +103,8 @@ const CookiesBanner = () => {
acceptedNecessary: true, acceptedNecessary: true,
acceptedAnalytics: localAnalytics, acceptedAnalytics: localAnalytics,
} }
await saveToStorage(COOKIES_KEY, newState) const expDays = localAnalytics ? 365 : 7
await saveCookie(COOKIES_KEY, newState, expDays)
setShowBanner(false) setShowBanner(false)
} }

View File

@ -0,0 +1,29 @@
// @flow
import Cookies from 'js-cookie'
import { getNetwork } from '~/config'
const PREFIX = `v1_${getNetwork()}`
export const loadFromCookie = async (key: string): Promise<*> => {
try {
const stringifiedValue = await Cookies.get(`${PREFIX}__${key}`)
if (stringifiedValue === null || stringifiedValue === undefined) {
return undefined
}
return JSON.parse(stringifiedValue)
} catch (err) {
console.error(`Failed to load ${key} from cookies:`, err)
return undefined
}
}
export const saveCookie = async (key: string, value: *, expirationDays: number): Promise<*> => {
try {
const stringifiedValue = JSON.stringify(value)
const expiration = expirationDays ? { expires: expirationDays } : undefined
await Cookies.set(`${PREFIX}__${key}`, stringifiedValue, expiration)
} catch (err) {
console.error(`Failed to save ${key} in cookies:`, err)
}
}

734
yarn.lock

File diff suppressed because it is too large Load Diff