1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-06-30 05:41:41 +02:00
market/src/components/organisms/AssetActions/Consume.tsx

72 lines
1.9 KiB
TypeScript
Raw Normal View History

import React, { ReactElement } from 'react'
2020-07-18 02:06:42 +02:00
import { toast } from 'react-toastify'
2020-07-21 13:21:22 +02:00
import { File as FileMetadata, DDO } from '@oceanprotocol/lib'
2020-07-08 17:57:53 +02:00
import compareAsBN, { Comparisson } from '../../../utils/compareAsBN'
import Button from '../../atoms/Button'
import File from '../../atoms/File'
import Price from '../../atoms/Price'
import Web3Feedback from '../../molecules/Wallet/Feedback'
2020-05-07 08:03:30 +02:00
import styles from './Consume.module.css'
2020-07-08 17:57:53 +02:00
import Loader from '../../atoms/Loader'
2020-07-21 13:21:22 +02:00
import { useOcean, useConsume } from '@oceanprotocol/react'
2020-05-07 08:03:30 +02:00
export default function Consume({
2020-07-21 13:21:22 +02:00
ddo,
2020-08-05 13:21:14 +02:00
price,
2020-07-20 12:57:26 +02:00
file
}: {
2020-07-21 13:21:22 +02:00
ddo: DDO
2020-08-05 13:21:14 +02:00
price: string // in OCEAN, not wei
2020-07-20 12:57:26 +02:00
file: FileMetadata
}): ReactElement {
2020-07-21 13:21:22 +02:00
const accessService = ddo.findServiceByType('access')
2020-07-18 02:06:42 +02:00
const { ocean } = useOcean()
const { consumeStepText, consume, consumeError } = useConsume()
2020-05-07 08:03:30 +02:00
2020-08-05 13:21:14 +02:00
const isFree = price === '0'
2020-07-18 02:06:42 +02:00
// const isBalanceSufficient =
// isFree || compareAsBN(balanceInOcean, fromWei(cost), Comparisson.gte)
const isDisabled = !ocean
if (consumeError) {
toast.error(consumeError)
}
const PurchaseButton = () =>
consumeStepText ? (
2020-07-09 13:42:06 +02:00
<Loader message={consumeStepText} />
2020-05-07 08:03:30 +02:00
) : (
<Button
style="primary"
2020-07-21 13:21:22 +02:00
onClick={() => consume(ddo.id, ddo.dataToken, 'access')}
disabled={isDisabled}
>
2020-07-21 14:04:32 +02:00
{isFree ? 'Download' : 'Buy'}
2020-05-07 08:03:30 +02:00
</Button>
)
return (
<aside className={styles.consume}>
<div className={styles.info}>
<div className={styles.filewrapper}>
<File file={file} />
</div>
<div className={styles.pricewrapper}>
2020-08-05 13:21:14 +02:00
{price ? (
2020-08-06 13:00:22 +02:00
<Price price={price} small />
) : price === '' ? (
'No price found'
2020-08-05 13:21:14 +02:00
) : (
<Loader message="Retrieving price..." />
)}
2020-05-07 08:03:30 +02:00
<PurchaseButton />
</div>
</div>
<footer className={styles.feedback}>
2020-07-18 02:06:42 +02:00
<Web3Feedback isBalanceInsufficient />
2020-05-07 08:03:30 +02:00
</footer>
</aside>
)
}