onion/js/components/ascribe_detail/edition_container.js

107 lines
3.1 KiB
JavaScript
Raw Normal View History

'use strict';
2015-05-26 10:52:20 +02:00
import React from 'react';
2015-10-19 15:29:57 +02:00
import { History } from 'react-router';
2015-05-26 10:52:20 +02:00
import ReactError from '../../mixins/react_error';
import { ResourceNotFoundError } from '../../models/errors';
2015-05-26 10:52:20 +02:00
2015-07-08 22:54:07 +02:00
import EditionActions from '../../actions/edition_actions';
import EditionStore from '../../stores/edition_store';
2015-05-26 10:52:20 +02:00
2015-07-08 22:54:07 +02:00
import Edition from './edition';
2015-05-26 10:52:20 +02:00
import AscribeSpinner from '../ascribe_spinner';
2015-09-02 14:02:23 +02:00
import { getLangText } from '../../utils/lang_utils';
2015-10-13 17:52:45 +02:00
import { setDocumentTitle } from '../../utils/dom_utils';
2015-09-02 14:02:23 +02:00
2015-05-26 10:52:20 +02:00
/**
* This is the component that implements resource/data specific functionality
*/
2015-05-26 13:48:46 +02:00
let EditionContainer = React.createClass({
propTypes: {
actionPanelButtonListType: React.PropTypes.func,
furtherDetailsType: React.PropTypes.func,
// Provided from AscribeApp
currentUser: React.PropTypes.object.isRequired,
whitelabel: React.PropTypes.object.isRequired,
2016-01-11 16:26:36 +01:00
// Provided from router
location: React.PropTypes.object,
params: React.PropTypes.object
},
mixins: [History, ReactError],
2015-10-19 15:29:57 +02:00
2015-05-26 10:52:20 +02:00
getInitialState() {
2016-02-05 10:38:59 +01:00
return EditionStore.getInitialState();
2015-05-26 10:52:20 +02:00
},
componentDidMount() {
2015-05-26 13:48:46 +02:00
EditionStore.listen(this.onChange);
2016-02-05 10:38:59 +01:00
this.loadEdition();
2015-05-26 10:52:20 +02:00
},
2015-09-01 14:45:14 +02:00
// This is done to update the container when the user clicks on the prev or next
// button to update the URL parameter (and therefore to switch pieces)
componentWillReceiveProps(nextProps) {
if (this.props.params.editionId !== nextProps.params.editionId) {
2016-02-05 10:38:59 +01:00
EditionActions.flushEdition();
this.loadEdition(nextProps.params.editionId);
2015-09-01 14:45:14 +02:00
}
},
2015-10-19 15:29:57 +02:00
componentDidUpdate() {
2016-02-05 10:38:59 +01:00
const { err: editionErr } = this.state.editionMeta;
if (editionErr && editionErr.json && editionErr.json.status === 404) {
2015-12-07 11:48:28 +01:00
this.throws(new ResourceNotFoundError(getLangText("Oops, the edition you're looking for doesn't exist.")));
2015-10-19 15:29:57 +02:00
}
},
componentWillUnmount() {
2015-07-03 15:05:14 +02:00
window.clearInterval(this.state.timerId);
2015-05-26 13:48:46 +02:00
EditionStore.unlisten(this.onChange);
2015-05-26 10:52:20 +02:00
},
onChange(state) {
this.setState(state);
2015-06-05 14:22:02 +02:00
},
2016-02-05 10:38:59 +01:00
loadEdition(editionId = this.props.params.editionId) {
EditionActions.fetchEdition(editionId);
},
2015-05-26 10:52:20 +02:00
render() {
const { actionPanelButtonListType, currentUser, furtherDetailsType, whitelabel } = this.props;
const { edition, coaMeta } = this.state;
2016-02-05 10:38:59 +01:00
if (edition.id) {
setDocumentTitle(`${edition.artist_name}, ${edition.title}`);
2015-05-26 10:52:20 +02:00
return (
<Edition
actionPanelButtonListType={actionPanelButtonListType}
2015-12-09 11:41:46 +01:00
coaError={coaMeta.err}
currentUser={currentUser}
edition={edition}
furtherDetailsType={furtherDetailsType}
2016-02-05 10:38:59 +01:00
loadEdition={this.loadEdition}
whitelabel={whitelabel} />
2015-05-26 10:52:20 +02:00
);
} else {
return (
<div className="fullpage-spinner">
<AscribeSpinner color='dark-blue' size='lg'/>
</div>
2015-05-26 10:52:20 +02:00
);
}
}
});
2015-05-26 13:48:46 +02:00
export default EditionContainer;