1
0
mirror of https://github.com/oceanprotocol/react.git synced 2024-11-22 09:47:06 +01:00

useMetadata fix

This commit is contained in:
mihaisc 2020-05-11 18:38:45 +03:00
parent 1a7b390a2f
commit 4d932e7a58
2 changed files with 11 additions and 6 deletions

View File

@ -1,7 +1,7 @@
import { useState, useEffect } from 'react'
import axios from 'axios'
import { DID, DDO, MetaData, Curation } from '@oceanprotocol/squid'
import { useOcean } from '../../providers'
import { useOcean, OceanConnectionStatus } from '../../providers'
interface UseMetadata {
ddo: DDO
@ -15,18 +15,21 @@ interface UseMetadata {
}
function useMetadata(did?: DID | string): UseMetadata {
const { aquarius, config } = useOcean()
const { aquarius, config, status } = useOcean()
const [ddo, setDDO] = useState<DDO | undefined>()
const [metadata, setMetadata] = useState<MetaData | undefined>()
const [title, setTitle] = useState<string | undefined>()
async function getDDO(did: DID | string): Promise<DDO> {
if(status!=OceanConnectionStatus.CONNECTED) return
const ddo = await aquarius.retrieveDDO(did)
return ddo
}
async function getMetadata(did: DID | string): Promise<MetaData> {
const ddo = await getDDO(did)
if(!ddo) return
const metadata = ddo.findServiceByType('metadata')
return metadata.attributes
}
@ -38,6 +41,7 @@ function useMetadata(did?: DID | string): UseMetadata {
async function getTitle(did: DID | string): Promise<string> {
const metadata = await getMetadata(did)
console.log(metadata)
return metadata.main.name
}
@ -48,6 +52,7 @@ function useMetadata(did?: DID | string): UseMetadata {
useEffect(() => {
async function init(): Promise<void> {
if(!did) return
const ddo = await getDDO(did)
setDDO(ddo)

View File

@ -3,7 +3,7 @@ import { Ocean, Config, Account, Aquarius, Logger } from '@oceanprotocol/squid'
import Web3 from 'web3'
import Balance from '@oceanprotocol/squid/dist/node/models/Balance'
import { connectOcean } from './utils'
import { useWeb3 } from '../Web3Provider'
import { useWeb3, InjectedProviderStatus } from '../Web3Provider'
enum OceanConnectionStatus {
OCEAN_CONNECTION_ERROR = -1,
@ -40,7 +40,7 @@ function OceanProvider({
const [status, setStatus] = useState<OceanConnectionStatus>(
OceanConnectionStatus.NOT_CONNECTED
)
const { web3 } = useWeb3()
const { web3,ethProviderStatus } = useWeb3()
// -------------------------------------------------------------
// 1. On mount, connect to Aquarius instance right away
@ -54,8 +54,8 @@ function OceanProvider({
// 2. Once `web3` becomes available, connect to the whole network
// -------------------------------------------------------------
useEffect(() => {
if (!web3) return
if (!web3 || ethProviderStatus!= InjectedProviderStatus.CONNECTED ) return
console.log(ethProviderStatus)
async function init(): Promise<void> {
const { ocean, account, accountId, balance } = await connectOcean(
web3,