1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 09:23:13 +01:00

If fetching COA results in 404, created a new COA

This commit is contained in:
Brett Sun 2015-12-17 15:46:30 +01:00
parent 95bce14220
commit e81341269d
2 changed files with 20 additions and 10 deletions

View File

@ -8,7 +8,12 @@ import EditionActions from '../actions/edition_actions';
const CoaSource = { const CoaSource = {
lookupCoa: { lookupCoa: {
remote(state) { 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, success: EditionActions.successFetchCoa,
@ -24,4 +29,4 @@ const CoaSource = {
} }
}; };
export default CoaSource; export default CoaSource;

View File

@ -31,16 +31,16 @@ class EditionStore {
this.getInstance().lookupEdition(); this.getInstance().lookupEdition();
} }
onSuccessFetchEdition(res) { onSuccessFetchEdition({ edition }) {
if(res && res.edition) { if (edition) {
this.edition = res.edition; this.edition = edition;
this.editionMeta.err = null; this.editionMeta.err = null;
this.editionMeta.idToFetch = null; this.editionMeta.idToFetch = null;
if (this.edition.coa && this.edition.acl.acl_coa && if (this.edition.coa && this.edition.acl.acl_coa &&
typeof this.edition.coa.constructor !== Object) { typeof this.edition.coa.constructor !== Object) {
this.getInstance().lookupCoa(); 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(); this.getInstance().performCreateCoa();
} }
} else { } else {
@ -48,9 +48,9 @@ class EditionStore {
} }
} }
onSuccessFetchCoa(res) { onSuccessFetchCoa({ coa }) {
if (res && res.coa && Object.keys(this.edition).length) { if (coa && Object.keys(this.edition).length) {
this.edition.coa = res.coa; this.edition.coa = coa;
this.coaMeta.err = null; this.coaMeta.err = null;
} else { } else {
this.coaMeta.err = new Error('Problem generating/fetching the COA'); this.coaMeta.err = new Error('Problem generating/fetching the COA');
@ -73,7 +73,12 @@ class EditionStore {
} }
onErrorCoa(err) { 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;
}
} }
} }