mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
use browser locale for number formatting
This commit is contained in:
parent
3260df17a9
commit
88f9c47b8c
@ -20,7 +20,7 @@ export default function Conversion({
|
|||||||
const tokenId = 'ocean-protocol'
|
const tokenId = 'ocean-protocol'
|
||||||
const currencies = appConfig.currencies.join(',') // comma-separated list
|
const currencies = appConfig.currencies.join(',') // comma-separated list
|
||||||
const url = `https://api.coingecko.com/api/v3/simple/price?ids=${tokenId}&vs_currencies=${currencies}&include_24hr_change=true`
|
const url = `https://api.coingecko.com/api/v3/simple/price?ids=${tokenId}&vs_currencies=${currencies}&include_24hr_change=true`
|
||||||
const { currency } = useUserPreferences()
|
const { currency, locale } = useUserPreferences()
|
||||||
|
|
||||||
const [priceConverted, setPriceConverted] = useState('0.00')
|
const [priceConverted, setPriceConverted] = useState('0.00')
|
||||||
|
|
||||||
@ -39,11 +39,14 @@ export default function Conversion({
|
|||||||
const values = data[tokenId]
|
const values = data[tokenId]
|
||||||
const fiatValue = values[currency.toLowerCase()]
|
const fiatValue = values[currency.toLowerCase()]
|
||||||
const converted = fiatValue * Number(price)
|
const converted = fiatValue * Number(price)
|
||||||
const convertedFormatted = Number(
|
const convertedFormatted = formatCurrency(
|
||||||
formatCurrency(converted, currency, 'en', true)
|
converted,
|
||||||
).toFixed(2)
|
currency,
|
||||||
|
locale,
|
||||||
setPriceConverted(`${convertedFormatted}`)
|
true,
|
||||||
|
{ decimalPlaces: 2 }
|
||||||
|
)
|
||||||
|
setPriceConverted(convertedFormatted)
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -3,6 +3,7 @@ import { formatCurrency } from '@coingecko/cryptoformat'
|
|||||||
import classNames from 'classnames/bind'
|
import classNames from 'classnames/bind'
|
||||||
import Conversion from './Conversion'
|
import Conversion from './Conversion'
|
||||||
import styles from './PriceUnit.module.css'
|
import styles from './PriceUnit.module.css'
|
||||||
|
import { useUserPreferences } from '../../../providers/UserPreferences'
|
||||||
|
|
||||||
const cx = classNames.bind(styles)
|
const cx = classNames.bind(styles)
|
||||||
|
|
||||||
@ -19,6 +20,8 @@ export default function PriceUnit({
|
|||||||
conversion?: boolean
|
conversion?: boolean
|
||||||
symbol?: string
|
symbol?: string
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
|
const { locale } = useUserPreferences()
|
||||||
|
|
||||||
const styleClasses = cx({
|
const styleClasses = cx({
|
||||||
price: true,
|
price: true,
|
||||||
small: small,
|
small: small,
|
||||||
@ -29,7 +32,7 @@ export default function PriceUnit({
|
|||||||
<div className={styleClasses}>
|
<div className={styleClasses}>
|
||||||
{Number.isNaN(Number(price))
|
{Number.isNaN(Number(price))
|
||||||
? '-'
|
? '-'
|
||||||
: formatCurrency(Number(price), '', 'en', false, {
|
: formatCurrency(Number(price), '', locale, false, {
|
||||||
// Not exactly clear what `significant figures` are for this library,
|
// Not exactly clear what `significant figures` are for this library,
|
||||||
// but setting this seems to give us the formatting we want.
|
// but setting this seems to give us the formatting we want.
|
||||||
// See https://github.com/oceanprotocol/market/issues/70
|
// See https://github.com/oceanprotocol/market/issues/70
|
||||||
|
@ -7,9 +7,11 @@ import { getNetworkName } from '../../../utils/wallet'
|
|||||||
import { getInjectedProviderName } from 'web3modal'
|
import { getInjectedProviderName } from 'web3modal'
|
||||||
import Conversion from '../../atoms/Price/Conversion'
|
import Conversion from '../../atoms/Price/Conversion'
|
||||||
import { formatCurrency } from '@coingecko/cryptoformat'
|
import { formatCurrency } from '@coingecko/cryptoformat'
|
||||||
|
import { useUserPreferences } from '../../../providers/UserPreferences'
|
||||||
|
|
||||||
export default function Details(): ReactElement {
|
export default function Details(): ReactElement {
|
||||||
const { balance, connect, logout, chainId } = useOcean()
|
const { balance, connect, logout, chainId } = useOcean()
|
||||||
|
const { locale } = useUserPreferences()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.details}>
|
<div className={styles.details}>
|
||||||
@ -17,7 +19,7 @@ export default function Details(): ReactElement {
|
|||||||
{Object.entries(balance).map(([key, value]) => (
|
{Object.entries(balance).map(([key, value]) => (
|
||||||
<li className={styles.balance} key={key}>
|
<li className={styles.balance} key={key}>
|
||||||
<span>{key.toUpperCase()}</span>{' '}
|
<span>{key.toUpperCase()}</span>{' '}
|
||||||
{formatCurrency(Number(value), '', 'en', false, {
|
{formatCurrency(Number(value), '', locale, false, {
|
||||||
significantFigures: 4
|
significantFigures: 4
|
||||||
})}
|
})}
|
||||||
{key === 'ocean' && <Conversion price={value} />}
|
{key === 'ocean' && <Conversion price={value} />}
|
||||||
|
@ -12,22 +12,23 @@ import { LogLevel } from '@oceanprotocol/lib/dist/node/utils/Logger'
|
|||||||
interface UserPreferencesValue {
|
interface UserPreferencesValue {
|
||||||
debug: boolean
|
debug: boolean
|
||||||
currency: string
|
currency: string
|
||||||
setDebug?: (value: boolean) => void
|
locale: string
|
||||||
setCurrency?: (value: string) => void
|
setDebug: (value: boolean) => void
|
||||||
|
setCurrency: (value: string) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
const UserPreferencesContext = createContext(null)
|
const UserPreferencesContext = createContext(null)
|
||||||
|
|
||||||
const localStorageKey = 'ocean-user-preferences'
|
const localStorageKey = 'ocean-user-preferences'
|
||||||
|
|
||||||
function getLocalStorage() {
|
function getLocalStorage(): UserPreferencesValue {
|
||||||
const storageParsed =
|
const storageParsed =
|
||||||
typeof window !== 'undefined' &&
|
typeof window !== 'undefined' &&
|
||||||
JSON.parse(window.localStorage.getItem(localStorageKey))
|
JSON.parse(window.localStorage.getItem(localStorageKey))
|
||||||
return storageParsed
|
return storageParsed
|
||||||
}
|
}
|
||||||
|
|
||||||
function setLocalStorage(values: UserPreferencesValue) {
|
function setLocalStorage(values: Partial<UserPreferencesValue>) {
|
||||||
return (
|
return (
|
||||||
typeof window !== 'undefined' &&
|
typeof window !== 'undefined' &&
|
||||||
window.localStorage.setItem(localStorageKey, JSON.stringify(values))
|
window.localStorage.setItem(localStorageKey, JSON.stringify(values))
|
||||||
@ -46,22 +47,37 @@ function UserPreferencesProvider({
|
|||||||
const [currency, setCurrency] = useState<string>(
|
const [currency, setCurrency] = useState<string>(
|
||||||
localStorage?.currency || 'EUR'
|
localStorage?.currency || 'EUR'
|
||||||
)
|
)
|
||||||
|
const [locale, setLocale] = useState<string>()
|
||||||
|
|
||||||
// Write values to localStorage on change
|
// Write values to localStorage on change
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setLocalStorage({ debug, currency })
|
setLocalStorage({ debug, currency })
|
||||||
}, [debug, currency])
|
}, [debug, currency])
|
||||||
|
|
||||||
// Set ocen-lib-js log levels, default: Error
|
// Set ocean-lib-js log levels, default: Error
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
debug === true
|
debug === true
|
||||||
? Logger.setLevel(LogLevel.Verbose)
|
? Logger.setLevel(LogLevel.Verbose)
|
||||||
: Logger.setLevel(LogLevel.Error)
|
: Logger.setLevel(LogLevel.Error)
|
||||||
}, [debug])
|
}, [debug])
|
||||||
|
|
||||||
|
// Get locale always from user's browser
|
||||||
|
useEffect(() => {
|
||||||
|
if (!window) return
|
||||||
|
setLocale(window.navigator.language)
|
||||||
|
}, [])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<UserPreferencesContext.Provider
|
<UserPreferencesContext.Provider
|
||||||
value={{ debug, currency, setDebug, setCurrency } as UserPreferencesValue}
|
value={
|
||||||
|
{
|
||||||
|
debug,
|
||||||
|
currency,
|
||||||
|
locale,
|
||||||
|
setDebug,
|
||||||
|
setCurrency
|
||||||
|
} as UserPreferencesValue
|
||||||
|
}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
</UserPreferencesContext.Provider>
|
</UserPreferencesContext.Provider>
|
||||||
|
Loading…
Reference in New Issue
Block a user