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

Fix issues consuming free assets (#834)

* get order history based on ddo chainId

* disabled consume when not connected to wallet, refetch orders on connect

* disable create price button if not assetNetwork

* get previous orders on order action
This commit is contained in:
Norbi 2021-09-13 17:14:43 +03:00 committed by GitHub
parent e19c918ed5
commit 421d5981e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 9 deletions

View File

@ -23,6 +23,7 @@ export default function Alert({
action?: { action?: {
name: string name: string
style?: 'text' | 'primary' | 'ghost' style?: 'text' | 'primary' | 'ghost'
disabled?: boolean
handleAction: (e: FormEvent<HTMLButtonElement>) => void handleAction: (e: FormEvent<HTMLButtonElement>) => void
} }
onDismiss?: () => void onDismiss?: () => void
@ -48,6 +49,7 @@ export default function Alert({
size="small" size="small"
style={action.style || 'primary'} style={action.style || 'primary'}
onClick={action.handleAction} onClick={action.handleAction}
disabled={action.disabled}
> >
{action.name} {action.name}
</Button> </Button>

View File

@ -5,7 +5,8 @@ import File from '../../atoms/File'
import Price from '../../atoms/Price' import Price from '../../atoms/Price'
import { useSiteMetadata } from '../../../hooks/useSiteMetadata' import { useSiteMetadata } from '../../../hooks/useSiteMetadata'
import { useAsset } from '../../../providers/Asset' import { useAsset } from '../../../providers/Asset'
import { gql, useQuery } from 'urql' import { gql } from 'urql'
import { fetchData, getQueryContext } from '../../../utils/subgraph'
import { OrdersData } from '../../../@types/apollo/OrdersData' import { OrdersData } from '../../../@types/apollo/OrdersData'
import BigNumber from 'bignumber.js' import BigNumber from 'bignumber.js'
import { useOcean } from '../../../providers/Ocean' import { useOcean } from '../../../providers/Ocean'
@ -63,15 +64,19 @@ export default function Consume({
const [maxDt, setMaxDT] = useState<number>(1) const [maxDt, setMaxDT] = useState<number>(1)
const [isConsumablePrice, setIsConsumablePrice] = useState(true) const [isConsumablePrice, setIsConsumablePrice] = useState(true)
const [assetTimeout, setAssetTimeout] = useState('') const [assetTimeout, setAssetTimeout] = useState('')
const [result] = useQuery<OrdersData>({ const [data, setData] = useState<OrdersData>()
query: previousOrderQuery,
variables: { useEffect(() => {
if (!ddo || !accountId) return
const context = getQueryContext(ddo.chainId)
const variables = {
id: ddo.dataToken?.toLowerCase(), id: ddo.dataToken?.toLowerCase(),
account: accountId?.toLowerCase() account: accountId?.toLowerCase()
} }
// pollInterval: 5000 fetchData(previousOrderQuery, variables, context).then((result: any) => {
}) setData(result.data)
const { data } = result })
}, [ddo, accountId, hasPreviousOrder])
async function checkMaxAvaialableTokens(price: BestPrice) { async function checkMaxAvaialableTokens(price: BestPrice) {
if (!ocean || !price) return if (!ocean || !price) return
@ -83,7 +88,8 @@ export default function Consume({
} }
useEffect(() => { useEffect(() => {
if (!data || !assetTimeout || data.tokenOrders.length === 0) return if (!data || !assetTimeout || data.tokenOrders.length === 0 || !accountId)
return
const lastOrder = data.tokenOrders[0] const lastOrder = data.tokenOrders[0]
if (assetTimeout === '0') { if (assetTimeout === '0') {
@ -99,7 +105,7 @@ export default function Consume({
setHasPreviousOrder(false) setHasPreviousOrder(false)
} }
} }
}, [data, assetTimeout]) }, [data, assetTimeout, accountId])
useEffect(() => { useEffect(() => {
const { timeout } = ddo.findServiceByType('access').attributes.main const { timeout } = ddo.findServiceByType('access').attributes.main
@ -120,6 +126,7 @@ export default function Consume({
}, [dtBalance]) }, [dtBalance])
useEffect(() => { useEffect(() => {
if (!accountId) return
setIsDisabled( setIsDisabled(
!isConsumable || !isConsumable ||
((!ocean || ((!ocean ||
@ -141,6 +148,7 @@ export default function Consume({
pricingIsLoading, pricingIsLoading,
isConsumablePrice, isConsumablePrice,
hasDatatoken, hasDatatoken,
accountId,
isConsumable isConsumable
]) ])

View File

@ -10,6 +10,7 @@ import Feedback from './Feedback'
import { graphql, useStaticQuery } from 'gatsby' import { graphql, useStaticQuery } from 'gatsby'
import { usePricing } from '../../../../hooks/usePricing' import { usePricing } from '../../../../hooks/usePricing'
import styles from './index.module.css' import styles from './index.module.css'
import { useAsset } from '../../../../providers/Asset'
const query = graphql` const query = graphql`
query PricingQuery { query PricingQuery {
@ -67,6 +68,7 @@ export default function Pricing({ ddo }: { ddo: DDO }): ReactElement {
const { createPricing, pricingIsLoading, pricingError, pricingStepText } = const { createPricing, pricingIsLoading, pricingError, pricingStepText } =
usePricing() usePricing()
const { isAssetNetwork } = useAsset()
const hasFeedback = pricingIsLoading || typeof success !== 'undefined' const hasFeedback = pricingIsLoading || typeof success !== 'undefined'
@ -133,6 +135,7 @@ export default function Pricing({ ddo }: { ddo: DDO }): ReactElement {
text={content.empty.info} text={content.empty.info}
action={{ action={{
name: content.empty.action.name, name: content.empty.action.name,
disabled: !isAssetNetwork,
handleAction: handleShowPricingForm handleAction: handleShowPricingForm
}} }}
/> />