1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-30 05:31:58 +02:00
onion/js/actions/coa_actions.js

52 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-06-25 14:39:39 +02:00
'use strict';
import { alt } from '../alt';
2015-06-25 14:39:39 +02:00
import CoaFetcher from '../fetchers/coa_fetcher';
2015-09-30 09:14:57 +02:00
import Q from 'q';
2015-06-25 14:39:39 +02:00
class CoaActions {
constructor() {
this.generateActions(
'updateCoa',
'flushCoa'
2015-06-25 14:39:39 +02:00
);
}
2015-09-30 11:26:31 +02:00
fetchOrCreate(id, bitcoinId) {
2015-09-30 09:14:57 +02:00
return Q.Promise((resolve, reject) => {
CoaFetcher.fetchOne(id)
.then((res) => {
if (res.coa) {
this.actions.updateCoa(res.coa);
resolve(res.coa);
}
else {
2015-09-30 11:26:31 +02:00
this.actions.create(bitcoinId);
2015-09-30 09:14:57 +02:00
}
})
.catch((err) => {
console.logGlobal(err);
this.actions.updateCoa(null);
reject(err);
});
});
2015-06-25 14:39:39 +02:00
}
2015-09-30 09:14:57 +02:00
2015-09-30 11:26:31 +02:00
create(bitcoinId) {
2015-09-30 09:14:57 +02:00
return Q.Promise((resolve, reject) => {
2015-09-30 11:26:31 +02:00
CoaFetcher.create(bitcoinId)
2015-09-30 09:14:57 +02:00
.then((res) => {
this.actions.updateCoa(res.coa);
})
.catch((err) => {
console.logGlobal(err);
this.actions.updateCoa(null);
reject(err);
});
});
2015-06-25 14:39:39 +02:00
}
}
export default alt.createActions(CoaActions);