clusters_bundle.js 6.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import Visibility from 'visibilityjs';
import Vue from 'vue';
import { s__, sprintf } from '../locale';
import Flash from '../flash';
import Poll from '../lib/utils/poll';
import initSettingsPanels from '../settings_panels';
import eventHub from './event_hub';
import {
  APPLICATION_INSTALLED,
  REQUEST_LOADING,
  REQUEST_SUCCESS,
  REQUEST_FAILURE,
} from './constants';
import ClustersService from './services/clusters_service';
import ClustersStore from './stores/clusters_store';
import applications from './components/applications.vue';
E
Eric Eastwood 已提交
17
import setupToggleButtons from '../toggle_buttons';
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

/**
 * Cluster page has 2 separate parts:
 * Toggle button and applications section
 *
 * - Polling status while creating or scheduled
 * - Update status area with the response result
 */

export default class Clusters {
  constructor() {
    const {
      statusPath,
      installHelmPath,
      installIngressPath,
33
      installRunnerPath,
34
      installPrometheusPath,
35 36 37 38 39 40 41 42 43 44 45 46
      clusterStatus,
      clusterStatusReason,
      helpPath,
    } = document.querySelector('.js-edit-cluster-form').dataset;

    this.store = new ClustersStore();
    this.store.setHelpPath(helpPath);
    this.store.updateStatus(clusterStatus);
    this.store.updateStatusReason(clusterStatusReason);
    this.service = new ClustersService({
      endpoint: statusPath,
      installHelmEndpoint: installHelmPath,
K
Kamil Trzcinski 已提交
47
      installIngressEndpoint: installIngressPath,
48
      installRunnerEndpoint: installRunnerPath,
49
      installPrometheusEndpoint: installPrometheusPath,
50 51 52
    });

    this.installApplication = this.installApplication.bind(this);
53
    this.showToken = this.showToken.bind(this);
54 55 56 57 58 59

    this.errorContainer = document.querySelector('.js-cluster-error');
    this.successContainer = document.querySelector('.js-cluster-success');
    this.creatingContainer = document.querySelector('.js-cluster-creating');
    this.errorReasonContainer = this.errorContainer.querySelector('.js-error-reason');
    this.successApplicationContainer = document.querySelector('.js-cluster-application-notice');
60 61
    this.showTokenButton = document.querySelector('.js-show-cluster-token');
    this.tokenField = document.querySelector('.js-cluster-token');
62 63

    initSettingsPanels();
E
Eric Eastwood 已提交
64
    setupToggleButtons(document.querySelector('.js-cluster-enable-toggle-area'));
65 66 67
    this.initApplications();

    if (this.store.state.status !== 'created') {
68
      this.updateContainer(null, this.store.state.status, this.store.state.statusReason);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    }

    this.addListeners();
    if (statusPath) {
      this.initPolling();
    }
  }

  initApplications() {
    const store = this.store;
    const el = document.querySelector('#js-cluster-applications');

    this.applications = new Vue({
      el,
      components: {
        applications,
      },
      data() {
        return {
          state: store.state,
        };
      },
      render(createElement) {
        return createElement('applications', {
          props: {
            applications: this.state.applications,
            helpPath: this.state.helpPath,
          },
        });
      },
    });
  }

  addListeners() {
F
Filipa Lacerda 已提交
103
    if (this.showTokenButton) this.showTokenButton.addEventListener('click', this.showToken);
104 105 106 107
    eventHub.$on('installApplication', this.installApplication);
  }

  removeListeners() {
F
Filipa Lacerda 已提交
108
    if (this.showTokenButton) this.showTokenButton.removeEventListener('click', this.showToken);
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    eventHub.$off('installApplication', this.installApplication);
  }

  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.destroyed) {
        this.poll.restart();
      } else {
        this.poll.stop();
      }
    });
  }

  static handleError() {
    Flash(s__('ClusterIntegration|Something went wrong on our end.'));
  }

  handleSuccess(data) {
142
    const prevStatus = this.store.state.status;
143 144
    const prevApplicationMap = Object.assign({}, this.store.state.applications);

145
    this.store.updateStateFromServer(data.data);
146

147
    this.checkForNewInstalls(prevApplicationMap, this.store.state.applications);
148
    this.updateContainer(prevStatus, this.store.state.status, this.store.state.statusReason);
149 150
  }

151 152 153 154 155 156 157 158 159 160
  showToken() {
    const type = this.tokenField.getAttribute('type');

    if (type === 'password') {
      this.tokenField.setAttribute('type', 'text');
    } else {
      this.tokenField.setAttribute('type', 'password');
    }
  }

161 162 163 164 165 166 167 168 169 170 171 172 173 174
  hideAll() {
    this.errorContainer.classList.add('hidden');
    this.successContainer.classList.add('hidden');
    this.creatingContainer.classList.add('hidden');
  }

  checkForNewInstalls(prevApplicationMap, newApplicationMap) {
    const appTitles = Object.keys(newApplicationMap)
      .filter(appId => newApplicationMap[appId].status === APPLICATION_INSTALLED &&
        prevApplicationMap[appId].status !== APPLICATION_INSTALLED &&
        prevApplicationMap[appId].status !== null)
      .map(appId => newApplicationMap[appId].title);

    if (appTitles.length > 0) {
E
Eric Eastwood 已提交
175
      const text = sprintf(s__('ClusterIntegration|%{appList} was successfully installed on your cluster'), {
176 177
        appList: appTitles.join(', '),
      });
E
Eric Eastwood 已提交
178
      Flash(text, 'notice', this.successApplicationContainer);
179 180 181
    }
  }

182
  updateContainer(prevStatus, status, error) {
183
    this.hideAll();
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

    // We poll all the time but only want the `created` banner to show when newly created
    if (this.store.state.status !== 'created' || prevStatus !== this.store.state.status) {
      switch (status) {
        case 'created':
          this.successContainer.classList.remove('hidden');
          break;
        case 'errored':
          this.errorContainer.classList.remove('hidden');
          this.errorReasonContainer.textContent = error;
          break;
        case 'scheduled':
        case 'creating':
          this.creatingContainer.classList.remove('hidden');
          break;
        default:
          this.hideAll();
      }
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    }
  }

  installApplication(appId) {
    this.store.updateAppProperty(appId, 'requestStatus', REQUEST_LOADING);
    this.store.updateAppProperty(appId, 'requestReason', null);

    this.service.installApplication(appId)
      .then(() => {
        this.store.updateAppProperty(appId, 'requestStatus', REQUEST_SUCCESS);
      })
      .catch(() => {
        this.store.updateAppProperty(appId, 'requestStatus', REQUEST_FAILURE);
        this.store.updateAppProperty(appId, 'requestReason', s__('ClusterIntegration|Request to begin installing failed'));
      });
  }

  destroy() {
    this.destroyed = true;

    this.removeListeners();

    if (this.poll) {
      this.poll.stop();
    }

    this.applications.$destroy();
  }
}