1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

Merge pull request #122 from oceanprotocol/feature/previousOrders

check previousValidOrders
This commit is contained in:
mihaisc 2020-10-23 17:01:59 +03:00 committed by GitHub
commit 8635236d02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 12 deletions

View File

@ -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'
@ -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,
@ -29,7 +30,7 @@ export default function Compute({
}): ReactElement {
const { marketFeeAddress } = useSiteMetadata()
const { ocean } = useOcean()
const { ocean, accountId } = useOcean()
const { compute, isLoading, computeStepText, computeError } = useCompute()
const { buyDT, dtSymbol } = usePricing(ddo)
@ -45,7 +46,8 @@ export default function Compute({
const [algorithmRawCode, setAlgorithmRawCode] = useState('')
const [isPublished, setIsPublished] = useState(false)
const [file, setFile] = useState(null)
const [hasPreviousOrder, setHasPreviousOrder] = useState(false)
const [previousOrderId, setPreviousOrderId] = useState<string>()
const isComputeButtonDisabled =
isJobStarting === true ||
file === null ||
@ -54,6 +56,15 @@ export default function Compute({
!isBalanceSufficient
const hasDatatoken = Number(dtBalance) >= 1
useEffect(() => {
async function checkPreviousOrders() {
const orderId = await checkPreviousOrder(ocean, accountId, ddo)
setPreviousOrderId(orderId)
setHasPreviousOrder(!!orderId)
}
checkPreviousOrders()
}, [ddo, accountId])
const onDrop = async (files: File[]) => {
setFile(files[0])
const fileText = await readFileContent(files[0])
@ -77,7 +88,7 @@ export default function Compute({
setIsPublished(false)
setError('')
!hasDatatoken && (await buyDT('1'))
!hasPreviousOrder && !hasDatatoken && (await buyDT('1'))
await compute(
ddo.id,
@ -85,9 +96,11 @@ export default function Compute({
ddo.dataToken,
algorithmRawCode,
computeContainer,
marketFeeAddress
marketFeeAddress,
previousOrderId
)
setHasPreviousOrder(true)
setIsPublished(true)
setFile(null)
} catch (error) {
@ -136,7 +149,7 @@ export default function Compute({
onClick={() => startJob()}
disabled={isComputeButtonDisabled}
>
{hasDatatoken ? 'Start job' : 'Buy'}
{hasDatatoken || hasPreviousOrder ? 'Start job' : 'Buy'}
</Button>
)}
</div>

View File

@ -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'
@ -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,
@ -21,8 +22,11 @@ export default function Consume({
isBalanceSufficient: boolean
dtBalance: string
}): ReactElement {
const { ocean } = useOcean()
const { ocean, accountId } = useOcean()
const { marketFeeAddress } = useSiteMetadata()
const [hasPreviousOrder, setHasPreviousOrder] = useState(false)
const [previousOrderId, setPreviousOrderId] = useState<string>()
const {
dtSymbol,
buyDT,
@ -31,7 +35,6 @@ export default function Consume({
pricingIsLoading
} = usePricing(ddo)
const { consumeStepText, consume, consumeError } = useConsume()
const isDisabled =
!ocean ||
!isBalanceSufficient ||
@ -39,9 +42,25 @@ export default function Consume({
pricingIsLoading
const hasDatatoken = Number(dtBalance) >= 1
useEffect(() => {
async function checkOrders() {
const orderId = await checkPreviousOrder(ocean, accountId, ddo)
setPreviousOrderId(orderId)
setHasPreviousOrder(!!orderId)
}
checkOrders()
}, [ddo, accountId])
async function handleConsume() {
!hasDatatoken && (await buyDT('1'))
await consume(ddo.id, ddo.dataToken, 'access', marketFeeAddress)
!hasPreviousOrder && !hasDatatoken && (await buyDT('1'))
await consume(
ddo.id,
ddo.dataToken,
'access',
marketFeeAddress,
previousOrderId
)
setHasPreviousOrder(true)
}
// Output errors in UI
@ -56,7 +75,7 @@ export default function Consume({
<Loader message={consumeStepText || pricingStepText} />
) : (
<Button style="primary" onClick={handleConsume} disabled={isDisabled}>
{hasDatatoken ? 'Download' : 'Buy'}
{hasDatatoken || hasPreviousOrder ? 'Download' : 'Buy'}
</Button>
)}
</div>

View File

@ -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
}