From ea4805caa0ae67eab853775aac914e8170753b02 Mon Sep 17 00:00:00 2001 From: diminator Date: Wed, 30 Sep 2015 09:14:57 +0200 Subject: [PATCH 1/2] create CoA when not found --- js/actions/coa_actions.js | 46 +++++++++++++++++-------- js/components/ascribe_detail/edition.js | 7 +++- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/js/actions/coa_actions.js b/js/actions/coa_actions.js index 7c6f5118..8249f730 100644 --- a/js/actions/coa_actions.js +++ b/js/actions/coa_actions.js @@ -3,6 +3,7 @@ import alt from '../alt'; import CoaFetcher from '../fetchers/coa_fetcher'; +import Q from 'q'; class CoaActions { constructor() { @@ -13,22 +14,39 @@ class CoaActions { } fetchOne(id) { - CoaFetcher.fetchOne(id) - .then((res) => { - this.actions.updateCoa(res.coa); - }) - .catch((err) => { - console.logGlobal(err); - }); + return Q.Promise((resolve, reject) => { + CoaFetcher.fetchOne(id) + .then((res) => { + if (res.coa) { + this.actions.updateCoa(res.coa); + resolve(res.coa); + } + else { + this.actions.updateCoa(null); + resolve(null); + } + + }) + .catch((err) => { + console.logGlobal(err); + this.actions.updateCoa(null); + reject(err); + }); + }); } + create(edition) { - CoaFetcher.create(edition.bitcoin_id) - .then((res) => { - this.actions.updateCoa(res.coa); - }) - .catch((err) => { - console.logGlobal(err); - }); + return Q.Promise((resolve, reject) => { + CoaFetcher.create(edition.bitcoin_id) + .then((res) => { + this.actions.updateCoa(res.coa); + }) + .catch((err) => { + console.logGlobal(err); + this.actions.updateCoa(null); + reject(err); + }); + }); } } diff --git a/js/components/ascribe_detail/edition.js b/js/components/ascribe_detail/edition.js index 97f9fb3d..aa82beb6 100644 --- a/js/components/ascribe_detail/edition.js +++ b/js/components/ascribe_detail/edition.js @@ -338,7 +338,12 @@ let CoaDetails = React.createClass({ componentDidMount() { CoaStore.listen(this.onChange); if(this.props.edition.coa) { - CoaActions.fetchOne(this.props.edition.coa); + CoaActions.fetchOne(this.props.edition.coa) + .then((res) => { + if (res === null){ + CoaActions.create(this.props.edition); + } + }); } else { CoaActions.create(this.props.edition); From 55f5d959fff8ee20e43861e899449f83e4c2b32b Mon Sep 17 00:00:00 2001 From: diminator Date: Wed, 30 Sep 2015 11:26:31 +0200 Subject: [PATCH 2/2] PR fixes --- js/actions/coa_actions.js | 10 ++++------ js/components/ascribe_detail/edition.js | 12 ++++-------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/js/actions/coa_actions.js b/js/actions/coa_actions.js index 8249f730..da279bc6 100644 --- a/js/actions/coa_actions.js +++ b/js/actions/coa_actions.js @@ -13,7 +13,7 @@ class CoaActions { ); } - fetchOne(id) { + fetchOrCreate(id, bitcoinId) { return Q.Promise((resolve, reject) => { CoaFetcher.fetchOne(id) .then((res) => { @@ -22,10 +22,8 @@ class CoaActions { resolve(res.coa); } else { - this.actions.updateCoa(null); - resolve(null); + this.actions.create(bitcoinId); } - }) .catch((err) => { console.logGlobal(err); @@ -35,9 +33,9 @@ class CoaActions { }); } - create(edition) { + create(bitcoinId) { return Q.Promise((resolve, reject) => { - CoaFetcher.create(edition.bitcoin_id) + CoaFetcher.create(bitcoinId) .then((res) => { this.actions.updateCoa(res.coa); }) diff --git a/js/components/ascribe_detail/edition.js b/js/components/ascribe_detail/edition.js index aa82beb6..7f700c42 100644 --- a/js/components/ascribe_detail/edition.js +++ b/js/components/ascribe_detail/edition.js @@ -336,17 +336,13 @@ let CoaDetails = React.createClass({ }, componentDidMount() { + let { edition } = this.props; CoaStore.listen(this.onChange); - if(this.props.edition.coa) { - CoaActions.fetchOne(this.props.edition.coa) - .then((res) => { - if (res === null){ - CoaActions.create(this.props.edition); - } - }); + if(edition.coa) { + CoaActions.fetchOrCreate(edition.coa, edition.bitcoin_id); } else { - CoaActions.create(this.props.edition); + CoaActions.create(edition.bitcoin_id); } },