Skip to content

Commit

Permalink
Merge pull request #226 from docknetwork/feat/read-ecosystem-data-fro…
Browse files Browse the repository at this point in the history
…m-chain

feat: implementing ecosystem metadata fetch
  • Loading branch information
Jayteekay authored Mar 20, 2024
2 parents 3ef8a8f + 7ef92d2 commit 054e8d4
Showing 1 changed file with 53 additions and 11 deletions.
64 changes: 53 additions & 11 deletions packages/react-native/lib/ecosystem-tools/ecosystemHooks.ts
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};
}

0 comments on commit 054e8d4

Please sign in to comment.