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

Commit

Permalink
feat(wallet): Persist wallet information
Browse files Browse the repository at this point in the history
  • Loading branch information
devinus committed Jan 11, 2018
1 parent fed6f34 commit c774075
Show file tree
Hide file tree
Showing 13 changed files with 512 additions and 38 deletions.
29 changes: 28 additions & 1 deletion app/ajax/service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { inject as service } from '@ember/service';
import { readOnly } from '@ember/object/computed';

import AjaxService from 'ember-ajax/services/ajax';
import AJAXPromise from 'ember-ajax/-private/promise';

import retryWithBackoff from 'ember-backoff/retry-with-backoff';

export default AjaxService.extend({
Expand All @@ -12,6 +15,30 @@ export default AjaxService.extend({
contentType: 'application/json',

request(...args) {
return retryWithBackoff(() => this._super(...args));
return retryWithBackoff(() => this._request(...args), 10, 500);
},

// https://github.com/ember-cli/ember-ajax/issues/296
_request(url, options) {
const hash = this.options(url, options);
const internalPromise = this._makeRequest(hash);

const ajaxPromise = new AJAXPromise((resolve, reject) => {
internalPromise
.then(({ response }) => {
resolve(response);
})
.catch(({ response }) => {
if (response instanceof Error) {
return reject(response);
}

reject(response.response);
});
}, `ember-ajax: ${hash.type} ${hash.url} response`);

ajaxPromise.xhr = internalPromise.xhr;

return ajaxPromise;
},
});
62 changes: 33 additions & 29 deletions app/components/wallet-overview/template.hbs
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
<p class="lead">
Wallet Balance: <span class="text-muted">&#11041;</span> {{format-amount wallet.balance}}
Total Balance: <span class="text-muted">&#11041;</span> {{format-amount wallet.balance}}
</p>

<table class="table table-striped table-responsive">
<caption class="sr-only">All your accounts</caption>
<thead>
<tr>
<th scope="col">Account</th>
<th scope="col">Balance</th>
<th scope="col">Pending</th>
</tr>
</thead>
<tbody>
{{#each wallet.accounts as |acc|}}
<tr scope="row">
<td>
{{#link-to 'wallets.accounts' wallet acc}}
{{truncate acc.id 30}}
{{/link-to}}
</td>
<td>
{{format-amount acc.balance}}
</td>
<td>
{{format-amount acc.pending}}
</td>
</tr>
{{/each}}
</tbody>
</table>
<p class="small text-muted">
Wallet: {{wallet.id}}
</p>

{{!-- {{truncate "Lorem ipsum dolor sit amet, consectetur adipiscing elit." 20}} --}}
{{#if wallet.accounts}}
<table class="table table-striped table-responsive">
<caption class="sr-only">All your accounts</caption>
<thead>
<tr>
<th scope="col">Account</th>
<th scope="col">Balance</th>
<th scope="col">Pending</th>
</tr>
</thead>
<tbody>
{{#each wallet.accounts as |acc|}}
<tr scope="row">
<td>
{{#link-to 'wallets.accounts' wallet acc}}
{{truncate acc.id 30}}
{{/link-to}}
</td>
<td>
{{format-amount acc.balance}}
</td>
<td>
{{format-amount acc.pending}}
</td>
</tr>
{{/each}}
</tbody>
</table>
{{/if}}

{{#bs-button type="primary" icon="fa fa-plus-circle" value=wallet onClick=(action 'createAccount')}}
Create Account
Expand Down
16 changes: 13 additions & 3 deletions app/index/route.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import Route from '@ember/routing/route';
import { get } from '@ember/object';
import { inject as service } from '@ember/service';

export default Route.extend({
redirect() {
const wallet = this.store.createRecord('wallet');
this.transitionTo('wallets', wallet.save());
settings: service('settings'),

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

return this.transitionTo('wallets', wallet);
},
});
39 changes: 39 additions & 0 deletions app/settings/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Service from '@ember/service';
import {
get,
set,
getProperties,
setProperties,
getWithDefault,
} from '@ember/object';

import { storageFor } from 'ember-local-storage';

export default Service.extend({
settings: storageFor('settings'),

get(keyName) {
const settings = get(this, 'settings');
return get(settings, keyName);
},

set(keyName, value) {
const settings = get(this, 'settings');
return set(settings, keyName, value);
},

getProperties(...args) {
const settings = get(this, 'settings');
return getProperties(settings, ...args);
},

setProperties(hash) {
const settings = get(this, 'settings');
return setProperties(settings, hash);
},

getWithDefault(keyName, defaultValue) {
const settings = get(this, 'settings');
return getWithDefault(settings, keyName, defaultValue);
},
});
14 changes: 14 additions & 0 deletions app/settings/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import StorageObject from 'ember-local-storage/local/object';

const Storage = StorageObject.extend();

// Uncomment if you would like to set initialState
Storage.reopenClass({
initialState() {
return {
wallet: null,
};
}
});

export default Storage;
2 changes: 2 additions & 0 deletions app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
</header>

<main role="main" class="container-fluid">
{{bread-crumbs tagName="ol" linkable=true crumbClass="breadcrumb-item"}}

<div class="row">
<div class="col">
{{outlet}}
Expand Down
4 changes: 4 additions & 0 deletions app/wallets/accounts/route.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import Route from '@ember/routing/route';

export default Route.extend({
breadCrumb: {
title: 'Account',
path: 'wallets.accounts',
},
});
8 changes: 5 additions & 3 deletions app/wallets/accounts/send/route.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Route from '@ember/routing/route';
import { get } from '@ember/object';

import { hash } from 'rsvp';

export default Route.extend({
breadCrumb: {
title: 'Send',
path: 'wallets.accounts.send',
},

model() {
const wallet = this.modelFor('wallets');
const source = this.modelFor('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
@@ -1,4 +1,8 @@
import Route from '@ember/routing/route';

export default Route.extend({
breadCrumb: {
title: 'Wallet',
path: 'wallets.overview',
},
});
3 changes: 1 addition & 2 deletions app/wallets/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import Route from '@ember/routing/route';

export default Route.extend({
// redirect() {
// const account = this.store.findRecord('account', 'xrb_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo');
// this.transitionTo('wallets.accounts', account);
// this.transitionTo('wallets.overview');
// },

// actions: {
Expand Down
Loading

0 comments on commit c774075

Please sign in to comment.