From 734d4f4677f0e3c9d51391111d9f5f8d0e630151 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Mon, 27 Apr 2020 14:58:37 +0200 Subject: [PATCH] example updates --- README.md | 6 ++++-- src/hooks/useConsume/useConsume.ts | 11 ++++++++--- src/hooks/useMetadata/useMetadata.ts | 9 ++++++--- src/providers/OceanProvider/OceanProvider.tsx | 13 +++++++++++-- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9f5a9e6..053aa6b 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ export default function MyComponent() { const { ocean, account } = useOcean() // Get metadata for this asset - const { title } = useMetadata(did) + const { title, metadata } = useMetadata(did) // publish asset const { publish, publishStep } = usePublish() @@ -86,7 +86,9 @@ export default function MyComponent() { return (

{title}

- Your account: {account} +

Price: {metadata.main.price}

+ +

Your account: {account}

) diff --git a/src/hooks/useConsume/useConsume.ts b/src/hooks/useConsume/useConsume.ts index 1d206d7..291031b 100644 --- a/src/hooks/useConsume/useConsume.ts +++ b/src/hooks/useConsume/useConsume.ts @@ -9,6 +9,9 @@ interface UseConsume { consumeError?: string } +// TODO: do something with this object, +// consumeStep should probably return one of those strings +// instead of just a number export const feedback: { [key in number]: string } = { 99: 'Decrypting file URL...', 0: '1/3 Asking for agreement signature...', @@ -19,6 +22,8 @@ export const feedback: { [key in number]: string } = { } function useConsume(): UseConsume { + // TODO: figure out if a hook within a hook could lead to problems. + // Otherwise we could just require `ocean` to be passed to `useConsume()` const { ocean, account, accountId } = useOcean() const [consumeStep, setConsumeStep] = useState() const [consumeError, setConsumeError] = useState() @@ -32,16 +37,16 @@ function useConsume(): UseConsume { const agreements = await ocean.keeper.conditions.accessSecretStoreCondition.getGrantedDidByConsumer( accountId ) - const agreement = agreements.find((el: AgreementData) => el.did === did) + const agreement = agreements.find((el: { did: string }) => el.did === did) const agreementId = agreement ? agreement.agreementId : await ocean.assets - .order(did, account) + .order(did as string, account) .next((step: number) => setConsumeStep(step)) // manually add another step here for better UX setConsumeStep(4) - await ocean.assets.consume(agreementId, did, account, '') + await ocean.assets.consume(agreementId, did as string, account, '') } catch (error) { setConsumeError(error.message) } finally { diff --git a/src/hooks/useMetadata/useMetadata.ts b/src/hooks/useMetadata/useMetadata.ts index fd88498..66a887a 100644 --- a/src/hooks/useMetadata/useMetadata.ts +++ b/src/hooks/useMetadata/useMetadata.ts @@ -42,9 +42,12 @@ function useMetadata(did?: DID | string): UseMetadata { useEffect(() => { async function init(): Promise { - setDDO(await getDDO(did)) - setMetadata(await getMetadata(did)) - setTitle(await getTitle(did)) + const ddo = await getDDO(did) + setDDO(ddo) + + const metadata = await getMetadata(did) + setMetadata(metadata) + setTitle(metadata.main.name) } init() }, []) diff --git a/src/providers/OceanProvider/OceanProvider.tsx b/src/providers/OceanProvider/OceanProvider.tsx index ea61d96..d4b3208 100644 --- a/src/providers/OceanProvider/OceanProvider.tsx +++ b/src/providers/OceanProvider/OceanProvider.tsx @@ -45,12 +45,17 @@ function OceanProvider({ OceanConnectionStatus.NOT_CONNECTED ) + // ------------------------------------------------------------- + // 1. On mount, connect to Aquarius instance right away + // ------------------------------------------------------------- useEffect(() => { - // on mount, connect to Aquarius instance right away const aquarius = new Aquarius(config.aquariusUri, Logger) setAquarius(aquarius) }, []) + // ------------------------------------------------------------- + // 2. Once `web3` becomes available, connect to the whole network + // ------------------------------------------------------------- useEffect(() => { async function init(): Promise { const { ocean, account, accountId, balance } = await connectOcean( @@ -73,6 +78,9 @@ function OceanProvider({ } }, [web3]) + // ------------------------------------------------------------- + // 3. Once `ocean` becomes available, spit out some info about it + // ------------------------------------------------------------- useEffect(() => { async function debug(): Promise { if (!ocean) return @@ -103,7 +111,8 @@ function OceanProvider({ ) } -const useOcean = () => useContext(OceanContext) +// Helper hook to access the provider values +const useOcean = (): OceanProviderValue => useContext(OceanContext) export { OceanProvider,