mirror of
https://github.com/oceanprotocol/react.git
synced 2025-01-03 18:35:18 +01:00
added bestPrice, updated example,readme
This commit is contained in:
parent
7fa93ae5b5
commit
f9b1e7a530
14
README.md
14
README.md
@ -64,7 +64,7 @@ export default function MyComponent() {
|
|||||||
const { ocean, web3, account } = useOcean()
|
const { ocean, web3, account } = useOcean()
|
||||||
|
|
||||||
// Get metadata for this asset
|
// Get metadata for this asset
|
||||||
const { title, metadata, isLoaded, getBestPrice } = useMetadata(did)
|
const { title, metadata, bestPrice } = useMetadata(did)
|
||||||
const [price, setPrice] = useState<string>()
|
const [price, setPrice] = useState<string>()
|
||||||
|
|
||||||
// publish asset
|
// publish asset
|
||||||
@ -73,16 +73,6 @@ export default function MyComponent() {
|
|||||||
// consume asset
|
// consume asset
|
||||||
const { consume, consumeStep } = useConsume()
|
const { consume, consumeStep } = useConsume()
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
async function init(): Promise<void> {
|
|
||||||
if (isLoaded) {
|
|
||||||
const price = await getBestPrice()
|
|
||||||
setPrice(price)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
init()
|
|
||||||
}, [isLoaded])
|
|
||||||
|
|
||||||
async function handleDownload() {
|
async function handleDownload() {
|
||||||
await consume(did)
|
await consume(did)
|
||||||
}
|
}
|
||||||
@ -90,7 +80,7 @@ export default function MyComponent() {
|
|||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h1>{title}</h1>
|
<h1>{title}</h1>
|
||||||
<p>Price: {price}</p>
|
<p>Price: {bestPrice}</p>
|
||||||
|
|
||||||
<p>Your account: {account}</p>
|
<p>Your account: {account}</p>
|
||||||
<button onClick={handleDownload}>
|
<button onClick={handleDownload}>
|
||||||
|
@ -2,23 +2,12 @@ import React, { useEffect, useState } from 'react'
|
|||||||
import { useMetadata } from '@oceanprotocol/react'
|
import { useMetadata } from '@oceanprotocol/react'
|
||||||
|
|
||||||
export function MetadataExample({ did }: { did: string }) {
|
export function MetadataExample({ did }: { did: string }) {
|
||||||
const { title, isLoaded, getBestPrice } = useMetadata(did)
|
const { title, bestPrice } = useMetadata(did)
|
||||||
const [price, setPrice] = useState<string>()
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
async function init(): Promise<void> {
|
|
||||||
if (isLoaded) {
|
|
||||||
const price = await getBestPrice()
|
|
||||||
setPrice(price)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
init()
|
|
||||||
}, [isLoaded])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div>
|
<div>
|
||||||
{title} - {price}
|
{title} - {bestPrice}
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
@ -9,6 +9,7 @@ interface UseMetadata {
|
|||||||
did: DID | string
|
did: DID | string
|
||||||
metadata: Metadata
|
metadata: Metadata
|
||||||
title: string
|
title: string
|
||||||
|
bestPrice: string
|
||||||
isLoaded: boolean
|
isLoaded: boolean
|
||||||
getBestPrice: (dataTokenAddress?: string) => Promise<string>
|
getBestPrice: (dataTokenAddress?: string) => Promise<string>
|
||||||
getBestPool: (
|
getBestPool: (
|
||||||
@ -23,6 +24,7 @@ function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata {
|
|||||||
const [metadata, setMetadata] = useState<Metadata | undefined>()
|
const [metadata, setMetadata] = useState<Metadata | undefined>()
|
||||||
const [title, setTitle] = useState<string | undefined>()
|
const [title, setTitle] = useState<string | undefined>()
|
||||||
const [isLoaded, setIsLoaded] = useState(false)
|
const [isLoaded, setIsLoaded] = useState(false)
|
||||||
|
const [bestPrice, setBestPrice] = useState<string | undefined>()
|
||||||
|
|
||||||
async function getDDO(did: DID | string): Promise<DDO> {
|
async function getDDO(did: DID | string): Promise<DDO> {
|
||||||
if (status === ProviderStatus.CONNECTED) {
|
if (status === ProviderStatus.CONNECTED) {
|
||||||
@ -79,6 +81,8 @@ function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata {
|
|||||||
const metadata = await getMetadata()
|
const metadata = await getMetadata()
|
||||||
setMetadata(metadata)
|
setMetadata(metadata)
|
||||||
setTitle(metadata.main.name)
|
setTitle(metadata.main.name)
|
||||||
|
const price = await getBestPrice()
|
||||||
|
setBestPrice(price)
|
||||||
setIsLoaded(true)
|
setIsLoaded(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -89,6 +93,7 @@ function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata {
|
|||||||
did: internalDid,
|
did: internalDid,
|
||||||
metadata,
|
metadata,
|
||||||
title,
|
title,
|
||||||
|
bestPrice,
|
||||||
isLoaded,
|
isLoaded,
|
||||||
getBestPrice,
|
getBestPrice,
|
||||||
getBestPool
|
getBestPool
|
||||||
|
Loading…
Reference in New Issue
Block a user