diff --git a/src/components/molecules/AssetListTitle.tsx b/src/components/molecules/AssetListTitle.tsx index 9a778c65b..15f5cdc9a 100644 --- a/src/components/molecules/AssetListTitle.tsx +++ b/src/components/molecules/AssetListTitle.tsx @@ -40,7 +40,6 @@ export default function AssetListTitle({ !ddo && did && getAssetName() return () => { - console.log('canceled?') source.cancel() } }, [assetTitle, config?.metadataCacheUri, ddo, did, title]) diff --git a/src/components/organisms/AssetActions/Consume.tsx b/src/components/organisms/AssetActions/Consume.tsx index adfbb5edd..314281951 100644 --- a/src/components/organisms/AssetActions/Consume.tsx +++ b/src/components/organisms/AssetActions/Consume.tsx @@ -12,6 +12,23 @@ import { useSiteMetadata } from '../../../hooks/useSiteMetadata' import checkPreviousOrder from '../../../utils/checkPreviousOrder' import { useAsset } from '../../../providers/Asset' import { secondsToString } from '../../../utils/metadata' +import { gql, useQuery } from '@apollo/client' +import { OrdersData } from '../../../@types/apollo/OrdersData' +import BigNumber from 'bignumber.js' + +const previousOrderQuery = gql` + query PreviousOrder($id: String!, $account: String!) { + tokenOrders( + first: 1 + where: { datatokenId: $id, payer: $account } + orderBy: timestamp + orderDirection: desc + ) { + timestamp + tx + } + } +` function getHelpText( token: { @@ -58,6 +75,34 @@ export default function Consume({ const [isConsumable, setIsConsumable] = useState(true) const [assetTimeout, setAssetTimeout] = useState('') + const { data } = useQuery(previousOrderQuery, { + variables: { + id: ddo.dataToken?.toLowerCase(), + account: accountId?.toLowerCase() + }, + pollInterval: 5000 + }) + + useEffect(() => { + if (!data || !assetTimeout || data.tokenOrders.length === 0) return + + const lastOrder = data.tokenOrders[0] + + if (assetTimeout === 'Forever') { + setPreviousOrderId(lastOrder.tx) + setHasPreviousOrder(true) + } else { + const expiry = new BigNumber(lastOrder.timestamp).plus(assetTimeout) + const unixTime = new BigNumber(Math.floor(Date.now() / 1000)) + if (unixTime.isLessThan(expiry)) { + setPreviousOrderId(lastOrder.tx) + setHasPreviousOrder(true) + } else { + setHasPreviousOrder(false) + } + } + }, [data, assetTimeout]) + useEffect(() => { const { timeout } = ddo.findServiceByType('access').attributes.main setAssetTimeout(secondsToString(timeout)) @@ -95,18 +140,6 @@ export default function Consume({ hasDatatoken ]) - useEffect(() => { - if (!ocean || !accountId) return - - async function checkOrders() { - // HEADS UP! checkPreviousOrder() also checks for expiration of possible set timeout. - const orderId = await checkPreviousOrder(ocean, accountId, ddo, 'access') - setPreviousOrderId(orderId) - setHasPreviousOrder(!!orderId) - } - checkOrders() - }, [ocean, ddo, accountId]) - async function handleConsume() { !hasPreviousOrder && !hasDatatoken && (await buyDT('1')) await consume(