2020-08-07 18:12:39 +02:00
|
|
|
import React, { ReactElement, useState, useEffect } from 'react'
|
2020-07-07 23:00:16 +02:00
|
|
|
import classNames from 'classnames/bind'
|
2020-07-13 23:45:20 +02:00
|
|
|
import PriceConversion from './Conversion'
|
|
|
|
import styles from './index.module.css'
|
2020-08-05 14:11:02 +02:00
|
|
|
import { formatCurrency } from '@coingecko/cryptoformat'
|
2020-08-10 15:08:37 +02:00
|
|
|
import { useMetadata, useOcean } from '@oceanprotocol/react'
|
2020-08-07 18:12:39 +02:00
|
|
|
import { DDO } from '@oceanprotocol/lib'
|
|
|
|
import Loader from '../Loader'
|
|
|
|
import Tooltip from '../Tooltip'
|
2020-05-07 08:03:30 +02:00
|
|
|
|
2020-07-07 23:00:16 +02:00
|
|
|
const cx = classNames.bind(styles)
|
|
|
|
|
2020-05-07 08:03:30 +02:00
|
|
|
export default function Price({
|
2020-08-07 18:12:39 +02:00
|
|
|
ddo,
|
2020-05-25 14:53:38 +02:00
|
|
|
className,
|
2020-08-11 07:34:32 +02:00
|
|
|
small,
|
|
|
|
setPriceOutside
|
2020-05-07 08:03:30 +02:00
|
|
|
}: {
|
2020-08-07 18:12:39 +02:00
|
|
|
ddo: DDO
|
2020-05-07 08:03:30 +02:00
|
|
|
className?: string
|
2020-05-25 14:53:38 +02:00
|
|
|
small?: boolean
|
2020-08-11 07:34:32 +02:00
|
|
|
setPriceOutside?: (price: string) => void
|
2020-07-07 23:00:16 +02:00
|
|
|
}): ReactElement {
|
2020-08-12 15:11:14 +02:00
|
|
|
const { ocean, chainId, accountId } = useOcean()
|
2020-08-10 15:08:37 +02:00
|
|
|
const { getBestPrice } = useMetadata()
|
2020-08-07 18:12:39 +02:00
|
|
|
const [price, setPrice] = useState<string>()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
async function init() {
|
2020-08-12 15:11:14 +02:00
|
|
|
console.log(ocean)
|
2020-08-07 18:12:39 +02:00
|
|
|
const price = await getBestPrice(ddo.dataToken)
|
|
|
|
setPrice(price)
|
2020-08-11 07:51:55 +02:00
|
|
|
setPriceOutside && price !== '' && setPriceOutside(price)
|
2020-08-07 18:12:39 +02:00
|
|
|
}
|
|
|
|
init()
|
2020-08-12 15:11:14 +02:00
|
|
|
}, [chainId, accountId, ocean])
|
2020-08-07 18:12:39 +02:00
|
|
|
|
2020-07-07 23:00:16 +02:00
|
|
|
const styleClasses = cx({
|
|
|
|
price: true,
|
|
|
|
small: small,
|
|
|
|
[className]: className
|
|
|
|
})
|
|
|
|
|
2020-05-07 08:03:30 +02:00
|
|
|
const isFree = price === '0'
|
2020-07-07 23:00:16 +02:00
|
|
|
|
2020-05-07 08:03:30 +02:00
|
|
|
const displayPrice = isFree ? (
|
|
|
|
'Free'
|
|
|
|
) : (
|
|
|
|
<>
|
2020-08-05 14:11:02 +02:00
|
|
|
<span>OCEAN</span> {formatCurrency(Number(price), '', 'en', false, true)}
|
2020-08-05 13:11:39 +02:00
|
|
|
<PriceConversion price={price} />
|
2020-05-07 08:03:30 +02:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
|
2020-08-12 15:11:14 +02:00
|
|
|
return !ocean ? (
|
|
|
|
<div className={styles.empty}>Please connect your wallet to view price</div>
|
|
|
|
) : price ? (
|
2020-08-07 18:12:39 +02:00
|
|
|
<div className={styleClasses}>{displayPrice}</div>
|
|
|
|
) : price === '' ? (
|
2020-08-07 18:20:08 +02:00
|
|
|
<div className={styles.empty}>
|
2020-08-07 18:12:39 +02:00
|
|
|
No price found{' '}
|
|
|
|
<Tooltip content="We could not find a pool for this data set, which can have multiple reasons. Is your wallet connected to the correct network?" />
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<Loader message="Retrieving price..." />
|
|
|
|
)
|
2020-05-07 08:03:30 +02:00
|
|
|
}
|