From 76e14be730c9bfb1e0ff503ba9fa26ec33bf9092 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Fri, 7 Aug 2020 18:12:39 +0200 Subject: [PATCH] refactor price component --- src/components/atoms/Price/index.module.css | 1 + src/components/atoms/Price/index.tsx | 32 ++++++++++++++++--- src/components/molecules/AssetTeaser.tsx | 19 +---------- .../organisms/AssetActions/Compute.tsx | 20 +++--------- .../organisms/AssetActions/Consume.module.css | 14 ++++---- .../organisms/AssetActions/Consume.tsx | 18 ++--------- .../organisms/AssetActions/index.tsx | 19 +++-------- 7 files changed, 48 insertions(+), 75 deletions(-) diff --git a/src/components/atoms/Price/index.module.css b/src/components/atoms/Price/index.module.css index 43a071ee5..e02c4a3e6 100644 --- a/src/components/atoms/Price/index.module.css +++ b/src/components/atoms/Price/index.module.css @@ -2,6 +2,7 @@ font-weight: var(--font-weight-bold); font-size: var(--font-size-large); color: var(--brand-grey-dark); + line-height: 1; } .price span:first-child { diff --git a/src/components/atoms/Price/index.tsx b/src/components/atoms/Price/index.tsx index a25eeb7e3..1a098c00f 100644 --- a/src/components/atoms/Price/index.tsx +++ b/src/components/atoms/Price/index.tsx @@ -1,20 +1,35 @@ -import React, { ReactElement } from 'react' +import React, { ReactElement, useState, useEffect } from 'react' import classNames from 'classnames/bind' import PriceConversion from './Conversion' import styles from './index.module.css' import { formatCurrency } from '@coingecko/cryptoformat' +import { useMetadata } from '@oceanprotocol/react' +import { DDO } from '@oceanprotocol/lib' +import Loader from '../Loader' +import Tooltip from '../Tooltip' const cx = classNames.bind(styles) export default function Price({ - price, + ddo, className, small }: { - price: string // expects price in OCEAN, not wei + ddo: DDO className?: string small?: boolean }): ReactElement { + const { getBestPrice } = useMetadata(ddo.id) + const [price, setPrice] = useState() + + useEffect(() => { + async function init() { + const price = await getBestPrice(ddo.dataToken) + setPrice(price) + } + init() + }, []) + const styleClasses = cx({ price: true, small: small, @@ -32,5 +47,14 @@ export default function Price({ ) - return
{displayPrice}
+ return price ? ( +
{displayPrice}
+ ) : price === '' ? ( +
+ No price found{' '} + +
+ ) : ( + + ) } diff --git a/src/components/molecules/AssetTeaser.tsx b/src/components/molecules/AssetTeaser.tsx index c04959abe..4decaea5f 100644 --- a/src/components/molecules/AssetTeaser.tsx +++ b/src/components/molecules/AssetTeaser.tsx @@ -23,17 +23,6 @@ const AssetTeaser: React.FC = ({ const { description } = metadata.additionalInformation const isCompute = Boolean(ddo.findServiceByType('compute')) - const { getBestPrice } = useMetadata(ddo.id) - const [price, setPrice] = useState() - - useEffect(() => { - async function init() { - const price = await getBestPrice(ddo.dataToken) - setPrice(price) - } - init() - }, []) - return (
@@ -47,13 +36,7 @@ const AssetTeaser: React.FC = ({
- {price ? ( - - ) : price === '' ? ( - 'No price found' - ) : ( - - )} +
diff --git a/src/components/organisms/AssetActions/Compute.tsx b/src/components/organisms/AssetActions/Compute.tsx index 36bc91bae..de9f07b88 100644 --- a/src/components/organisms/AssetActions/Compute.tsx +++ b/src/components/organisms/AssetActions/Compute.tsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect, ReactElement } from 'react' +import React, { useState, ReactElement } from 'react' import { DDO } from '@oceanprotocol/lib' import compareAsBN, { Comparisson } from '../../../utils/compareAsBN' import Loader from '../../atoms/Loader' @@ -16,13 +16,7 @@ import Button from '../../atoms/Button' import Input from '../../atoms/Input' import Alert from '../../atoms/Alert' -export default function Compute({ - ddo, - price -}: { - ddo: DDO - price: string // in OCEAN, not wei -}): ReactElement { +export default function Compute({ ddo }: { ddo: DDO }): ReactElement { const { ocean } = useOcean() const { compute, isLoading, computeStepText, computeError } = useCompute() const computeService = ddo.findServiceByType('compute').attributes.main @@ -41,7 +35,7 @@ export default function Compute({ const [file, setFile] = useState(null) const [isTermsAgreed, setIsTermsAgreed] = useState(true) - const isFree = price === '0' + // const isFree = price === '0' const isComputeButtonDisabled = isJobStarting || @@ -101,13 +95,7 @@ export default function Compute({ return (
- {price ? ( - - ) : price === '' ? ( - 'No price found' - ) : ( - - )} +
diff --git a/src/components/organisms/AssetActions/Consume.module.css b/src/components/organisms/AssetActions/Consume.module.css index 760230d7a..73fda7fe1 100644 --- a/src/components/organisms/AssetActions/Consume.module.css +++ b/src/components/organisms/AssetActions/Consume.module.css @@ -12,16 +12,18 @@ flex-shrink: 0; } -.consume button { - margin-left: calc(var(--spacer) / 4); +.pricewrapper { + display: flex; + flex-wrap: wrap; } -.consume button:first-of-type { - margin-left: 0; +.pricewrapper button { + margin-top: calc(var(--spacer) / 2); + align-self: flex-end; } -.price { - margin-bottom: calc(var(--spacer) / 2); +.pricewrapper > div { + min-height: 30px; } .feedback { diff --git a/src/components/organisms/AssetActions/Consume.tsx b/src/components/organisms/AssetActions/Consume.tsx index 7bc722bd8..02e88cbb8 100644 --- a/src/components/organisms/AssetActions/Consume.tsx +++ b/src/components/organisms/AssetActions/Consume.tsx @@ -1,7 +1,6 @@ import React, { ReactElement } from 'react' import { toast } from 'react-toastify' import { File as FileMetadata, DDO } from '@oceanprotocol/lib' -import compareAsBN, { Comparisson } from '../../../utils/compareAsBN' import Button from '../../atoms/Button' import File from '../../atoms/File' import Price from '../../atoms/Price' @@ -12,20 +11,13 @@ import { useOcean, useConsume } from '@oceanprotocol/react' export default function Consume({ ddo, - price, file }: { ddo: DDO - price: string // in OCEAN, not wei file: FileMetadata }): ReactElement { - const accessService = ddo.findServiceByType('access') const { ocean } = useOcean() const { consumeStepText, consume, consumeError } = useConsume() - - const isFree = price === '0' - // const isBalanceSufficient = - // isFree || compareAsBN(balanceInOcean, fromWei(cost), Comparisson.gte) const isDisabled = !ocean if (consumeError) { @@ -41,7 +33,7 @@ export default function Consume({ onClick={() => consume(ddo.id, ddo.dataToken, 'access')} disabled={isDisabled} > - {isFree ? 'Download' : 'Buy'} + Buy ) @@ -52,13 +44,7 @@ export default function Consume({
- {price ? ( - - ) : price === '' ? ( - 'No price found' - ) : ( - - )} +
diff --git a/src/components/organisms/AssetActions/index.tsx b/src/components/organisms/AssetActions/index.tsx index b593b908b..c751414dd 100644 --- a/src/components/organisms/AssetActions/index.tsx +++ b/src/components/organisms/AssetActions/index.tsx @@ -1,11 +1,10 @@ -import React, { ReactElement, useState, useEffect } from 'react' +import React, { ReactElement } from 'react' import styles from './index.module.css' import Compute from './Compute' import Consume from './Consume' import { MetadataMarket } from '../../../@types/Metadata' import { DDO } from '@oceanprotocol/lib' import Tabs from '../../atoms/Tabs' -import { useMetadata } from '@oceanprotocol/react' export default function AssetActions({ metadata, @@ -14,22 +13,12 @@ export default function AssetActions({ metadata: MetadataMarket ddo: DDO }): ReactElement { - const { getBestPrice } = useMetadata(ddo.id) - const [price, setPrice] = useState() - - useEffect(() => { - async function init() { - const price = await getBestPrice(ddo.dataToken) - setPrice(price) - } - init() - }, []) - const isCompute = Boolean(ddo.findServiceByType('compute')) + const UseContent = isCompute ? ( - + ) : ( - + ) const tabs = [