1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-07-01 06:11:43 +02:00
market/src/components/atoms/Price/index.tsx

55 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-08-07 18:12:39 +02:00
import React, { ReactElement, useState, useEffect } from 'react'
2020-07-13 23:45:20 +02:00
import styles from './index.module.css'
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'
import PriceUnit from './PriceUnit'
2020-07-07 23:00:16 +02:00
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,
conversion,
2020-08-11 07:34:32 +02:00
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
conversion?: 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(() => {
if (!ocean || !accountId || !chainId) return
2020-08-07 18:12:39 +02:00
async function init() {
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-08-12 15:11:14 +02:00
return !ocean ? (
<div className={styles.empty}>Please connect your wallet to view price</div>
) : price ? (
<PriceUnit
price={price}
className={className}
small={small}
conversion={conversion}
/>
2020-08-07 18:12:39 +02:00
) : 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
}