2015-07-13 14:10:46 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
2015-07-13 17:09:44 +02:00
|
|
|
import EditionListActions from '../../actions/edition_list_actions';
|
2015-07-13 18:13:16 +02:00
|
|
|
import EditionListStore from '../../stores/edition_list_store';
|
2015-07-13 14:10:46 +02:00
|
|
|
|
2015-10-09 16:21:26 +02:00
|
|
|
import AscribeSpinner from '../ascribe_spinner';
|
|
|
|
|
2015-07-13 18:52:55 +02:00
|
|
|
import { getLangText } from '../../utils/lang_utils';
|
2015-07-13 14:10:46 +02:00
|
|
|
|
2015-07-13 18:13:16 +02:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2015-07-13 14:10:46 +02:00
|
|
|
let CreateEditionsButton = React.createClass({
|
|
|
|
propTypes: {
|
2015-07-13 18:52:55 +02:00
|
|
|
label: React.PropTypes.string,
|
2015-07-13 18:13:16 +02:00
|
|
|
className: React.PropTypes.string,
|
2015-07-13 17:09:44 +02:00
|
|
|
piece: React.PropTypes.object.isRequired,
|
2015-07-13 18:13:16 +02:00
|
|
|
toggleCreateEditionsDialog: React.PropTypes.func.isRequired,
|
|
|
|
onPollingSuccess: React.PropTypes.func
|
2015-07-13 17:09:44 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState() {
|
2015-07-13 18:13:16 +02:00
|
|
|
return EditionListStore.getState();
|
2015-07-13 17:09:44 +02:00
|
|
|
},
|
|
|
|
|
2015-07-13 18:13:16 +02:00
|
|
|
componentDidMount() {
|
|
|
|
EditionListStore.listen(this.onChange);
|
2015-07-13 17:09:44 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2015-07-13 18:13:16 +02:00
|
|
|
EditionListStore.unlisten(this.onChange);
|
2015-07-13 17:09:44 +02:00
|
|
|
clearInterval(this.state.pollingIntervalIndex);
|
2015-07-13 14:10:46 +02:00
|
|
|
},
|
|
|
|
|
2015-07-13 18:13:16 +02:00
|
|
|
onChange(state) {
|
|
|
|
this.setState(state);
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
if(this.props.piece.num_editions === 0 && typeof this.state.pollingIntervalIndex === 'undefined') {
|
|
|
|
this.startPolling();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-07-13 17:09:44 +02:00
|
|
|
startPolling() {
|
|
|
|
// start polling until editions are defined
|
|
|
|
let pollingIntervalIndex = setInterval(() => {
|
2015-08-04 15:06:02 +02:00
|
|
|
|
2015-08-06 13:43:53 +02:00
|
|
|
// requests, will try to merge the filterBy parameter with other parameters (mergeOptions).
|
|
|
|
// Therefore it can't but null but instead has to be an empty object
|
|
|
|
EditionListActions.fetchEditionList(this.props.piece.id, null, null, null, null, {})
|
2015-07-13 17:09:44 +02:00
|
|
|
.then((res) => {
|
|
|
|
|
|
|
|
clearInterval(this.state.pollingIntervalIndex);
|
2015-07-13 18:13:16 +02:00
|
|
|
this.props.onPollingSuccess(this.props.piece.id, res.editions[0].num_editions);
|
2015-07-13 17:09:44 +02:00
|
|
|
|
|
|
|
})
|
2015-08-06 13:43:53 +02:00
|
|
|
.catch((err) => {
|
2015-07-13 17:09:44 +02:00
|
|
|
/* Ignore and keep going */
|
|
|
|
});
|
|
|
|
}, 5000);
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
pollingIntervalIndex
|
|
|
|
});
|
|
|
|
},
|
2015-07-13 14:10:46 +02:00
|
|
|
|
|
|
|
render: function () {
|
2015-07-13 17:09:44 +02:00
|
|
|
let piece = this.props.piece;
|
|
|
|
|
2015-08-05 16:02:48 +02:00
|
|
|
if (!piece.acl.acl_create_editions || piece.num_editions > 0){
|
2015-07-13 14:10:46 +02:00
|
|
|
return null;
|
|
|
|
}
|
2015-07-13 17:09:44 +02:00
|
|
|
|
2015-07-13 18:13:16 +02:00
|
|
|
if(piece.num_editions === 0 && typeof this.state.editionList[piece.id] === 'undefined') {
|
2015-07-13 17:09:44 +02:00
|
|
|
return (
|
2015-07-13 18:13:16 +02:00
|
|
|
<button
|
|
|
|
disabled
|
2015-10-21 14:52:25 +02:00
|
|
|
className={classNames('btn', this.props.className)}>
|
2015-10-09 16:21:26 +02:00
|
|
|
{getLangText('Creating editions')} <AscribeSpinner
|
|
|
|
size='sm'
|
|
|
|
color='white'
|
|
|
|
classNames='pull-right margin-left-2px'/>
|
2015-07-13 17:09:44 +02:00
|
|
|
</button>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
2015-07-13 18:13:16 +02:00
|
|
|
<button
|
2015-10-21 14:52:25 +02:00
|
|
|
className={classNames('btn', this.props.className)}
|
2015-07-13 17:09:44 +02:00
|
|
|
onClick={this.props.toggleCreateEditionsDialog}>
|
2015-07-13 18:52:55 +02:00
|
|
|
{this.props.label}
|
2015-07-13 17:09:44 +02:00
|
|
|
</button>
|
|
|
|
);
|
2015-07-13 14:10:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default CreateEditionsButton;
|
|
|
|
|