From f085ecd2efcb8e9ce738da1b3dee2938ebbbe828 Mon Sep 17 00:00:00 2001 From: mihaisc Date: Fri, 23 Oct 2020 12:57:12 +0300 Subject: [PATCH] checking previous orders in consume and compute Signed-off-by: mihaisc --- .../organisms/AssetActions/Compute.tsx | 34 ++++++++++++------- .../organisms/AssetActions/Consume.tsx | 33 +++++++++++------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/components/organisms/AssetActions/Compute.tsx b/src/components/organisms/AssetActions/Compute.tsx index efccd29cd..b791bfca0 100644 --- a/src/components/organisms/AssetActions/Compute.tsx +++ b/src/components/organisms/AssetActions/Compute.tsx @@ -1,4 +1,4 @@ -import React, { useState, ReactElement, ChangeEvent } from 'react' +import React, { useState, ReactElement, ChangeEvent, useEffect } from 'react' import { DDO, Logger } from '@oceanprotocol/lib' import Loader from '../../atoms/Loader' import Web3Feedback from '../../molecules/Wallet/Feedback' @@ -29,7 +29,7 @@ export default function Compute({ }): ReactElement { const { marketFeeAddress } = useSiteMetadata() - const { ocean, account } = useOcean() + const { ocean, accountId } = useOcean() const { compute, isLoading, computeStepText, computeError } = useCompute() const { buyDT, dtSymbol } = usePricing(ddo) @@ -45,6 +45,7 @@ export default function Compute({ const [algorithmRawCode, setAlgorithmRawCode] = useState('') const [isPublished, setIsPublished] = useState(false) const [file, setFile] = useState(null) + const [hasPreviousOrder, setHasPreviousOrder] = useState(false) const isComputeButtonDisabled = isJobStarting === true || @@ -54,14 +55,22 @@ export default function Compute({ !isBalanceSufficient const hasDatatoken = Number(dtBalance) >= 1 - const previousOrder = ocean.datatokens.getPreviousValidOrders( - ddo.dataToken, - computeService.attributes.main.cost, - computeService.index, - computeService.attributes.main.timeout, - account.getId() - ) - const hasPreviousOrder = !!previousOrder + useEffect(() => { + async function checkPreviousOrders() { + const service = ddo.findServiceByType('access') + const previousOrder = await ocean.datatokens.getPreviousValidOrders( + ddo.dataToken, + service.attributes.main.cost, + service.index, + service.attributes.main.timeout, + accountId + ) + console.log('prev ord', previousOrder, !!previousOrder) + setHasPreviousOrder(!!previousOrder) + } + checkPreviousOrders() + }, [ddo, accountId]) + const onDrop = async (files: File[]) => { setFile(files[0]) const fileText = await readFileContent(files[0]) @@ -85,7 +94,7 @@ export default function Compute({ setIsPublished(false) setError('') - !hasDatatoken && (await buyDT('1')) + !hasPreviousOrder && !hasDatatoken && (await buyDT('1')) await compute( ddo.id, @@ -96,6 +105,7 @@ export default function Compute({ marketFeeAddress ) + setHasPreviousOrder(true) setIsPublished(true) setFile(null) } catch (error) { @@ -114,7 +124,7 @@ export default function Compute({
- {hasDatatoken && ( + {hasPreviousOrder && hasDatatoken && (
You own {dtBalance} {dtSymbol} allowing you to use this data set without paying again. diff --git a/src/components/organisms/AssetActions/Consume.tsx b/src/components/organisms/AssetActions/Consume.tsx index d573f78e8..0b3b89747 100644 --- a/src/components/organisms/AssetActions/Consume.tsx +++ b/src/components/organisms/AssetActions/Consume.tsx @@ -1,4 +1,4 @@ -import React, { ReactElement, useEffect } from 'react' +import React, { ReactElement, useEffect, useState } from 'react' import { toast } from 'react-toastify' import { File as FileMetadata, DDO } from '@oceanprotocol/lib' import Button from '../../atoms/Button' @@ -21,8 +21,9 @@ export default function Consume({ isBalanceSufficient: boolean dtBalance: string }): ReactElement { - const { ocean, account } = useOcean() + const { ocean, accountId } = useOcean() const { marketFeeAddress } = useSiteMetadata() + const [hasPreviousOrder, setHasPreviousOrder] = useState(false) const { dtSymbol, buyDT, @@ -31,7 +32,6 @@ export default function Consume({ pricingIsLoading } = usePricing(ddo) const { consumeStepText, consume, consumeError } = useConsume() - const isDisabled = !ocean || !isBalanceSufficient || @@ -39,19 +39,26 @@ export default function Consume({ pricingIsLoading const hasDatatoken = Number(dtBalance) >= 1 - const service = ddo.findServiceByType('access') - const previousOrder = ocean.datatokens.getPreviousValidOrders( - ddo.dataToken, - service.attributes.main.cost, - service.index, - service.attributes.main.timeout, - account.getId() - ) - const hasPreviousOrder = !!previousOrder + useEffect(() => { + async function checkPreviousOrders() { + const service = ddo.findServiceByType('access') + const previousOrder = await ocean.datatokens.getPreviousValidOrders( + ddo.dataToken, + service.attributes.main.cost, + service.index, + service.attributes.main.timeout, + accountId + ) + console.log('prev ord', previousOrder, !!previousOrder) + setHasPreviousOrder(!!previousOrder) + } + checkPreviousOrders() + }, [ddo, accountId]) async function handleConsume() { - !hasDatatoken && (await buyDT('1')) + !hasPreviousOrder && !hasDatatoken && (await buyDT('1')) await consume(ddo.id, ddo.dataToken, 'access', marketFeeAddress) + setHasPreviousOrder(true) } // Output errors in UI