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() {
|
2016-01-13 17:24:32 +01:00
|
|
|
this.getInitialState();
|
|
|
|
|
|
|
|
this.bindActions(EditionActions);
|
|
|
|
this.registerAsync(mergeOptions(EditionSource, CoaSource));
|
|
|
|
this.exportPublicMethods({
|
|
|
|
getInitialState: this.getInitialState.bind(this)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getInitialState() {
|
2015-12-09 13:21:55 +01:00
|
|
|
this.edition = {};
|
2015-12-08 13:28:47 +01:00
|
|
|
this.editionMeta = {
|
2016-01-15 13:55:58 +01:00
|
|
|
err: null
|
2015-12-08 13:28:47 +01:00
|
|
|
};
|
|
|
|
this.coaMeta = {
|
|
|
|
err: null
|
|
|
|
};
|
|
|
|
|
2016-01-13 17:24:32 +01:00
|
|
|
return {
|
|
|
|
edition: this.edition,
|
|
|
|
editionMeta: this.editionMeta,
|
|
|
|
coaMeta: this.coaMeta
|
|
|
|
};
|
2015-12-08 13:28:47 +01:00
|
|
|
}
|
|
|
|
|
2016-01-15 13:55:58 +01:00
|
|
|
onFetchEdition(editionId) {
|
|
|
|
this.getInstance().lookupEdition(editionId);
|
2015-12-08 13:28:47 +01:00
|
|
|
|
2016-01-15 13:56:56 +01:00
|
|
|
// Prevent alt from sending an empty change event when a request is sent
|
|
|
|
// off to the source
|
|
|
|
this.preventDefault();
|
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;
|
2015-12-08 13:28:47 +01:00
|
|
|
|
2016-01-15 13:55:58 +01:00
|
|
|
// Also fetch coa if allowed
|
|
|
|
if (edition.acl.acl_coa) {
|
|
|
|
if (edition.coa && typeof edition.coa.constructor !== Object) {
|
|
|
|
this.getInstance().lookupCoa(edition.coa);
|
|
|
|
} else if (!edition.coa) {
|
|
|
|
this.getInstance().performCreateCoaForEdition(edition.bitcoin_id);
|
|
|
|
}
|
2015-12-08 17:38:04 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.editionMeta.err = new Error('Problem fetching the edition');
|
2016-01-15 13:55:58 +01:00
|
|
|
console.logGlobal(this.editionMeta.err);
|
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');
|
2016-01-15 13:55:58 +01:00
|
|
|
console.logGlobal(this.coaMeta.err);
|
2015-12-08 17:38:04 +01:00
|
|
|
}
|
2015-12-08 13:28:47 +01:00
|
|
|
}
|
|
|
|
|
2015-12-08 14:18:31 +01:00
|
|
|
onErrorEdition(err) {
|
2016-01-15 13:55:58 +01:00
|
|
|
console.logGlobal(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) {
|
2016-01-15 13:55:58 +01:00
|
|
|
this.getInstance().performCreateCoaForEdition(this.edition.bitcoin_id);
|
2015-12-17 15:46:30 +01:00
|
|
|
} else {
|
2016-01-15 13:55:58 +01:00
|
|
|
console.logGlobal(err);
|
2015-12-17 15:46:30 +01:00
|
|
|
this.coaMeta.err = err;
|
|
|
|
}
|
2015-05-26 13:48:00 +02:00
|
|
|
}
|
2016-01-13 17:24:32 +01:00
|
|
|
|
|
|
|
onFlushEdition() {
|
|
|
|
this.getInitialState();
|
|
|
|
}
|
2015-05-26 13:48:00 +02:00
|
|
|
}
|
|
|
|
|
2015-06-15 08:44:44 +02:00
|
|
|
export default alt.createStore(EditionStore, 'EditionStore');
|