Skip to content
This repository has been archived by the owner on Oct 21, 2020. It is now read-only.

Commit

Permalink
feat(history): Add history routes
Browse files Browse the repository at this point in the history
  • Loading branch information
devinus committed Jan 11, 2018
1 parent c774075 commit f980907
Show file tree
Hide file tree
Showing 25 changed files with 531 additions and 35 deletions.
11 changes: 7 additions & 4 deletions app/account/model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import DS from 'ember-data';

const { attr, hasMany, belongsTo } = DS;

export default DS.Model.extend({
wallet: DS.belongsTo('wallet'),
blocks: DS.hasMany('block', { inverse: 'source' }),
wallet: belongsTo('wallet'),
blocks: hasMany('block', { async: true, inverse: 'source' }),
history: hasMany('history', { async: true, inverse: 'parentAccount' }),

balance: DS.attr('big-number'),
pending: DS.attr('big-number'),
balance: attr('big-number'),
pending: attr('big-number'),
});
10 changes: 6 additions & 4 deletions app/block/model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import DS from 'ember-data';

const { attr, belongsTo } = DS;

export default DS.Model.extend({
wallet: DS.belongsTo('wallet', { inverse: null }),
source: DS.belongsTo('account', { inverse: 'blocks' }),
wallet: belongsTo('wallet', { inverse: null }),
source: belongsTo('account', { inverse: 'blocks' }),

destination: DS.attr('string'),
amount: DS.attr('big-number'),
destination: attr('string'),
amount: attr('big-number'),
});
7 changes: 7 additions & 0 deletions app/components/account-history/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Component from '@ember/component';

