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

Merge pull request #81 from ascribe/AD-1524-coa-should-be-created-if-none-exists

If fetching COA results in 404, create a new COA
This commit is contained in:
Brett Sun 2015-12-17 16:03:10 +01:00
commit d996967dd9
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,

View File

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