From 95c65b74616428f8cfa7be23c0e7ca202a64f106 Mon Sep 17 00:00:00 2001 From: mihaisc Date: Fri, 23 Oct 2020 14:10:24 +0300 Subject: [PATCH] update hooks, refactor Signed-off-by: mihaisc --- package-lock.json | 6 ++-- package.json | 2 +- .../organisms/AssetActions/Compute.tsx | 19 +++++-------- .../organisms/AssetActions/Consume.tsx | 28 ++++++++++--------- src/utils/checkPreviousOrder.ts | 17 +++++++++++ 5 files changed, 43 insertions(+), 29 deletions(-) create mode 100644 src/utils/checkPreviousOrder.ts diff --git a/package-lock.json b/package-lock.json index 226b99c83..3c3c12134 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4479,9 +4479,9 @@ } }, "@oceanprotocol/react": { - "version": "0.3.5", - "resolved": "https://registry.npmjs.org/@oceanprotocol/react/-/react-0.3.5.tgz", - "integrity": "sha512-k51Mltb8bhd/n7cpcwV3knURpUXiwlryQgi6/vyd2kMNMQX6OorwHT1LcmP593FW5pIHpUEC3x/zLthYY42EiA==", + "version": "0.3.6", + "resolved": "https://registry.npmjs.org/@oceanprotocol/react/-/react-0.3.6.tgz", + "integrity": "sha512-fUnbim8d0sNta5NXpwJDixS76kCmOkyaEpTYYHZOoCHyZuesDjdBEwglbxYQl2Dtz/2lnsK2y+aYSBNYsPGiVw==", "requires": { "@oceanprotocol/lib": "^0.7.3", "axios": "^0.20.0", diff --git a/package.json b/package.json index b1e6a5e2c..926833965 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "@loadable/component": "5.13.1", "@oceanprotocol/art": "^3.0.0", "@oceanprotocol/lib": "^0.7.5", - "@oceanprotocol/react": "^0.3.5", + "@oceanprotocol/react": "^0.3.6", "@oceanprotocol/typographies": "^0.1.0", "@sindresorhus/slugify": "^1.0.0", "@tippyjs/react": "^4.2.0", diff --git a/src/components/organisms/AssetActions/Compute.tsx b/src/components/organisms/AssetActions/Compute.tsx index b791bfca0..997804206 100644 --- a/src/components/organisms/AssetActions/Compute.tsx +++ b/src/components/organisms/AssetActions/Compute.tsx @@ -17,6 +17,7 @@ import Button from '../../atoms/Button' import Input from '../../atoms/Input' import Alert from '../../atoms/Alert' import { useSiteMetadata } from '../../../hooks/useSiteMetadata' +import checkPreviousOrder from '../../../utils/checkPreviousOrder' export default function Compute({ ddo, @@ -46,7 +47,7 @@ export default function Compute({ const [isPublished, setIsPublished] = useState(false) const [file, setFile] = useState(null) const [hasPreviousOrder, setHasPreviousOrder] = useState(false) - + const [previousOrderId, setPreviousOrderId] = useState() const isComputeButtonDisabled = isJobStarting === true || file === null || @@ -57,16 +58,9 @@ export default function Compute({ 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) + const orderId = await checkPreviousOrder(ocean, accountId, ddo) + setPreviousOrderId(orderId) + setHasPreviousOrder(!!orderId) } checkPreviousOrders() }, [ddo, accountId]) @@ -102,7 +96,8 @@ export default function Compute({ ddo.dataToken, algorithmRawCode, computeContainer, - marketFeeAddress + marketFeeAddress, + previousOrderId ) setHasPreviousOrder(true) diff --git a/src/components/organisms/AssetActions/Consume.tsx b/src/components/organisms/AssetActions/Consume.tsx index 0b3b89747..420a46b65 100644 --- a/src/components/organisms/AssetActions/Consume.tsx +++ b/src/components/organisms/AssetActions/Consume.tsx @@ -9,6 +9,7 @@ import styles from './Consume.module.css' import Loader from '../../atoms/Loader' import { useOcean, useConsume, usePricing } from '@oceanprotocol/react' import { useSiteMetadata } from '../../../hooks/useSiteMetadata' +import checkPreviousOrder from '../../../utils/checkPreviousOrder' export default function Consume({ ddo, @@ -24,6 +25,8 @@ export default function Consume({ const { ocean, accountId } = useOcean() const { marketFeeAddress } = useSiteMetadata() const [hasPreviousOrder, setHasPreviousOrder] = useState(false) + const [previousOrderId, setPreviousOrderId] = useState() + const { dtSymbol, buyDT, @@ -40,24 +43,23 @@ export default function Consume({ const hasDatatoken = Number(dtBalance) >= 1 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) + async function checkOrders() { + const orderId = await checkPreviousOrder(ocean, accountId, ddo) + setPreviousOrderId(orderId) + setHasPreviousOrder(!!orderId) } - checkPreviousOrders() + checkOrders() }, [ddo, accountId]) async function handleConsume() { !hasPreviousOrder && !hasDatatoken && (await buyDT('1')) - await consume(ddo.id, ddo.dataToken, 'access', marketFeeAddress) + await consume( + ddo.id, + ddo.dataToken, + 'access', + marketFeeAddress, + previousOrderId + ) setHasPreviousOrder(true) } diff --git a/src/utils/checkPreviousOrder.ts b/src/utils/checkPreviousOrder.ts new file mode 100644 index 000000000..af27d7a97 --- /dev/null +++ b/src/utils/checkPreviousOrder.ts @@ -0,0 +1,17 @@ +import { DDO, Ocean } from '@oceanprotocol/lib' + +export default async function checkPreviousOrder( + ocean: Ocean, + accountId: string, + ddo: DDO +) { + const service = ddo.findServiceByType('access') + const previousOrder = await ocean.datatokens.getPreviousValidOrders( + ddo.dataToken, + service.attributes.main.cost, + service.index, + service.attributes.main.timeout, + accountId + ) + return previousOrder +}