fa2af5e0f5
Reduced the technical debt around our JS flash function by making it a module that is imported rather than relying on the global function. The global function still exists mainly for technical debt with how some requests are being completed, but new JS should import the module directly. Also reduces some tech debt in the file by removing the need for jQuery. Instead Flash is now 100% vanilla JS.
58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
import Visibility from 'visibilityjs';
|
|
import Flash from '../flash';
|
|
import Poll from '../lib/utils/poll';
|
|
import PipelineStore from './stores/pipeline_store';
|
|
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',
|
|
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) {
|
|
return response.json().then((data) => {
|
|
this.state.isLoading = false;
|
|
this.store.storePipeline(data);
|
|
});
|
|
}
|
|
|
|
errorCallback() {
|
|
this.state.isLoading = false;
|
|
return new Flash('An error occurred while fetching the pipeline.');
|
|
}
|
|
|
|
refreshPipeline() {
|
|
this.service.getPipeline()
|
|
.then(response => this.successCallback(response))
|
|
.catch(() => this.errorCallback());
|
|
}
|
|
}
|