1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 01:39:36 +01:00
onion/js/components/ascribe_buttons/create_editions_button.js

103 lines
3.0 KiB
JavaScript
Raw Permalink 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 AscribeSpinner from '../ascribe_spinner';
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
},
componentDidUpdate() {
if(this.props.piece.num_editions === 0 && typeof this.state.pollingIntervalIndex === 'undefined') {
this.startPolling();
}
},
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);
},
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({
pieceId: this.props.piece.id,
filterBy: {}
})
.then((res) => {
clearInterval(this.state.pollingIntervalIndex);
this.props.onPollingSuccess(this.props.piece.id, res.editions[0].num_editions);
})
.catch((err) => {
/* Ignore and keep going */
});
2015-07-13 17:09:44 +02:00
}, 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)}>
{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}>
{this.props.label}
2015-07-13 17:09:44 +02:00
</button>
);
2015-07-13 14:10:46 +02:00
}
}
});
export default CreateEditionsButton;