mirror of
https://github.com/oceanprotocol/market.git
synced 2024-06-30 22:01:44 +02:00
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 <katunanorbert@gmai.com> Co-authored-by: Bogdan Fazakas <bogdan.fazakas@gmail.com>
This commit is contained in:
parent
3a4851132d
commit
88c94f6ce9
|
@ -363,6 +363,8 @@ export default function Compute({
|
||||||
await checkPreviousOrders(ddo)
|
await checkPreviousOrders(ddo)
|
||||||
setIsPublished(true)
|
setIsPublished(true)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
await checkPreviousOrders(selectedAlgorithmAsset)
|
||||||
|
await checkPreviousOrders(ddo)
|
||||||
setError('Failed to start job!')
|
setError('Failed to start job!')
|
||||||
Logger.error('[compute] Failed to start job: ', error.message)
|
Logger.error('[compute] Failed to start job: ', error.message)
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -53,12 +53,11 @@ export default function Consume({
|
||||||
const { isInPurgatory, price, type } = useAsset()
|
const { isInPurgatory, price, type } = useAsset()
|
||||||
const { buyDT, pricingStepText, pricingError, pricingIsLoading } =
|
const { buyDT, pricingStepText, pricingError, pricingIsLoading } =
|
||||||
usePricing()
|
usePricing()
|
||||||
const { consumeStepText, consume, consumeError } = useConsume()
|
const { consumeStepText, consume, consumeError, isLoading } = useConsume()
|
||||||
const [isDisabled, setIsDisabled] = useState(true)
|
const [isDisabled, setIsDisabled] = useState(true)
|
||||||
const [hasDatatoken, setHasDatatoken] = useState(false)
|
const [hasDatatoken, setHasDatatoken] = useState(false)
|
||||||
const [isConsumable, setIsConsumable] = useState(true)
|
const [isConsumable, setIsConsumable] = useState(true)
|
||||||
const [assetTimeout, setAssetTimeout] = useState('')
|
const [assetTimeout, setAssetTimeout] = useState('')
|
||||||
|
|
||||||
const { data } = useQuery<OrdersData>(previousOrderQuery, {
|
const { data } = useQuery<OrdersData>(previousOrderQuery, {
|
||||||
variables: {
|
variables: {
|
||||||
id: ddo.dataToken?.toLowerCase(),
|
id: ddo.dataToken?.toLowerCase(),
|
||||||
|
@ -89,7 +88,7 @@ export default function Consume({
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const { timeout } = ddo.findServiceByType('access').attributes.main
|
const { timeout } = ddo.findServiceByType('access').attributes.main
|
||||||
setAssetTimeout(secondsToString(timeout))
|
setAssetTimeout(timeout.toString())
|
||||||
}, [ddo])
|
}, [ddo])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -125,22 +124,28 @@ export default function Consume({
|
||||||
])
|
])
|
||||||
|
|
||||||
async function handleConsume() {
|
async function handleConsume() {
|
||||||
!hasPreviousOrder && !hasDatatoken && (await buyDT('1', price, ddo))
|
if (!hasPreviousOrder && !hasDatatoken) {
|
||||||
await consume(
|
const tx = await buyDT('1', price, ddo)
|
||||||
|
if (tx === undefined) return
|
||||||
|
}
|
||||||
|
const error = await consume(
|
||||||
ddo.id,
|
ddo.id,
|
||||||
ddo.dataToken,
|
ddo.dataToken,
|
||||||
'access',
|
'access',
|
||||||
appConfig.marketFeeAddress,
|
appConfig.marketFeeAddress,
|
||||||
previousOrderId
|
previousOrderId
|
||||||
)
|
)
|
||||||
setHasPreviousOrder(true)
|
error || setHasPreviousOrder(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output errors in UI
|
// Output errors in UI
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
consumeError && toast.error(consumeError)
|
consumeError && toast.error(consumeError)
|
||||||
|
}, [consumeError])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
pricingError && toast.error(pricingError)
|
pricingError && toast.error(pricingError)
|
||||||
}, [consumeError, pricingError])
|
}, [pricingError])
|
||||||
|
|
||||||
const PurchaseButton = () => (
|
const PurchaseButton = () => (
|
||||||
<ButtonBuy
|
<ButtonBuy
|
||||||
|
@ -154,7 +159,7 @@ export default function Consume({
|
||||||
assetTimeout={assetTimeout}
|
assetTimeout={assetTimeout}
|
||||||
assetType={type}
|
assetType={type}
|
||||||
stepText={consumeStepText || pricingStepText}
|
stepText={consumeStepText || pricingStepText}
|
||||||
isLoading={pricingIsLoading}
|
isLoading={pricingIsLoading || isLoading}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ interface UseConsume {
|
||||||
serviceType: ServiceType,
|
serviceType: ServiceType,
|
||||||
marketFeeAddress: string,
|
marketFeeAddress: string,
|
||||||
orderId?: string
|
orderId?: string
|
||||||
) => Promise<void>
|
) => Promise<string>
|
||||||
consumeStep?: number
|
consumeStep?: number
|
||||||
consumeStepText?: string
|
consumeStepText?: string
|
||||||
consumeError?: string
|
consumeError?: string
|
||||||
|
@ -37,7 +37,7 @@ function useConsume(): UseConsume {
|
||||||
serviceType: ServiceType = 'access',
|
serviceType: ServiceType = 'access',
|
||||||
marketFeeAddress: string,
|
marketFeeAddress: string,
|
||||||
orderId?: string
|
orderId?: string
|
||||||
): Promise<void> {
|
): Promise<string> {
|
||||||
if (!ocean || !account || !accountId) return
|
if (!ocean || !account || !accountId) return
|
||||||
|
|
||||||
setIsLoading(true)
|
setIsLoading(true)
|
||||||
|
@ -53,19 +53,25 @@ function useConsume(): UseConsume {
|
||||||
)
|
)
|
||||||
if (parseFloat(userOwnedTokens) < 1) {
|
if (parseFloat(userOwnedTokens) < 1) {
|
||||||
setConsumeError('Not enough datatokens')
|
setConsumeError('Not enough datatokens')
|
||||||
|
return 'Not enough datatokens'
|
||||||
} else {
|
} else {
|
||||||
setStep(1)
|
setStep(1)
|
||||||
orderId = await ocean.assets.order(
|
try {
|
||||||
did as string,
|
orderId = await ocean.assets.order(
|
||||||
serviceType,
|
did as string,
|
||||||
accountId,
|
serviceType,
|
||||||
undefined,
|
accountId,
|
||||||
marketFeeAddress,
|
undefined,
|
||||||
undefined,
|
marketFeeAddress,
|
||||||
false
|
undefined,
|
||||||
)
|
false
|
||||||
Logger.log('order created', orderId)
|
)
|
||||||
setStep(2)
|
Logger.log('order created', orderId)
|
||||||
|
setStep(2)
|
||||||
|
} catch (error) {
|
||||||
|
setConsumeError(error.message)
|
||||||
|
return error.message
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setStep(3)
|
setStep(3)
|
||||||
|
@ -81,11 +87,13 @@ function useConsume(): UseConsume {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setConsumeError(error.message)
|
setConsumeError(error.message)
|
||||||
Logger.error(error)
|
Logger.error(error)
|
||||||
|
return error.message
|
||||||
} finally {
|
} finally {
|
||||||
setConsumeStep(undefined)
|
setConsumeStep(undefined)
|
||||||
setConsumeStepText(undefined)
|
setConsumeStepText(undefined)
|
||||||
setIsLoading(false)
|
setIsLoading(false)
|
||||||
}
|
}
|
||||||
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
return { consume, consumeStep, consumeStepText, consumeError, isLoading }
|
return { consume, consumeStep, consumeStepText, consumeError, isLoading }
|
||||||
|
|
Loading…
Reference in New Issue
Block a user