Process contribution data before loading models

This commit is contained in:
2020-05-28 13:44:32 +02:00
parent e7c9620bbe
commit 69141a31c4
10 changed files with 501 additions and 34 deletions
+11 -5
View File
@@ -1,6 +1,5 @@
import EmberObject, { computed } from '@ember/object';
import { isEmpty, isPresent } from '@ember/utils';
import bignumber from 'kredits-web/utils/cps/bignumber';
import moment from 'moment';
export default EmberObject.extend({
@@ -9,11 +8,15 @@ export default EmberObject.extend({
id: null,
contributorId: null,
amount: null,
confirmedAt: bignumber('confirmedAtBlock', 'toNumber'),
confirmedAt: null,
vetoed: null,
ipfsHash: null,
creatorAccount: null,
// contributor model instance
contributor: null,
// TODO contributor who submitted the contribution
// submittedBy: null,
// IPFS
kind: null,
@@ -22,7 +25,6 @@ export default EmberObject.extend({
url: null,
date: null,
time: null,
ipfsData: '',
pendingTx: null,
@@ -41,6 +43,10 @@ export default EmberObject.extend({
hasPendingChanges: computed('pendingTx', function() {
return isPresent(this.pendingTx);
})
}),
serialize () {
return JSON.stringify(this);
}
});
+1
View File
@@ -22,4 +22,5 @@ export default EmberObject.extend({
serialize () {
return JSON.stringify(this);
}
});
+26 -12
View File
@@ -11,6 +11,7 @@ import { inject as service } from '@ember/service';
import groupBy from 'kredits-web/utils/group-by';
import processContributorData from 'kredits-web/utils/process-contributor-data';
import processContributionData from 'kredits-web/utils/process-contribution-data';
import formatKredits from 'kredits-web/utils/format-kredits';
import config from 'kredits-web/config/environment';
@@ -185,11 +186,11 @@ export default Service.extend({
}
const numCachedContributions = await this.browserCache.contributions.length();
if (numCachedContributions > 0) {
// if (numCachedContributions > 0) {
// TODO promises.push(this.loadContributionsFromCache);
} else {
// } else {
await this.fetchContributions({ page: { size: 30 } });
}
// }
return Promise.resolve();
},
@@ -235,11 +236,11 @@ export default Service.extend({
});
})
.then(() => {
return this.cacheContributors();
return this.cacheLoadedContributors();
});
},
async cacheContributors () {
async cacheLoadedContributors () {
for (const c of this.contributors) {
await this.browserCache.contributors.setItem(c.id, c.serialize());
}
@@ -248,9 +249,9 @@ export default Service.extend({
},
async loadContributorsFromCache () {
return this.browserCache.contributors.iterate((value, key/* , iterationNumber */) => {
return this.browserCache.contributors.iterate((value/*, key , iterationNumber */) => {
this.contributors.pushObject(Contributor.create(JSON.parse(value)));
}).then(result => {
}).then((/* result */) => {
console.debug(`[kredits] Loaded ${this.contributors.length} contributors from cache`);
});
},
@@ -275,15 +276,28 @@ export default Service.extend({
return this.kredits.Contribution.all(options)
.then(contributions => {
return contributions.map(data => {
data.contributor = this.contributors.findBy('id', data.contributorId.toString());
const contribution = Contribution.create(data);
const contribution = Contribution.create(processContributionData(data));
contribution.set('contributor', this.contributors.findBy('id', data.contributorId.toString()));
this.contributions.pushObject(contribution);
return contribution;
});
})
.then(contributions => {
const cacheWrites = contributions.map(c => {
return this.browserCache.contributions.setItem(c.id.toString(), c.serialize());
});
return Promise.all(cacheWrites).then(() => {
console.debug(`[kredits] Cached ${contributions.length} contributions in browser storage`);
});
});
// TODO .then(() => {
// this.cacheContributions()
// });
},
async cacheLoadedContributions () {
for (const c of this.contributions) {
await this.browserCache.contributions.setItem(c.id, c.serialize());
}
console.debug(`[kredits] Cached ${this.contributions.length} contributions in browser storage`);
return Promise.resolve();
},
veto (contributionId) {
+21
View File
@@ -0,0 +1,21 @@
export default function processContributionData(data) {
const processed = {}
if (data.confirmedAtBlock && (typeof data.confirmedAtBlock.toNumber === 'function')) {
processed.confirmedAt = data.confirmedAtBlock.toNumber();
} else if (data.confirmedAt !== 'undefined') {
processed.confirmedAt = data.confirmedAt;
}
const otherProperties = [
'id', 'contributorId', 'amount', 'vetoed',
'ipfsHash', 'kind', 'description', 'details',
'url', 'date', 'time', 'pendingTx'
];
otherProperties.forEach(prop => {
processed[prop] = data[prop];
});
return processed;
}
+1 -1
View File
@@ -3,7 +3,7 @@ export default function processContributorData(data) {
id: data.id.toString(),
balance: data.balanceInt,
totalKreditsEarned: data.totalKreditsEarned,
contributionsCount: data.contributionsCount.toNumber()
contributionsCount: data.contributionsCount?.toNumber()
}
const otherProperties = [