refactor ddo,did with asset

This commit is contained in:
mihaisc 2020-08-20 16:11:16 +03:00
parent 34a485b930
commit 15ded202ae
4 changed files with 24 additions and 13 deletions

View File

@ -33,7 +33,7 @@ export function AllDdos() {
{ddos?.map((ddo) => {
return (
<div key={shortid.generate()}>
<MetadataExample did={ddo.id} />
<MetadataExample ddo={ddo} />
<br />
</div>
)

View File

@ -1,13 +1,14 @@
import React, { useEffect, useState } from 'react'
import { useMetadata } from '@oceanprotocol/react'
import { DDO } from '@oceanprotocol/lib'
export function MetadataExample({ did }: { did: string }) {
const { title, pool } = useMetadata(did)
export function MetadataExample({ ddo }: { ddo: DDO }) {
const { title, poolAddress, price } = useMetadata(ddo)
return (
<>
<div>
{title} - {pool && pool.price + ' - ' + pool.address}
{title} - price = {price} // pool = {poolAddress}
</div>
</>
)

View File

@ -4,6 +4,7 @@ import { useOcean } from '../../providers'
import ProviderStatus from '../../providers/OceanProvider/ProviderStatus'
import { getBestDataTokenPrice, getCheapestPool } from '../../utils/dtUtils'
import Pool from './Pool'
import { isDDO } from '../../utils'
interface UseMetadata {
ddo: DDO
@ -17,7 +18,7 @@ interface UseMetadata {
getPool: (dataTokenAddress?: string) => Promise<Pool>
}
function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata {
function useMetadata(asset?: DID | string | DDO): UseMetadata {
const { ocean, status, config, accountId } = useOcean()
const [internalDdo, setDDO] = useState<DDO | undefined>()
const [internalDid, setDID] = useState<DID | string | undefined>()
@ -58,16 +59,16 @@ function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata {
async function init(): Promise<void> {
Logger.debug('meta init', status)
if (ocean && status === ProviderStatus.CONNECTED) {
if (ddo) {
setDDO(ddo)
setDID(ddo.id)
}
Logger.debug('meta init', did)
if (did && !ddo) {
const ddo = await getDDO(did)
if (!asset) return
if (isDDO(asset)) {
setDDO(asset)
setDID(asset.id)
} else {
const ddo = await getDDO(asset)
Logger.debug('DDO', ddo)
setDDO(ddo)
setDID(did)
setDID(asset)
}
}
}

View File

@ -1,3 +1,5 @@
import { DDO, DID } from '@oceanprotocol/lib'
export function readFileContent(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader()
@ -12,6 +14,13 @@ export function readFileContent(file: File): Promise<string> {
})
}
export function isDDO(toBeDetermined): toBeDetermined is DDO {
if ((toBeDetermined as DDO).id) {
return true
}
return false
}
export const feedback: { [key in number]: string } = {
99: 'Decrypting file URL...',
0: '1/3 Looking for data token. Buying if none found...',