1
0
mirror of https://github.com/oceanprotocol/react.git synced 2025-02-14 21:10:38 +01:00

useMetadata refactor for strict type checking

This commit is contained in:
Matthias Kretschmann 2020-09-23 21:06:56 +02:00
parent d8107d6686
commit f5724c553d
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 71 additions and 59 deletions

View File

@ -13,7 +13,7 @@ interface UseCompute {
dataTokenAddress: string, dataTokenAddress: string,
algorithmRawCode: string, algorithmRawCode: string,
computeContainer: ComputeValue computeContainer: ComputeValue
) => Promise<ComputeJob> ) => Promise<ComputeJob | void>
computeStep?: number computeStep?: number
computeStepText?: string computeStepText?: string
computeError?: string computeError?: string
@ -44,7 +44,13 @@ function useCompute(): UseCompute {
const [computeError, setComputeError] = useState<string | undefined>() const [computeError, setComputeError] = useState<string | undefined>()
const [isLoading, setIsLoading] = useState(false) const [isLoading, setIsLoading] = useState(false)
function setStep(index: number) { function setStep(index?: number) {
if (!index) {
setComputeStep(undefined)
setComputeStepText(undefined)
return
}
setComputeStep(index) setComputeStep(index)
setComputeStepText(computeFeedback[index]) setComputeStepText(computeFeedback[index])
} }
@ -55,7 +61,7 @@ function useCompute(): UseCompute {
dataTokenAddress: string, dataTokenAddress: string,
algorithmRawCode: string, algorithmRawCode: string,
computeContainer: ComputeValue computeContainer: ComputeValue
): Promise<ComputeJob> { ): Promise<ComputeJob | void> {
if (!ocean || !account) return if (!ocean || !account) return
setComputeError(undefined) setComputeError(undefined)

View File

@ -7,89 +7,100 @@ import { isDDO } from '../../utils'
import BestPrice from './BestPrice' import BestPrice from './BestPrice'
interface UseMetadata { interface UseMetadata {
ddo: DDO ddo: DDO | undefined
did: DID | string did: DID | string | undefined
metadata: Metadata metadata: Metadata | undefined
title: string title: string | undefined
price: BestPrice price: BestPrice | undefined
isLoaded: boolean isLoaded: boolean
getPrice: (dataTokenAddress?: string) => Promise<BestPrice> getPrice: (dataTokenAddress: string) => Promise<BestPrice | void>
} }
function useMetadata(asset?: DID | string | DDO): UseMetadata { function useMetadata(asset?: DID | string | DDO): UseMetadata {
const { ocean, status, accountId } = useOcean() const { ocean, status, accountId, chainId } = useOcean()
const [internalDdo, setDDO] = useState<DDO | undefined>() const [internalDdo, setDDO] = useState<DDO>()
const [internalDid, setDID] = useState<DID | string | undefined>() const [internalDid, setDID] = useState<DID | string>()
const [metadata, setMetadata] = useState<Metadata | undefined>() const [metadata, setMetadata] = useState<Metadata>()
const [title, setTitle] = useState<string | undefined>() const [title, setTitle] = useState<string>()
const [isLoaded, setIsLoaded] = useState(false) const [isLoaded, setIsLoaded] = useState(false)
const [price, setPrice] = useState<BestPrice | undefined>() const [price, setPrice] = useState<BestPrice>()
const getDDO = useCallback( const getDDO = useCallback(
async (did: DID | string): Promise<DDO | null> => { async (did: DID | string): Promise<DDO> => {
if (status !== ProviderStatus.CONNECTED) return null
const ddo = await ocean.metadatastore.retrieveDDO(did) const ddo = await ocean.metadatastore.retrieveDDO(did)
return ddo return ddo
}, },
[ocean?.metadatastore, status] [ocean?.metadatastore]
) )
const getPrice = useCallback( const getPrice = useCallback(
async (dataTokenAddress?: string): Promise<BestPrice> => { async (dataTokenAddress: string): Promise<BestPrice> => {
if (!dataTokenAddress) dataTokenAddress = internalDdo.dataToken const price = await getBestDataTokenPrice(
return await getBestDataTokenPrice(ocean, dataTokenAddress, accountId) ocean,
dataTokenAddress,
accountId
)
return price
}, },
[ocean, accountId, internalDdo?.dataToken] [ocean, accountId]
) )
const getMetadata = useCallback(async (): Promise<Metadata | null> => { const getMetadata = useCallback(async (ddo: DDO): Promise<Metadata> => {
if (!internalDdo) return null const metadata = ddo.findServiceByType('metadata')
const metadata = internalDdo.findServiceByType('metadata')
return metadata.attributes return metadata.attributes
}, [internalDdo]) }, [])
//
// Get and set DDO based on passed DDO or DID
//
useEffect(() => { useEffect(() => {
if (!asset || !ocean || status !== ProviderStatus.CONNECTED) return
async function init(): Promise<void> { async function init(): Promise<void> {
if (ocean && status === ProviderStatus.CONNECTED) {
if (!asset) return if (!asset) return
if (isDDO(asset)) { if (isDDO(asset as string | DDO | DID)) {
setDDO(asset) setDDO(asset as DDO)
setDID(asset.id) setDID((asset as DDO).id)
} else { } else {
const ddo = await getDDO(asset) // asset is a DID
const ddo = await getDDO(asset as DID)
Logger.debug('DDO', ddo) Logger.debug('DDO', ddo)
setDDO(ddo) setDDO(ddo)
setDID(asset) setDID(asset as DID)
}
} }
} }
init() init()
}, [ocean, status, asset, getDDO]) }, [ocean, status, asset, getDDO])
//
// Get metadata for stored DDO
//
useEffect(() => { useEffect(() => {
if (!accountId) return if (!accountId) return
async function init(): Promise<void> { async function init(): Promise<void> {
if (internalDdo) { if (!internalDdo) return
const metadata = await getMetadata()
const metadata = await getMetadata(internalDdo)
setMetadata(metadata) setMetadata(metadata)
setTitle(metadata.main.name) setTitle(metadata.main.name)
const price = await getPrice() const price = await getPrice(internalDdo.dataToken)
price && setPrice(price)
setPrice(price)
setIsLoaded(true) setIsLoaded(true)
} }
}
init() init()
const interval = setInterval(async () => { const interval = setInterval(async () => {
const price = await getPrice() if (!internalDdo) return
setPrice(price) const price = await getPrice(internalDdo.dataToken)
price && setPrice(price)
}, 10000) }, 10000)
return () => clearInterval(interval)
}, [accountId, internalDdo, getMetadata, getPrice]) return () => {
clearInterval(interval)
}
}, [accountId, chainId, internalDdo, getMetadata, getPrice])
return { return {
ddo: internalDdo, ddo: internalDdo,

View File

@ -50,8 +50,7 @@ export async function getCheapestPool(
export async function getCheapestExchange( export async function getCheapestExchange(
ocean: Ocean, ocean: Ocean,
dataTokenAddress: string dataTokenAddress: string
): Promise<{ address?: string; price: string } | null> { ): Promise<{ address?: string; price: string }> {
if (!ocean || !dataTokenAddress) return
try { try {
const tokenExchanges = await ocean.fixedRateExchange.searchforDT( const tokenExchanges = await ocean.fixedRateExchange.searchforDT(
dataTokenAddress, dataTokenAddress,
@ -94,7 +93,7 @@ export async function getBestDataTokenPrice(
ocean: Ocean, ocean: Ocean,
dataTokenAddress: string, dataTokenAddress: string,
accountId: string accountId: string
): Promise<BestPrice | undefined> { ): Promise<BestPrice> {
const cheapestPool = await getCheapestPool(ocean, accountId, dataTokenAddress) const cheapestPool = await getCheapestPool(ocean, accountId, dataTokenAddress)
const cheapestExchange = await getCheapestExchange(ocean, dataTokenAddress) const cheapestExchange = await getCheapestExchange(ocean, dataTokenAddress)
Decimal.set({ precision: 5 }) Decimal.set({ precision: 5 })

View File

@ -11,11 +11,7 @@
"sourceMap": true, "sourceMap": true,
"declaration": true, "declaration": true,
"importHelpers": true, "importHelpers": true,
"strict": true
/* Strict Type-Checking */
// TODO: remove and adapt for `strictNullChecks`
"strict": true,
"strictNullChecks": false
}, },
"include": ["./src/@types", "./src/index.ts"] "include": ["./src/@types", "./src/index.ts"]
} }