From 88c94f6ce9ae9998e9015c01831a1efbb3962d4c Mon Sep 17 00:00:00 2001 From: Norbi <37236152+KatunaNorbert@users.noreply.github.com> Date: Mon, 14 Jun 2021 16:13:10 +0300 Subject: [PATCH] Fix buy asset button (#537) * fixed button turning to Download on unsuccessful buy * removed console.log * fixed multiple error message displayed bug * return if error on buying datatoken * verify orderId value * added try catch block for asset.order in consume hook * added checkPreviousOrders also when compute ends with error * bumped oceanjs lib to 0.15.0 * fixed lint errors * show loading while consume * fixed wrong timeout validation * updated package-lock from main Co-authored-by: Norbi Co-authored-by: Bogdan Fazakas --- .../organisms/AssetActions/Compute/index.tsx | 2 ++ .../organisms/AssetActions/Consume.tsx | 21 +++++++----- src/hooks/useConsume.ts | 34 ++++++++++++------- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/components/organisms/AssetActions/Compute/index.tsx b/src/components/organisms/AssetActions/Compute/index.tsx index 71a101fb5..5777559e1 100644 --- a/src/components/organisms/AssetActions/Compute/index.tsx +++ b/src/components/organisms/AssetActions/Compute/index.tsx @@ -363,6 +363,8 @@ export default function Compute({ await checkPreviousOrders(ddo) setIsPublished(true) } catch (error) { + await checkPreviousOrders(selectedAlgorithmAsset) + await checkPreviousOrders(ddo) setError('Failed to start job!') Logger.error('[compute] Failed to start job: ', error.message) } finally { diff --git a/src/components/organisms/AssetActions/Consume.tsx b/src/components/organisms/AssetActions/Consume.tsx index 9753d4c87..2918d1bdf 100644 --- a/src/components/organisms/AssetActions/Consume.tsx +++ b/src/components/organisms/AssetActions/Consume.tsx @@ -53,12 +53,11 @@ export default function Consume({ const { isInPurgatory, price, type } = useAsset() const { buyDT, pricingStepText, pricingError, pricingIsLoading } = usePricing() - const { consumeStepText, consume, consumeError } = useConsume() + const { consumeStepText, consume, consumeError, isLoading } = useConsume() const [isDisabled, setIsDisabled] = useState(true) const [hasDatatoken, setHasDatatoken] = useState(false) const [isConsumable, setIsConsumable] = useState(true) const [assetTimeout, setAssetTimeout] = useState('') - const { data } = useQuery(previousOrderQuery, { variables: { id: ddo.dataToken?.toLowerCase(), @@ -89,7 +88,7 @@ export default function Consume({ useEffect(() => { const { timeout } = ddo.findServiceByType('access').attributes.main - setAssetTimeout(secondsToString(timeout)) + setAssetTimeout(timeout.toString()) }, [ddo]) useEffect(() => { @@ -125,22 +124,28 @@ export default function Consume({ ]) async function handleConsume() { - !hasPreviousOrder && !hasDatatoken && (await buyDT('1', price, ddo)) - await consume( + if (!hasPreviousOrder && !hasDatatoken) { + const tx = await buyDT('1', price, ddo) + if (tx === undefined) return + } + const error = await consume( ddo.id, ddo.dataToken, 'access', appConfig.marketFeeAddress, previousOrderId ) - setHasPreviousOrder(true) + error || setHasPreviousOrder(true) } // Output errors in UI useEffect(() => { consumeError && toast.error(consumeError) + }, [consumeError]) + + useEffect(() => { pricingError && toast.error(pricingError) - }, [consumeError, pricingError]) + }, [pricingError]) const PurchaseButton = () => ( ) diff --git a/src/hooks/useConsume.ts b/src/hooks/useConsume.ts index 5ae2cb5da..a0b98670d 100644 --- a/src/hooks/useConsume.ts +++ b/src/hooks/useConsume.ts @@ -11,7 +11,7 @@ interface UseConsume { serviceType: ServiceType, marketFeeAddress: string, orderId?: string - ) => Promise + ) => Promise consumeStep?: number consumeStepText?: string consumeError?: string @@ -37,7 +37,7 @@ function useConsume(): UseConsume { serviceType: ServiceType = 'access', marketFeeAddress: string, orderId?: string - ): Promise { + ): Promise { if (!ocean || !account || !accountId) return setIsLoading(true) @@ -53,19 +53,25 @@ function useConsume(): UseConsume { ) if (parseFloat(userOwnedTokens) < 1) { setConsumeError('Not enough datatokens') + return 'Not enough datatokens' } else { setStep(1) - orderId = await ocean.assets.order( - did as string, - serviceType, - accountId, - undefined, - marketFeeAddress, - undefined, - false - ) - Logger.log('order created', orderId) - setStep(2) + try { + orderId = await ocean.assets.order( + did as string, + serviceType, + accountId, + undefined, + marketFeeAddress, + undefined, + false + ) + Logger.log('order created', orderId) + setStep(2) + } catch (error) { + setConsumeError(error.message) + return error.message + } } } setStep(3) @@ -81,11 +87,13 @@ function useConsume(): UseConsume { } catch (error) { setConsumeError(error.message) Logger.error(error) + return error.message } finally { setConsumeStep(undefined) setConsumeStepText(undefined) setIsLoading(false) } + return undefined } return { consume, consumeStep, consumeStepText, consumeError, isLoading }