mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
persist user preferences through localStorage
This commit is contained in:
parent
c867253dd9
commit
d5d9ce370b
@ -6,6 +6,7 @@ import Tabs from '../../../atoms/Tabs'
|
|||||||
import Fixed from './Fixed'
|
import Fixed from './Fixed'
|
||||||
import Dynamic from './Dynamic'
|
import Dynamic from './Dynamic'
|
||||||
import { useField } from 'formik'
|
import { useField } from 'formik'
|
||||||
|
import { useUserPreferences } from '../../../../providers/UserPreferences'
|
||||||
|
|
||||||
const query = graphql`
|
const query = graphql`
|
||||||
query PriceFieldQuery {
|
query PriceFieldQuery {
|
||||||
@ -35,6 +36,7 @@ const query = graphql`
|
|||||||
`
|
`
|
||||||
|
|
||||||
export default function Price(props: InputProps): ReactElement {
|
export default function Price(props: InputProps): ReactElement {
|
||||||
|
const { debug } = useUserPreferences()
|
||||||
const data = useStaticQuery(query)
|
const data = useStaticQuery(query)
|
||||||
const content = data.content.edges[0].node.childPagesJson.price
|
const content = data.content.edges[0].node.childPagesJson.price
|
||||||
|
|
||||||
@ -89,9 +91,11 @@ export default function Price(props: InputProps): ReactElement {
|
|||||||
return (
|
return (
|
||||||
<div className={styles.price}>
|
<div className={styles.price}>
|
||||||
<Tabs items={tabs} handleTabChange={handleTabChange} />
|
<Tabs items={tabs} handleTabChange={handleTabChange} />
|
||||||
|
{debug === true && (
|
||||||
<pre>
|
<pre>
|
||||||
<code>{JSON.stringify(field.value)}</code>
|
<code>{JSON.stringify(field.value)}</code>
|
||||||
</pre>
|
</pre>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,8 @@ import React, {
|
|||||||
useContext,
|
useContext,
|
||||||
ReactElement,
|
ReactElement,
|
||||||
ReactNode,
|
ReactNode,
|
||||||
useState
|
useState,
|
||||||
|
useEffect
|
||||||
} from 'react'
|
} from 'react'
|
||||||
|
|
||||||
declare type Currency = 'EUR' | 'USD'
|
declare type Currency = 'EUR' | 'USD'
|
||||||
@ -11,19 +12,46 @@ declare type Currency = 'EUR' | 'USD'
|
|||||||
interface UserPreferencesValue {
|
interface UserPreferencesValue {
|
||||||
debug: boolean
|
debug: boolean
|
||||||
currency: Currency
|
currency: Currency
|
||||||
setDebug: (value: boolean) => void
|
setDebug?: (value: boolean) => void
|
||||||
setCurrency: (value: string) => void
|
setCurrency?: (value: string) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
const UserPreferencesContext = createContext(null)
|
const UserPreferencesContext = createContext(null)
|
||||||
|
|
||||||
|
const localStorageKey = 'ocean-user-preferences'
|
||||||
|
|
||||||
|
function getLocalStorage() {
|
||||||
|
const storageParsed =
|
||||||
|
window && JSON.parse(window.localStorage.getItem(localStorageKey))
|
||||||
|
return storageParsed
|
||||||
|
}
|
||||||
|
|
||||||
|
function setLocalStorage(values: UserPreferencesValue) {
|
||||||
|
return (
|
||||||
|
window &&
|
||||||
|
window.localStorage.setItem(localStorageKey, JSON.stringify(values))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
function UserPreferencesProvider({
|
function UserPreferencesProvider({
|
||||||
children
|
children
|
||||||
}: {
|
}: {
|
||||||
children: ReactNode
|
children: ReactNode
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const [debug, setDebug] = useState<boolean>(false)
|
const localStorage = getLocalStorage()
|
||||||
const [currency, setCurrency] = useState<Currency>('EUR')
|
|
||||||
|
// Set default values from localStorage
|
||||||
|
const [debug, setDebug] = useState<boolean>(
|
||||||
|
(localStorage && localStorage.debug) || false
|
||||||
|
)
|
||||||
|
const [currency, setCurrency] = useState<Currency>(
|
||||||
|
(localStorage && localStorage.currency) || 'EUR'
|
||||||
|
)
|
||||||
|
|
||||||
|
// Write values to localStorage on change
|
||||||
|
useEffect(() => {
|
||||||
|
setLocalStorage({ debug, currency })
|
||||||
|
}, [debug, currency])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<UserPreferencesContext.Provider
|
<UserPreferencesContext.Provider
|
||||||
|
Loading…
x
Reference in New Issue
Block a user