// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import React from 'react';
import PropTypes from 'prop-types';
import {FormattedMessage} from 'react-intl';
import {createJob, cancelJob} from 'actions/job_actions.jsx';
import {JobTypes, JobStatuses} from 'utils/constants.jsx';
import RequestButton from '../request_button/request_button.jsx';
export default class Status extends React.PureComponent {
static propTypes = {
/**
* Array of jobs
*/
jobs: PropTypes.arrayOf(PropTypes.object).isRequired,
/**
* Whether Elasticsearch is properly configured.
*/
isConfigured: PropTypes.bool.isRequired,
actions: PropTypes.shape({
/**
* Function to fetch jobs
*/
getJobsByType: PropTypes.func.isRequired
}).isRequired
};
constructor(props) {
super(props);
this.interval = null;
this.state = {
loading: true,
cancelInProgress: false
};
}
componentWillMount() {
// reload the cluster status every 15 seconds
this.interval = setInterval(this.reload, 15000);
}
componentDidMount() {
this.props.actions.getJobsByType(JobTypes.ELASTICSEARCH_POST_INDEXING).then(
() => this.setState({loading: false})
);
}
componentWillUnmount() {
if (this.interval) {
clearInterval(this.interval);
}
}
reload = () => {
this.props.actions.getJobsByType(JobTypes.ELASTICSEARCH_POST_INDEXING).then(
() => {
this.setState({
loading: false,
cancelInProgress: false
});
}
);
};
createIndexJob = (success, error) => {
const job = {
type: JobTypes.ELASTICSEARCH_POST_INDEXING
};
createJob(
job,
() => {
this.reload();
success();
},
error
);
};
cancelIndexJob = (e) => {
e.preventDefault();
const chosenJob = this.getChosenJob();
if (!chosenJob) {
return;
}
this.setState({
cancelInProgress: true
});
cancelJob(
chosenJob.id,
() => {
this.reload();
},
() => {
this.reload();
}
);
};
getChosenJob = () => {
let chosenJob = null;
if (this.props.jobs.length > 0) {
for (let i = 0; i < this.props.jobs.length; i++) {
const job = this.props.jobs[i];
if (job.status === JobStatuses.CANCEL_REQUESTED || job.status === JobStatuses.IN_PROGRESS) {
chosenJob = job;
} else {
break;
}
}
if (!chosenJob) {
for (let i = 0; i < this.props.jobs.length; i++) {
const job = this.props.jobs[i];
if (job.status !== JobStatuses.PENDING && chosenJob) {
continue;
} else {
chosenJob = job;
break;
}
}
}
}
return chosenJob;
};
render() {
const chosenJob = this.getChosenJob();
let indexButtonDisabled = !this.props.isConfigured;
let buttonText = (