From ff93d688b4bba105971092f151742e52e1eeefdf Mon Sep 17 00:00:00 2001 From: mihaisc Date: Wed, 20 May 2020 13:37:59 +0300 Subject: [PATCH] added getConsumedList --- src/hooks/useSearch/useSearch.ts | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/hooks/useSearch/useSearch.ts b/src/hooks/useSearch/useSearch.ts index eaa2988..532c4c7 100644 --- a/src/hooks/useSearch/useSearch.ts +++ b/src/hooks/useSearch/useSearch.ts @@ -1,5 +1,5 @@ import { useState } from 'react' -import { Logger } from '@oceanprotocol/squid' +import { Logger, DDO } from '@oceanprotocol/squid' import { useOcean } from '../../providers' import { SearchQuery, @@ -7,19 +7,20 @@ import { QueryResult } from '@oceanprotocol/squid/dist/node/aquarius/Aquarius' -// TODO searchText +// TODO searchText, interface UseSearch { searchWithQuery: (query: SearchQuery) => Promise getPublishedList: ( - account: string, page: number, offset: number ) => Promise + getConsumedList: () => Promise<(DDO[] | undefined)> searchError?: string } function useSearch(): UseSearch { - const { ocean, account, config } = useOcean() + // should we call the useOcean hook in useSearch or in each function? + const { ocean, account, config, accountId } = useOcean() const [searchError, setSearchError] = useState() async function searchWithQuery(query: SearchQuery): Promise { @@ -36,7 +37,6 @@ function useSearch(): UseSearch { } async function getPublishedList( - account: string, page: number, offset: number ): Promise { @@ -49,7 +49,7 @@ function useSearch(): UseSearch { page, offset, query: { - 'publicKey.owner': [account] + 'publicKey.owner': [accountId] }, sort: { created: -1 @@ -62,7 +62,25 @@ function useSearch(): UseSearch { } } - return { searchWithQuery, getPublishedList, searchError } + async function getConsumedList() : Promise<(DDO []| undefined)>{ + const consumed = await ocean.assets.consumerAssets(accountId) + const consumedItems = await Promise.all( + consumed.map(async (did) => { + const ddo = await ocean.assets.resolve(did) + if (ddo) { + // Since we are getting assets from chain there might be + // assets from other marketplaces. So return only those assets + // whose serviceEndpoint contains the configured Aquarius URI. + const { serviceEndpoint } = ddo.findServiceByType('metadata') + if (serviceEndpoint?.includes(config.aquariusUri)) return ddo + } + }) + ) + + return consumedItems + } + + return { searchWithQuery, getPublishedList,getConsumedList, searchError } } export { useSearch, UseSearch }