1
0
mirror of https://github.com/ascribe/onion.git synced 2024-11-15 09:35:10 +01:00
onion/js/components/ascribe_buttons/create_editions_button.js

96 lines
2.8 KiB
JavaScript
Raw Normal View History

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
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: {
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-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
className={classNames('btn', 'btn-default', this.props.className)}>
{getLangText('Creating editions')} <span className="glyph-ascribe-spool-chunked spin"/>
2015-07-13 17:09:44 +02:00
</button>
);
} else {
return (
2015-07-13 18:13:16 +02:00
<button
className={classNames('btn', 'btn-default', this.props.className)}
2015-07-13 17:09:44 +02:00
onClick={this.props.toggleCreateEditionsDialog}>
{this.props.label}
2015-07-13 17:09:44 +02:00
</button>
);
2015-07-13 14:10:46 +02:00
}
}
});
export default CreateEditionsButton;