diff --git a/js/sources/coa_source.js b/js/sources/coa_source.js index 88c72cce..bf1d8d0f 100644 --- a/js/sources/coa_source.js +++ b/js/sources/coa_source.js @@ -8,7 +8,12 @@ import EditionActions from '../actions/edition_actions'; const CoaSource = { lookupCoa: { remote(state) { - return requests.get('coa', { id: state.edition.coa }); + return requests + .get('coa', { id: state.edition.coa }) + .then((res) => { + // If no coa is found here, fake a 404 error so the error action can pick it up + return (res && res.coa) ? res : Promise.reject({ json: { status: 404 } }); + }); }, success: EditionActions.successFetchCoa, @@ -24,4 +29,4 @@ const CoaSource = { } }; -export default CoaSource; \ No newline at end of file +export default CoaSource; diff --git a/js/stores/edition_store.js b/js/stores/edition_store.js index 8fdd681b..3bf01a90 100644 --- a/js/stores/edition_store.js +++ b/js/stores/edition_store.js @@ -31,16 +31,16 @@ class EditionStore { this.getInstance().lookupEdition(); } - onSuccessFetchEdition(res) { - if(res && res.edition) { - this.edition = res.edition; + onSuccessFetchEdition({ edition }) { + if (edition) { + this.edition = edition; this.editionMeta.err = null; this.editionMeta.idToFetch = null; if (this.edition.coa && this.edition.acl.acl_coa && typeof this.edition.coa.constructor !== Object) { this.getInstance().lookupCoa(); - } else if(!this.edition.coa && this.edition.acl.acl_coa) { + } else if (!this.edition.coa && this.edition.acl.acl_coa) { this.getInstance().performCreateCoa(); } } else { @@ -48,9 +48,9 @@ class EditionStore { } } - onSuccessFetchCoa(res) { - if (res && res.coa && Object.keys(this.edition).length) { - this.edition.coa = res.coa; + onSuccessFetchCoa({ coa }) { + if (coa && Object.keys(this.edition).length) { + this.edition.coa = coa; this.coaMeta.err = null; } else { this.coaMeta.err = new Error('Problem generating/fetching the COA'); @@ -73,7 +73,12 @@ class EditionStore { } onErrorCoa(err) { - this.coaMeta.err = err; + // On 404s, create a new COA as the COA has not been made yet + if (err && err.json && err.json.status === 404) { + this.getInstance().performCreateCoa(); + } else { + this.coaMeta.err = err; + } } }