2017-05-26 06:56:54 -04:00
|
|
|
import Visibility from 'visibilityjs';
|
2019-10-10 05:06:08 -04:00
|
|
|
import PipelineStore from './stores/pipeline_store';
|
2017-10-02 08:32:53 -04:00
|
|
|
import Flash from '../flash';
|
2017-05-26 06:56:54 -04:00
|
|
|
import Poll from '../lib/utils/poll';
|
2018-02-16 07:07:04 -05:00
|
|
|
import { __ } from '../locale';
|
2017-05-26 06:56:54 -04:00
|
|
|
import PipelineService from './services/pipeline_service';
|
|
|
|
|
|
|
|
export default class pipelinesMediator {
|
2017-05-26 10:05:43 -04:00
|
|
|
constructor(options = {}) {
|
|
|
|
this.options = options;
|
2017-05-26 06:56:54 -04:00
|
|
|
this.store = new PipelineStore();
|
|
|
|
this.service = new PipelineService(options.endpoint);
|
|
|
|
|
|
|
|
this.state = {};
|
|
|
|
this.state.isLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchPipeline() {
|
|
|
|
this.poll = new Poll({
|
|
|
|
resource: this.service,
|
|
|
|
method: 'getPipeline',
|
2019-03-07 05:29:46 -05:00
|
|
|
data: this.store.state.expandedPipelines ? this.getExpandedParameters() : undefined,
|
2017-05-26 06:56:54 -04:00
|
|
|
successCallback: this.successCallback.bind(this),
|
|
|
|
errorCallback: this.errorCallback.bind(this),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!Visibility.hidden()) {
|
|
|
|
this.state.isLoading = true;
|
|
|
|
this.poll.makeRequest();
|
2017-06-02 09:24:42 -04:00
|
|
|
} else {
|
|
|
|
this.refreshPipeline();
|
2017-05-26 06:56:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Visibility.change(() => {
|
|
|
|
if (!Visibility.hidden()) {
|
|
|
|
this.poll.restart();
|
|
|
|
} else {
|
2020-01-08 07:07:59 -05:00
|
|
|
this.stopPipelinePoll();
|
2017-05-26 06:56:54 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
successCallback(response) {
|
2018-04-11 09:54:19 -04:00
|
|
|
this.state.isLoading = false;
|
|
|
|
this.store.storePipeline(response.data);
|
2017-05-26 06:56:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
errorCallback() {
|
|
|
|
this.state.isLoading = false;
|
2018-02-16 07:07:04 -05:00
|
|
|
Flash(__('An error occurred while fetching the pipeline.'));
|
2017-05-26 06:56:54 -04:00
|
|
|
}
|
2017-06-02 09:24:42 -04:00
|
|
|
|
|
|
|
refreshPipeline() {
|
2020-01-08 07:07:59 -05:00
|
|
|
this.stopPipelinePoll();
|
2018-04-06 11:42:19 -04:00
|
|
|
|
2018-06-27 10:28:05 -04:00
|
|
|
return this.service
|
|
|
|
.getPipeline()
|
2017-06-02 09:24:42 -04:00
|
|
|
.then(response => this.successCallback(response))
|
2018-04-06 11:42:19 -04:00
|
|
|
.catch(() => this.errorCallback())
|
2019-03-07 05:29:46 -05:00
|
|
|
.finally(() =>
|
|
|
|
this.poll.restart(
|
|
|
|
this.store.state.expandedPipelines ? this.getExpandedParameters() : undefined,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-08 07:07:59 -05:00
|
|
|
stopPipelinePoll() {
|
|
|
|
this.poll.stop();
|
|
|
|
}
|
|
|
|
|
2019-03-07 05:29:46 -05:00
|
|
|
/**
|
|
|
|
* Backend expects paramets in the following format: `expanded[]=id&expanded[]=id`
|
|
|
|
*/
|
|
|
|
getExpandedParameters() {
|
|
|
|
return {
|
|
|
|
expanded: this.store.state.expandedPipelines,
|
|
|
|
};
|
2017-06-02 09:24:42 -04:00
|
|
|
}
|
2017-05-26 06:56:54 -04:00
|
|
|
}
|