export default Component.extend({
wallet: null,
account: null,
history: null,
});
27 changes: 27 additions & 0 deletions app/components/account-history/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<table class="table table-striped table-responsive">
<caption class="sr-only">History for account {{account.id}}</caption>
<thead>
<tr>
<th scope="col">Type</th>
<th scope="col">Account</th>
<th scope="col">Amount</th>
</tr>
</thead>
<tbody>
{{#each history as |entry|}}
<tr scope="row">
<td>
{{entry.type}}
</td>
<td>
{{#link-to 'wallets.accounts.index' wallet account disabledWhen=(not (contains account wallet.accounts))}}
{{truncate entry.account 30}}
{{/link-to}}
</td>
<td>
{{format-amount entry.amount}}
</td>
</tr>
{{/each}}
</tbody>
</table>
24 changes: 24 additions & 0 deletions app/history/adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import DS from 'ember-data';
import { inject as service } from '@ember/service';

import { resolve } from 'rsvp';

export default DS.Adapter.extend({
rpc: service(),

// async findRecord(store, type, id, snapshot) {
// const info = await this.get('rpc').accountInfo(id);
// const data = this.serialize(snapshot, { includeId: true });
// return assign(data, info);
// },

// async createRecord(store, type, snapshot) {
// const { wallet } = this.serialize(snapshot, { includeId: true });
// const { account } = await this.get('rpc').accountCreate(wallet);
// return this.get('rpc').accountInfo(account);
// },

query(store, type, { account, count = 10 }) {
return this.get('rpc').accountHistory(account, count);
},
});
12 changes: 12 additions & 0 deletions app/history/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import DS from 'ember-data';

const { attr, belongsTo } = DS;

export default DS.Model.extend({
parentAccount: belongsTo('account'),

// hash: attr('string'),
type: attr('string'),
account: attr('string'),
amount: attr('big-number'),
});
5 changes: 5 additions & 0 deletions app/history/serializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import DS from 'ember-data';

export default DS.JSONSerializer.extend({
primaryKey: 'hash',
});
8 changes: 4 additions & 4 deletions app/index/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { get } from '@ember/object';
import { inject as service } from '@ember/service';

export default Route.extend({
settings: service('settings'),
settings: service(),

async redirect() {
async beforeModel(transition) {
const settings = get(this, 'settings');
let wallet = settings.get('wallet');
if (!wallet) {
transition.abort();
wallet = await this.store.createRecord('wallet').save();
settings.setProperties(this.store.serialize(wallet, { includeId: true }));
return this.transitionTo('wallets', wallet);
}

return this.transitionTo('wallets', wallet);
},
});
1 change: 1 addition & 0 deletions app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Router.map(function() {
this.route('send', function() {});
this.route('accounts', { path: '/:account_id' }, function() {
this.route('send', function() {});
this.route('history');
});
});
});
Expand Down
32 changes: 24 additions & 8 deletions app/rpc/service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import Service from '@ember/service';
import { inject as service } from '@ember/service';
import { assign } from '@ember/polyfills';
import { A } from '@ember/array';

const WALLET_CREATE = 'wallet_create';
const ACCOUNT_CREATE = 'account_create';
const ACCOUNT_INFO = 'account_info';
const WALLET_BALANCE_TOTAL = 'wallet_balance_total';
const ACCOUNT_LIST = 'account_list';
const SEND = 'send';
const ACCOUNT_HISTORY = 'account_history';

export default Service.extend({
ajax: service(),
Expand All @@ -11,15 +20,15 @@ export default Service.extend({
},

walletCreate() {
return this.call('wallet_create');
return this.call(WALLET_CREATE);
},

accountCreate(wallet) {
return this.call('account_create', { wallet });
return this.call(ACCOUNT_CREATE, { wallet });
},

async accountInfo(account, pending = true) {
let info = await this.call('account_info', { account, pending });
let info = await this.call(ACCOUNT_INFO, { account, pending });

// When an account has no transactions, the RPC seems to return an
// HTTP OK *and* an error.
Expand All @@ -28,27 +37,34 @@ export default Service.extend({
if (pending) {
info.pending = "0";
}

return info;
}

return info;
},

walletBalanceTotal(wallet) {
return this.call('wallet_balance_total', { wallet });
return this.call(WALLET_BALANCE_TOTAL, { wallet });
},

accountList(wallet) {
return this.call('account_list', { wallet });
return this.call(ACCOUNT_LIST, { wallet });
},

send(wallet, source, destination, amount) {
return this.call('send', {
return this.call(SEND, {
wallet,
source,
destination,
amount,
});
},

async accountHistory(account, count = 1) {
const { history } = await this.call(ACCOUNT_HISTORY, {
account,
count,
});

return A(history);
},
});
1 change: 1 addition & 0 deletions app/settings/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Storage.reopenClass({
initialState() {
return {
wallet: null,
account: null,
};
}
});
Expand Down
6 changes: 4 additions & 2 deletions app/wallet/model.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import DS from 'ember-data';

const { attr, hasMany } = DS;

export default DS.Model.extend({
accounts: DS.hasMany('account', { async: true }),
accounts: hasMany('account', { async: true }),

balance: DS.attr('big-number'),
balance: attr('big-number'),
});
16 changes: 16 additions & 0 deletions app/wallets/accounts/history/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Route from '@ember/routing/route';
import { get, set } from '@ember/object';

export default Route.extend({
async model() {
const account = this.modelFor('wallets.accounts');
const history = await this.store.query('history', {
account: get(account, 'id'),
count: 10,
});

set(account, 'history', history);

return account;
}
});
7 changes: 7 additions & 0 deletions app/wallets/accounts/history/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>History</h1>

<p class="lead">
Account: {{model.id}}
</p>

{{account-history wallet=model.wallet account=model history=model.history}}
4 changes: 2 additions & 2 deletions app/wallets/accounts/index/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
Send
{{/link-to}}

{{#link-to 'wallets.accounts.send' class="card-link"}}
Receive
{{#link-to 'wallets.accounts.history' class="card-link"}}
History
{{/link-to}}
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions app/wallets/accounts/route.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default Route.extend({
settings: service(),

breadCrumb: {
title: 'Account',
path: 'wallets.accounts',
Expand Down
4 changes: 4 additions & 0 deletions app/wallets/index/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ export default Route.extend({
title: 'Wallet',
path: 'wallets.overview',
},

redirect() {
this.transitionTo('wallets.overview');
},
});
11 changes: 0 additions & 11 deletions app/wallets/route.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import Route from '@ember/routing/route';

export default Route.extend({
// redirect() {
// this.transitionTo('wallets.overview');
// },

// actions: {
// createAccount() {
// this.store.createRecord('account', {
// wallet: '5884EFFA051F8363A4CCFAB1CBECCA4E0CD13DC4D125722ED75E8CD37FC55662'
// });
// }
// }
});
Loading

0 comments on commit f980907

Please sign in to comment.