gitlab-org--gitlab-foss/app/assets/javascripts/clusters.js

122 lines
3.3 KiB
JavaScript
Raw Normal View History

/* globals Flash */
import Visibility from 'visibilityjs';
import axios from 'axios';
import Poll from './lib/utils/poll';
import { s__ } from './locale';
import initSettingsPanels from './settings_panels';
import Flash from './flash';
/**
* Cluster page has 2 separate parts:
2017-10-04 11:48:34 +00:00
* Toggle button
*
* - Polling status while creating or scheduled
* -- Update status area with the response result
*/
2017-10-04 11:48:34 +00:00
class ClusterService {
constructor(options = {}) {
this.options = options;
}
fetchData() {
return axios.get(this.options.endpoint);
}
}
2017-10-04 15:43:45 +00:00
export default class Clusters {
constructor() {
initSettingsPanels();
const dataset = document.querySelector('.js-edit-cluster-form').dataset;
this.state = {
2017-10-04 11:48:34 +00:00
statusPath: dataset.statusPath,
clusterStatus: dataset.clusterStatus,
2017-10-04 11:48:34 +00:00
clusterStatusReason: dataset.clusterStatusReason,
toggleStatus: dataset.toggleStatus,
};
2017-10-04 11:48:34 +00:00
this.service = new ClusterService({ endpoint: this.state.statusPath });
this.toggleButton = document.querySelector('.js-toggle-cluster');
2017-10-04 11:48:34 +00:00
this.toggleInput = document.querySelector('.js-toggle-input');
this.errorContainer = document.querySelector('.js-cluster-error');
this.successContainer = document.querySelector('.js-cluster-success');
this.creatingContainer = document.querySelector('.js-cluster-creating');
2017-10-04 22:47:03 +00:00
this.errorReasonContainer = this.errorContainer.querySelector('.js-error-reason');
2017-10-04 11:48:34 +00:00
this.toggleButton.addEventListener('click', this.toggle.bind(this));
2017-10-04 11:48:34 +00:00
if (this.state.clusterStatus !== 'created') {
this.updateContainer(this.state.clusterStatus, this.state.clusterStatusReason);
}
2017-10-04 11:48:34 +00:00
if (this.state.statusPath) {
2017-10-04 22:47:03 +00:00
this.initPolling();
2017-10-04 11:48:34 +00:00
}
}
toggle() {
this.toggleButton.classList.toggle('checked');
2017-10-04 15:43:45 +00:00
this.toggleInput.setAttribute('value', this.toggleButton.classList.contains('checked').toString());
}
2017-10-04 22:47:03 +00:00
initPolling() {
this.poll = new Poll({
resource: this.service,
method: 'fetchData',
successCallback: data => this.handleSuccess(data),
errorCallback: () => Clusters.handleError(),
});
if (!Visibility.hidden()) {
this.poll.makeRequest();
} else {
this.service.fetchData()
.then(data => this.handleSuccess(data))
.catch(() => Clusters.handleError());
}
Visibility.change(() => {
if (!Visibility.hidden()) {
this.poll.restart();
} else {
this.poll.stop();
}
});
}
static handleError() {
Flash(s__('ClusterIntegration|Something went wrong on our end.'));
}
handleSuccess(data) {
const { status, status_reason } = data.data;
this.updateContainer(status, status_reason);
}
hideAll() {
this.errorContainer.classList.add('hidden');
this.successContainer.classList.add('hidden');
this.creatingContainer.classList.add('hidden');
}
updateContainer(status, error) {
this.hideAll();
switch (status) {
case 'created':
this.successContainer.classList.remove('hidden');
break;
2017-10-04 11:48:34 +00:00
case 'errored':
this.errorContainer.classList.remove('hidden');
2017-10-04 22:47:03 +00:00
this.errorReasonContainer.textContent = error;
break;
2017-10-04 11:48:34 +00:00
case 'scheduled':
case 'creating':
2017-10-04 11:48:34 +00:00
this.creatingContainer.classList.remove('hidden');
break;
default:
this.hideAll();
}
}
}