From d1f654531f55632e37a4b23b61291e9b9af50c3b Mon Sep 17 00:00:00 2001 From: Andrey Shovkoplyas Date: Mon, 16 Dec 2019 11:48:30 +0100 Subject: [PATCH] [#16] dev.dap.ps is empty on IOS (develop 09.08.2019) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- .../discover-service/discover-service.js | 69 +++++++++++++------ src/modules/Dapps/Dapps.reducer.js | 12 ++++ 2 files changed, 59 insertions(+), 22 deletions(-) diff --git a/src/common/blockchain/services/discover-service/discover-service.js b/src/common/blockchain/services/discover-service/discover-service.js index 1efa2b0..58ba611 100644 --- a/src/common/blockchain/services/discover-service/discover-service.js +++ b/src/common/blockchain/services/discover-service/discover-service.js @@ -50,6 +50,49 @@ class DiscoverService extends BlockchainService { return MetadataClient.getDappsCount() } + async pushDapps(dappsCache, dapps) { + Object.keys(dappsCache).forEach(metadataHash => { + const dappMetadata = dappsCache[metadataHash] + + if (dappMetadata.status == 'APPROVED') { + dapps.push({ + developer: '', + id: dappMetadata.compressedMetadata, + metadata: { + ...dappMetadata.details, + status: dappMetadata.status, + }, + balance: 0, + rate: 0, + available: 0, + votesMinted: 0, + votesCast: 0, + effectiveBalance: 0, + }) + } + }) + } + + async getAllDappsWithoutMetadata() { + try { + const contractDappsCount = await DiscoverContract.methods + .getDAppsCount() + .call({ from: this.sharedContext.account }) + + const dappsCache = JSON.parse( + JSON.stringify(await MetadataClient.retrieveMetadataCache()), + ) + + let dapps = []; + + await this.pushDapps(dappsCache, dapps) + + return dapps + } catch (error) { + throw new Error(`Error fetching dapps. Details: ${error.message}`) + } + } + async getAllDappsWithMetadata() { try { const contractDappsCount = await DiscoverContract.methods @@ -66,10 +109,11 @@ class DiscoverService extends BlockchainService { DiscoverContract.methods.dapps(i).call({from: this.sharedContext.account}) ) } + let dapps = []; /* using Promise.all() to run calls in parallel */ - let dapps = await Promise.all(asyncCalls) + let dappsCalls = await Promise.all(asyncCalls) - for (let dapp of dapps) { + for (let dapp of dappsCalls) { const dappMetadata = dappsCache[dapp.metadata] if (dappMetadata) { delete dappsCache[dapp.metadata] @@ -80,26 +124,7 @@ class DiscoverService extends BlockchainService { } } - Object.keys(dappsCache).forEach(metadataHash => { - const dappMetadata = dappsCache[metadataHash] - - if (dappMetadata.status == 'APPROVED') { - dapps.push({ - developer: '', - id: dappMetadata.compressedMetadata, - metadata: { - ...dappMetadata.details, - status: dappMetadata.status, - }, - balance: 0, - rate: 0, - available: 0, - votesMinted: 0, - votesCast: 0, - effectiveBalance: 0, - }) - } - }) + await this.pushDapps(dappsCache, dapps) return dapps } catch (error) { diff --git a/src/modules/Dapps/Dapps.reducer.js b/src/modules/Dapps/Dapps.reducer.js index 2727e0f..1b7a842 100644 --- a/src/modules/Dapps/Dapps.reducer.js +++ b/src/modules/Dapps/Dapps.reducer.js @@ -33,7 +33,19 @@ export const fetchAllDappsAction = () => { const { transactionStatus } = state let dappSource = '' + /* we want to show dapps list first and then load all data from blockchain */ + const allDappsWithoutMeta = await discoverService.getAllDappsWithoutMetadata() + + for (let i = 0; i < allDappsWithoutMeta.length; i++) { + dappSource = allDappsWithoutMeta[i] + if (dappSource !== null) { + const dappModel = DappModel.instanceFromBlockchainWithMetadata(dappSource) + dispatch(onUpdateDappsAction(dappState.creditDapp(dappModel))) + } + } + const allDapps = await discoverService.getAllDappsWithMetadata() + for (let i = 0; i < allDapps.length; i++) { dappSource = allDapps[i] if (dappSource !== null) {