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

Add getInitialState pattern for EditionStore

This commit is contained in:
Brett Sun 2016-01-13 17:24:32 +01:00
parent ea18b31db1
commit a5442e5acd
3 changed files with 33 additions and 29 deletions

View File

@ -7,11 +7,11 @@ class EditionActions {
constructor() {
this.generateActions(
'fetchEdition',
'successFetchEdition',
'successFetchCoa',
'flushEdition',
'successFetchEdition',
'errorCoa',
'errorEdition'
'errorEdition',
'flushEdition'
);
}
}

View File

@ -35,7 +35,7 @@ let EditionContainer = React.createClass({
getInitialState() {
return mergeOptions(
EditionStore.getState(),
EditionStore.getInitialState(),
UserStore.getState()
);
},
@ -44,13 +44,7 @@ let EditionContainer = React.createClass({
EditionStore.listen(this.onChange);
UserStore.listen(this.onChange);
// Every time we're entering the edition detail page,
// just reset the edition that is saved in the edition store
// as it will otherwise display wrong/old data once the user loads
// the edition detail a second time
EditionActions.flushEdition();
EditionActions.fetchEdition(this.props.params.editionId);
this.loadEdition();
UserActions.fetchCurrentUser();
},
@ -58,13 +52,13 @@ let EditionContainer = React.createClass({
// button to update the URL parameter (and therefore to switch pieces)
componentWillReceiveProps(nextProps) {
if(this.props.params.editionId !== nextProps.params.editionId) {
EditionActions.fetchEdition(this.props.params.editionId);
this.loadEdition(nextProps.params.editionId);
}
},
componentDidUpdate() {
const { editionMeta } = this.state;
if(editionMeta.err && editionMeta.err.json && editionMeta.err.json.status === 404) {
const { editionErr } = this.state.editionMeta;
if (editionErr && editionErr.json && editionErr.json.status === 404) {
this.throws(new ResourceNotFoundError(getLangText("Oops, the edition you're looking for doesn't exist.")));
}
},
@ -81,12 +75,16 @@ let EditionContainer = React.createClass({
if(state && state.edition && state.edition.digital_work) {
let isEncoding = state.edition.digital_work.isEncoding;
if (state.edition.digital_work.mime === 'video' && typeof isEncoding === 'number' && isEncoding !== 100 && !this.state.timerId) {
let timerId = window.setInterval(() => EditionActions.fetchOne(this.props.params.editionId), 10000);
let timerId = window.setInterval(() => EditionActions.fetchEdition(this.props.params.editionId), 10000);
this.setState({timerId: timerId});
}
}
},
loadEdition(editionId = this.props.params.editionId) {
EditionActions.fetchEdition(editionId);
},
render() {
const { edition, currentUser, coaMeta } = this.state;
const { actionPanelButtonListType, furtherDetailsType } = this.props;
@ -101,7 +99,7 @@ let EditionContainer = React.createClass({
edition={edition}
coaError={coaMeta.err}
currentUser={currentUser}
loadEdition={() => EditionActions.fetchEdition(this.props.params.editionId)} />
loadEdition={this.loadEdition} />
);
} else {
return (

View File

@ -12,6 +12,16 @@ import { mergeOptions } from '../utils/general_utils';
class EditionStore {
constructor() {
this.getInitialState();
this.bindActions(EditionActions);
this.registerAsync(mergeOptions(EditionSource, CoaSource));
this.exportPublicMethods({
getInitialState: this.getInitialState.bind(this)
});
}
getInitialState() {
this.edition = {};
this.editionMeta = {
err: null,
@ -21,8 +31,11 @@ class EditionStore {
err: null
};
this.bindActions(EditionActions);
this.registerAsync(mergeOptions(EditionSource, CoaSource));
return {
edition: this.edition,
editionMeta: this.editionMeta,
coaMeta: this.coaMeta
};
}
onFetchEdition(idToFetch) {
@ -57,17 +70,6 @@ class EditionStore {
}
}
onFlushEdition() {
this.edition = {};
this.editionMeta = {
err: null,
idToFetch: null
};
this.coaMeta = {
err: null
};
}
onErrorEdition(err) {
this.editionMeta.err = err;
}
@ -80,6 +82,10 @@ class EditionStore {
this.coaMeta.err = err;
}
}
onFlushEdition() {
this.getInitialState();
}
}
export default alt.createStore(EditionStore, 'EditionStore');