1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 01:39:36 +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() { constructor() {
this.generateActions( this.generateActions(
'fetchEdition', 'fetchEdition',
'successFetchEdition',
'successFetchCoa', 'successFetchCoa',
'flushEdition', 'successFetchEdition',
'errorCoa', 'errorCoa',
'errorEdition' 'errorEdition',
'flushEdition'
); );
} }
} }

View File

@ -35,7 +35,7 @@ let EditionContainer = React.createClass({
getInitialState() { getInitialState() {
return mergeOptions( return mergeOptions(
EditionStore.getState(), EditionStore.getInitialState(),
UserStore.getState() UserStore.getState()
); );
}, },
@ -44,13 +44,7 @@ let EditionContainer = React.createClass({
EditionStore.listen(this.onChange); EditionStore.listen(this.onChange);
UserStore.listen(this.onChange); UserStore.listen(this.onChange);
// Every time we're entering the edition detail page, this.loadEdition();
// 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);
UserActions.fetchCurrentUser(); UserActions.fetchCurrentUser();
}, },
@ -58,13 +52,13 @@ let EditionContainer = React.createClass({
// button to update the URL parameter (and therefore to switch pieces) // button to update the URL parameter (and therefore to switch pieces)
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
if(this.props.params.editionId !== nextProps.params.editionId) { if(this.props.params.editionId !== nextProps.params.editionId) {
EditionActions.fetchEdition(this.props.params.editionId); this.loadEdition(nextProps.params.editionId);
} }
}, },
componentDidUpdate() { componentDidUpdate() {
const { editionMeta } = this.state; const { editionErr } = this.state.editionMeta;
if(editionMeta.err && editionMeta.err.json && editionMeta.err.json.status === 404) { if (editionErr && editionErr.json && editionErr.json.status === 404) {
this.throws(new ResourceNotFoundError(getLangText("Oops, the edition you're looking for doesn't exist."))); 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) { if(state && state.edition && state.edition.digital_work) {
let isEncoding = state.edition.digital_work.isEncoding; let isEncoding = state.edition.digital_work.isEncoding;
if (state.edition.digital_work.mime === 'video' && typeof isEncoding === 'number' && isEncoding !== 100 && !this.state.timerId) { 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}); this.setState({timerId: timerId});
} }
} }
}, },
loadEdition(editionId = this.props.params.editionId) {
EditionActions.fetchEdition(editionId);
},
render() { render() {
const { edition, currentUser, coaMeta } = this.state; const { edition, currentUser, coaMeta } = this.state;
const { actionPanelButtonListType, furtherDetailsType } = this.props; const { actionPanelButtonListType, furtherDetailsType } = this.props;
@ -101,7 +99,7 @@ let EditionContainer = React.createClass({
edition={edition} edition={edition}
coaError={coaMeta.err} coaError={coaMeta.err}
currentUser={currentUser} currentUser={currentUser}
loadEdition={() => EditionActions.fetchEdition(this.props.params.editionId)} /> loadEdition={this.loadEdition} />
); );
} else { } else {
return ( return (

View File

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