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 = {
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,

View File

@ -31,9 +31,9 @@ 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;
@ -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,8 +73,13 @@ class EditionStore {
}
onErrorCoa(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;
}
}
}
export default alt.createStore(EditionStore, 'EditionStore');