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

80 lines
2 KiB
JavaScript
Raw Normal View History

2017-09-10 17:25:29 +05:30
import Visibility from 'visibilityjs';
2020-10-24 23:57:45 +05:30
import { deprecatedCreateFlash as 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';
2021-03-11 19:13:27 +05:30
import PipelineStore from './stores/pipeline_store';
2017-09-10 17:25:29 +05:30
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 {
2020-03-13 15:44:24 +05:30
this.stopPipelinePoll();
2017-09-10 17:25:29 +05:30
}
});
}
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() {
2020-03-13 15:44:24 +05:30
this.stopPipelinePoll();
2018-05-09 12:01:36 +05:30
2018-11-08 19:23:39 +05:30
return this.service
.getPipeline()
2021-03-08 18:12:59 +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,
),
);
}
2020-03-13 15:44:24 +05:30
stopPipelinePoll() {
this.poll.stop();
}
2019-05-03 19:53:19 +05:30
/**
* 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
}
}