2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-06 16:47:59 +02:00
|
|
|
import { alt } from '../alt';
|
2015-12-08 13:28:47 +01:00
|
|
|
|
2015-06-01 18:20:15 +02:00
|
|
|
import EditionActions from '../actions/edition_actions';
|
2015-12-09 13:18:07 +01:00
|
|
|
|
2015-12-08 13:28:47 +01:00
|
|
|
import EditionSource from '../sources/edition_source';
|
2015-12-15 16:26:49 +01:00
|
|
|
import CoaSource from '../sources/coa_source';
|
|
|
|
|
|
|
|
import { mergeOptions } from '../utils/general_utils';
|
2015-12-10 15:42:33 +01:00
|
|
|
|
2015-05-26 13:48:00 +02:00
|
|
|
|
2015-05-26 13:48:46 +02:00
|
|
|
class EditionStore {
|
2015-05-26 13:48:00 +02:00
|
|
|
constructor() {
|
2015-12-09 13:21:55 +01:00
|
|
|
this.edition = {};
|
2015-12-08 13:28:47 +01:00
|
|
|
this.editionMeta = {
|
|
|
|
err: null,
|
|
|
|
idToFetch: null
|
|
|
|
};
|
|
|
|
this.coaMeta = {
|
|
|
|
err: null
|
|
|
|
};
|
|
|
|
|
2015-06-01 18:20:15 +02:00
|
|
|
this.bindActions(EditionActions);
|
2015-12-15 16:26:49 +01:00
|
|
|
this.registerAsync(mergeOptions(EditionSource, CoaSource));
|
2015-12-08 13:28:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onFetchEdition(idToFetch) {
|
|
|
|
this.editionMeta.idToFetch = idToFetch;
|
|
|
|
|
2015-12-09 13:04:15 +01:00
|
|
|
this.getInstance().lookupEdition();
|
2015-05-26 13:48:00 +02:00
|
|
|
}
|
|
|
|
|
2015-12-17 15:46:30 +01:00
|
|
|
onSuccessFetchEdition({ edition }) {
|
|
|
|
if (edition) {
|
|
|
|
this.edition = edition;
|
2015-12-08 17:38:04 +01:00
|
|
|
this.editionMeta.err = null;
|
|
|
|
this.editionMeta.idToFetch = null;
|
2015-12-08 13:28:47 +01:00
|
|
|
|
2015-12-08 17:38:04 +01:00
|
|
|
if (this.edition.coa && this.edition.acl.acl_coa &&
|
|
|
|
typeof this.edition.coa.constructor !== Object) {
|
|
|
|
this.getInstance().lookupCoa();
|
2015-12-17 15:46:30 +01:00
|
|
|
} else if (!this.edition.coa && this.edition.acl.acl_coa) {
|
2015-12-08 17:38:04 +01:00
|
|
|
this.getInstance().performCreateCoa();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.editionMeta.err = new Error('Problem fetching the edition');
|
2015-12-08 13:28:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 15:46:30 +01:00
|
|
|
onSuccessFetchCoa({ coa }) {
|
|
|
|
if (coa && Object.keys(this.edition).length) {
|
|
|
|
this.edition.coa = coa;
|
2015-12-08 17:38:04 +01:00
|
|
|
this.coaMeta.err = null;
|
|
|
|
} else {
|
|
|
|
this.coaMeta.err = new Error('Problem generating/fetching the COA');
|
|
|
|
}
|
2015-12-08 13:28:47 +01:00
|
|
|
}
|
|
|
|
|
2015-12-08 14:18:31 +01:00
|
|
|
onFlushEdition() {
|
2015-12-09 13:21:55 +01:00
|
|
|
this.edition = {};
|
2015-12-08 14:18:31 +01:00
|
|
|
this.editionMeta = {
|
|
|
|
err: null,
|
|
|
|
idToFetch: null
|
|
|
|
};
|
|
|
|
this.coaMeta = {
|
|
|
|
err: null
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onErrorEdition(err) {
|
2015-12-08 13:28:47 +01:00
|
|
|
this.editionMeta.err = err;
|
2015-10-19 15:29:57 +02:00
|
|
|
}
|
|
|
|
|
2015-12-08 14:18:31 +01:00
|
|
|
onErrorCoa(err) {
|
2015-12-17 15:46:30 +01:00
|
|
|
// 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;
|
|
|
|
}
|
2015-05-26 13:48:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 08:44:44 +02:00
|
|
|
export default alt.createStore(EditionStore, 'EditionStore');
|