-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #226 from docknetwork/feat/read-ecosystem-data-fro…
…m-chain feat: implementing ecosystem metadata fetch
- Loading branch information
Showing
1 changed file
with
53 additions
and
11 deletions.
There are no files selected for viewing
64 changes: 53 additions & 11 deletions
64
packages/react-native/lib/ecosystem-tools/ecosystemHooks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,58 @@ | ||
import { useEffect, useState } from "react"; | ||
import { getEcosystems } from "@docknetwork/wallet-sdk-core/src/ecosystem-tools"; | ||
import {useEffect, useState} from 'react'; | ||
import {getEcosystems} from '@docknetwork/wallet-sdk-core/src/ecosystem-tools'; | ||
import {hexToString} from '@polkadot/util'; | ||
import axios from 'axios'; | ||
|
||
export function useEcosystems({issuer, credential}) { | ||
const getMetadata = async govFramework => { | ||
const metadataURL = await hexToString(govFramework); | ||
try { | ||
const response = await axios.get(metadataURL); | ||
return response.data; | ||
} catch (e) { | ||
console.log('error: ', e) | ||
} | ||
|
||
return {}; | ||
}; | ||
|
||
const formatEcosystemData = async ecosystems => { | ||
const formattedEcosystems = []; | ||
const ecosystemsList = Object.entries(ecosystems); | ||
|
||
for (let i = 0; i < ecosystemsList.length; i++) { | ||
let ecosystemData = {}; | ||
ecosystemData.trustRegistryId = ecosystemsList[i][0]; | ||
ecosystemData = {...ecosystemData, ...ecosystemsList[i][1]}; | ||
|
||
const metadata = await getMetadata(ecosystemsList[i][1]?.govFramework); | ||
formattedEcosystems.push({...ecosystemData, ...metadata}); | ||
} | ||
|
||
return formattedEcosystems; | ||
}; | ||
|
||
export function useEcosystems({issuer, schemaId}) { | ||
const [ecosystems, setEcosystems] = useState([]); | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
useEffect(() => { | ||
getEcosystems({issuerDID: issuer, schemaId: credential?.credentialSchema.id}).then( | ||
result => { | ||
setEcosystems(result || []); | ||
}, | ||
); | ||
}, [issuer, credential]); | ||
|
||
return {ecosystems}; | ||
setIsLoading(true); | ||
getEcosystems({issuerDID: issuer, schemaId: schemaId}) | ||
.then(async result => { | ||
try { | ||
const ecosystemData = await formatEcosystemData(result); | ||
setEcosystems(ecosystemData); | ||
} catch (e) { | ||
console.log('Error formatting ecosystem data', e); | ||
} | ||
}) | ||
.catch(e => { | ||
console.log('error fetching ecosystem', e); | ||
}) | ||
.finally(() => { | ||
setIsLoading(false); | ||
}); | ||
}, [issuer, schemaId]); | ||
|
||
return {ecosystems, isLoading}; | ||
} |