debian-mirror-gitlab/app/assets/javascripts/pipelines/pipeline_details_mediator.js

76 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-09-10 17:25:29 +05:30
import Visibility from 'visibilityjs';
2019-09-30 21:07:59 +05:30
import PipelineStore from 'ee_else_ce/pipelines/stores/pipeline_store';
2018-03-17 18:26:18 +05:30
import Flash from '../flash';
2017-09-10 17:25:29 +05:30
import Poll from '../lib/utils/poll';
2018-03-27 19:54:05 +05:30
import { __ } from '../locale';
2017-09-10 17:25:29 +05:30
import PipelineService from './services/pipeline_service';
export default class pipelinesMediator {
constructor(options = {}) {
this.options = options;
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-05-03 19:53:19 +05:30
data: this.store.state.expandedPipelines ? this.getExpandedParameters() : undefined,
2017-09-10 17:25:29 +05:30
successCallback: this.successCallback.bind(this),
errorCallback: this.errorCallback.bind(this),
});
if (!Visibility.hidden()) {
this.state.isLoading = true;
this.poll.makeRequest();
} else {
this.refreshPipeline();
}
Visibility.change(() => {
if (!Visibility.hidden()) {
this.poll.restart();
} else {
this.poll.stop();
}
});
}
successCallback(response) {
2018-10-15 14:42:47 +05:30
this.state.isLoading = false;
this.store.storePipeline(response.data);
2017-09-10 17:25:29 +05:30
}
errorCallback() {
this.state.isLoading = false;
2018-03-27 19:54:05 +05:30
Flash(__('An error occurred while fetching the pipeline.'));
2017-09-10 17:25:29 +05:30
}
refreshPipeline() {
2018-05-09 12:01:36 +05:30
this.poll.stop();
2018-11-08 19:23:39 +05:30
return this.service
.getPipeline()
2017-09-10 17:25:29 +05:30
.then(response => this.successCallback(response))
2018-05-09 12:01:36 +05:30
.catch(() => this.errorCallback())
2019-05-03 19:53:19 +05:30
.finally(() =>
this.poll.restart(
this.store.state.expandedPipelines ? this.getExpandedParameters() : undefined,
),
);
}
/**
* Backend expects paramets in the following format: `expanded[]=id&expanded[]=id`
*/
getExpandedParameters() {
return {
expanded: this.store.state.expandedPipelines,
};
2017-09-10 17:25:29 +05:30
}
}