From 5a8c864d6c49725a1ca6e2762cd7aadb576f3e9b Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 18 Aug 2020 19:11:52 +0200 Subject: [PATCH] refactor Price component to be more universal --- .../atoms/Price/Conversion.module.css | 1 + .../atoms/Price/PriceUnit.module.css | 19 +++++++ src/components/atoms/Price/PriceUnit.tsx | 35 ++++++++++++ src/components/atoms/Price/index.module.css | 19 ------- src/components/atoms/Price/index.tsx | 32 ++++------- .../organisms/AssetActions/Compute.tsx | 2 +- .../organisms/AssetActions/Consume.tsx | 2 +- .../AssetActions/Pool/Token.module.css | 6 +-- .../organisms/AssetActions/Pool/Token.tsx | 5 +- .../AssetActions/Pool/index.module.css | 23 ++++++-- .../organisms/AssetActions/Pool/index.tsx | 54 ++++++++----------- 11 files changed, 113 insertions(+), 85 deletions(-) create mode 100644 src/components/atoms/Price/PriceUnit.module.css create mode 100644 src/components/atoms/Price/PriceUnit.tsx diff --git a/src/components/atoms/Price/Conversion.module.css b/src/components/atoms/Price/Conversion.module.css index 21c71782d..55e9d3cfd 100644 --- a/src/components/atoms/Price/Conversion.module.css +++ b/src/components/atoms/Price/Conversion.module.css @@ -3,4 +3,5 @@ font-size: var(--font-size-small); margin-left: calc(var(--spacer) / 6); color: var(--color-secondary); + font-weight: var(--font-weight-base); } diff --git a/src/components/atoms/Price/PriceUnit.module.css b/src/components/atoms/Price/PriceUnit.module.css new file mode 100644 index 000000000..7f57dba53 --- /dev/null +++ b/src/components/atoms/Price/PriceUnit.module.css @@ -0,0 +1,19 @@ +.price { + display: inline-block; + font-weight: var(--font-weight-bold); + font-size: var(--font-size-large); + color: var(--brand-grey-dark); + line-height: 1; +} + +.price span:first-child { + font-weight: var(--font-weight-base); + color: var(--color-secondary); + font-size: var(--font-size-base); +} + +.small { + /* lazy making-conversion-smaller-with-same-markup */ + transform: scale(0.8); + transform-origin: left 80%; +} diff --git a/src/components/atoms/Price/PriceUnit.tsx b/src/components/atoms/Price/PriceUnit.tsx new file mode 100644 index 000000000..fbe424817 --- /dev/null +++ b/src/components/atoms/Price/PriceUnit.tsx @@ -0,0 +1,35 @@ +import React, { ReactElement } from 'react' +import { formatCurrency } from '@coingecko/cryptoformat' +import classNames from 'classnames/bind' +import Conversion from './Conversion' +import styles from './PriceUnit.module.css' + +const cx = classNames.bind(styles) + +export default function PriceUnit({ + price, + className, + small, + conversion, + symbol +}: { + price: string + className?: string + small?: boolean + conversion?: boolean + symbol?: string +}): ReactElement { + const styleClasses = cx({ + price: true, + small: small, + [className]: className + }) + + return ( +
+ {formatCurrency(Number(price), '', 'en', false, true)}{' '} + {symbol || 'OCEAN'} + {conversion && } +
+ ) +} diff --git a/src/components/atoms/Price/index.module.css b/src/components/atoms/Price/index.module.css index d2be93aa1..7b0cb4b4e 100644 --- a/src/components/atoms/Price/index.module.css +++ b/src/components/atoms/Price/index.module.css @@ -1,23 +1,4 @@ -.price { - font-weight: var(--font-weight-bold); - font-size: var(--font-size-large); - color: var(--brand-grey-dark); - line-height: 1; -} - -.price span:first-child { - font-weight: var(--font-weight-base); - color: var(--color-secondary); - font-size: var(--font-size-base); -} - .empty { color: var(--color-secondary); font-weight: var(--font-weight-bold); } - -.small { - /* lazy making-conversion-smaller-with-same-markup */ - transform: scale(0.8); - transform-origin: left 80%; -} diff --git a/src/components/atoms/Price/index.tsx b/src/components/atoms/Price/index.tsx index 854fc06a0..5448613d6 100644 --- a/src/components/atoms/Price/index.tsx +++ b/src/components/atoms/Price/index.tsx @@ -1,24 +1,22 @@ 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, useOcean } from '@oceanprotocol/react' import { DDO } from '@oceanprotocol/lib' import Loader from '../Loader' import Tooltip from '../Tooltip' - -const cx = classNames.bind(styles) +import PriceUnit from './PriceUnit' export default function Price({ ddo, className, small, + conversion, setPriceOutside }: { ddo: DDO className?: string small?: boolean + conversion?: boolean setPriceOutside?: (price: string) => void }): ReactElement { const { ocean, chainId, accountId } = useOcean() @@ -35,27 +33,15 @@ export default function Price({ init() }, [chainId, accountId, ocean]) - const styleClasses = cx({ - price: true, - small: small, - [className]: className - }) - - const isFree = price === '0' - - const displayPrice = isFree ? ( - 'Free' - ) : ( - <> - OCEAN {formatCurrency(Number(price), '', 'en', false, true)} - - - ) - return !ocean ? (
Please connect your wallet to view price
) : price ? ( -
{displayPrice}
+ ) : price === '' ? (
No price found{' '} diff --git a/src/components/organisms/AssetActions/Compute.tsx b/src/components/organisms/AssetActions/Compute.tsx index 0d593c6e6..ad25455f1 100644 --- a/src/components/organisms/AssetActions/Compute.tsx +++ b/src/components/organisms/AssetActions/Compute.tsx @@ -89,7 +89,7 @@ export default function Compute({ return (
- +
diff --git a/src/components/organisms/AssetActions/Consume.tsx b/src/components/organisms/AssetActions/Consume.tsx index 64018fe58..7b2bb9e50 100644 --- a/src/components/organisms/AssetActions/Consume.tsx +++ b/src/components/organisms/AssetActions/Consume.tsx @@ -49,7 +49,7 @@ export default function Consume({
- +
diff --git a/src/components/organisms/AssetActions/Pool/Token.module.css b/src/components/organisms/AssetActions/Pool/Token.module.css index 650da450b..ab5a2da2a 100644 --- a/src/components/organisms/AssetActions/Pool/Token.module.css +++ b/src/components/organisms/AssetActions/Pool/Token.module.css @@ -13,14 +13,14 @@ display: inline-block; border: 1px solid var(--brand-grey-lighter); border-radius: 50%; - padding: 0.4rem; + padding: 0.3rem; vertical-align: middle; margin-right: calc(var(--spacer) / 8); } .icon svg { - width: var(--font-size-large); - height: var(--font-size-large); + width: var(--font-size-base); + height: var(--font-size-base); } /* Data Token Icon Style */ diff --git a/src/components/organisms/AssetActions/Pool/Token.tsx b/src/components/organisms/AssetActions/Pool/Token.tsx index 82d825daa..920e79acb 100644 --- a/src/components/organisms/AssetActions/Pool/Token.tsx +++ b/src/components/organisms/AssetActions/Pool/Token.tsx @@ -1,7 +1,7 @@ import React, { ReactElement } from 'react' import styles from './Token.module.css' import { ReactComponent as Logo } from '../../../../images/logo.svg' -import { formatCurrency } from '@coingecko/cryptoformat' +import PriceUnit from '../../../atoms/Price/PriceUnit' export default function Token({ symbol, @@ -15,8 +15,7 @@ export default function Token({
- {formatCurrency(Number(balance), '', 'en', false, true)}{' '} - {symbol} +
) } diff --git a/src/components/organisms/AssetActions/Pool/index.module.css b/src/components/organisms/AssetActions/Pool/index.module.css index 20a98f861..6ab0d5a4c 100644 --- a/src/components/organisms/AssetActions/Pool/index.module.css +++ b/src/components/organisms/AssetActions/Pool/index.module.css @@ -1,8 +1,25 @@ -.title { +.dataToken { + margin-bottom: var(--spacer); + padding-bottom: calc(var(--spacer) / 1.5); font-size: var(--font-size-large); - margin-bottom: calc(var(--spacer) / 4); + border-bottom: 1px solid var(--brand-grey-lighter); + margin-left: -2rem; + margin-right: -2rem; + padding-left: var(--spacer); + padding-right: var(--spacer); + text-align: center; +} + +.poolTokens { + display: grid; + gap: var(--spacer); + grid-template-columns: 1fr 1fr; +} + +.title { + font-size: var(--font-size-base); + margin-bottom: calc(var(--spacer) / 1.5); } .tokens { - margin-bottom: var(--spacer); } diff --git a/src/components/organisms/AssetActions/Pool/index.tsx b/src/components/organisms/AssetActions/Pool/index.tsx index 5015a75fd..4208fd7a2 100644 --- a/src/components/organisms/AssetActions/Pool/index.tsx +++ b/src/components/organisms/AssetActions/Pool/index.tsx @@ -1,9 +1,9 @@ import React, { ReactElement, useEffect, useState } from 'react' import { useOcean, useMetadata } from '@oceanprotocol/react' import { DDO } from '@oceanprotocol/lib' -import { formatCurrency } from '@coingecko/cryptoformat' import styles from './index.module.css' import Token from './Token' +import PriceUnit from '../../../atoms/Price/PriceUnit' interface Balance { ocean: string @@ -14,7 +14,6 @@ export default function Pool({ ddo }: { ddo: DDO }): ReactElement { const { ocean, accountId } = useOcean() const { getBestPool } = useMetadata() const [totalBalance, setTotalBalance] = useState() - const [poolPrice, setPoolPrice] = useState() const [dtPrice, setDtPrice] = useState() const [dtSymbol, setDtSymbol] = useState() const [userBalance, setUserBalance] = useState() @@ -22,8 +21,7 @@ export default function Pool({ ddo }: { ddo: DDO }): ReactElement { useEffect(() => { async function init() { try { - const { poolAddress, poolPrice } = await getBestPool(ddo.dataToken) - setPoolPrice(poolPrice) + const { poolAddress } = await getBestPool(ddo.dataToken) const dtSymbol = await ocean.datatokens.getSymbol( ddo.dataToken, @@ -64,36 +62,28 @@ export default function Pool({ ddo }: { ddo: DDO }): ReactElement { return ( <> -

Your Pooled Tokens

- {userBalance && ( -
- - -
- )} +
+ ={' '} + +
-

Total Pooled Tokens

- {totalBalance && ( -
- - -
- )} +
+ {userBalance && ( +
+

Your Pooled Tokens

+ + +
+ )} - {poolPrice && ( -

- Pool Price:
- OCEAN{' '} - {formatCurrency(Number(poolPrice), '', 'en', false, true)} -

- )} - {dtPrice && ( -

- Data Token Price:
- OCEAN{' '} - {formatCurrency(Number(dtPrice), '', 'en', false, true)} -

- )} + {totalBalance && ( +
+

Total Pooled Tokens

+ + +
+ )} +
) }