diff --git a/packages/react-native/lib/ecosystem-tools/ecosystemHooks.ts b/packages/react-native/lib/ecosystem-tools/ecosystemHooks.ts index 6da43659..314c1acf 100644 --- a/packages/react-native/lib/ecosystem-tools/ecosystemHooks.ts +++ b/packages/react-native/lib/ecosystem-tools/ecosystemHooks.ts @@ -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}; }