Skip to content

Commit

Permalink
[#16] dev.dap.ps is empty on IOS (develop 09.08.2019)
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Sokołowski <jakub@status.im>
  • Loading branch information
flexsurfer authored and jakubgs committed Dec 17, 2019
1 parent ccae507 commit d383ae4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 22 deletions.
69 changes: 47 additions & 22 deletions src/common/blockchain/services/discover-service/discover-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand All @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions src/modules/Dapps/Dapps.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d383ae4

Please sign in to comment.