/** * Common code between environmets app and folder view */ import { isEqual, isFunction, omitBy } from 'lodash'; import Visibility from 'visibilityjs'; import createFlash from '~/flash'; import Poll from '../../lib/utils/poll'; import { getParameterByName } from '../../lib/utils/url_utility'; import { s__, __ } from '../../locale'; import tabs from '../../vue_shared/components/navigation_tabs.vue'; import tablePagination from '../../vue_shared/components/pagination/table_pagination.vue'; import container from '../components/container.vue'; import environmentTable from '../components/environments_table.vue'; import eventHub from '../event_hub'; import EnvironmentsService from '../services/environments_service'; import EnvironmentsStore from '../stores/environments_store'; export default { components: { environmentTable, container, tabs, tablePagination, }, data() { const store = new EnvironmentsStore(); const isDetailView = document.body.contains( document.getElementById('environments-detail-view'), ); return { store, state: store.state, isLoading: false, isMakingRequest: false, scope: getParameterByName('scope') || 'available', page: getParameterByName('page') || '1', requestData: {}, environmentInStopModal: {}, environmentInDeleteModal: {}, environmentInRollbackModal: {}, isDetailView, }; }, methods: { saveData(resp) { this.isLoading = false; // Prevent the absence of the nested flag from causing mismatches const response = this.filterNilValues(resp.config.params); const request = this.filterNilValues(this.requestData); if (isEqual(response, request)) { this.store.storeAvailableCount(resp.data.available_count); this.store.storeStoppedCount(resp.data.stopped_count); this.store.storeEnvironments(resp.data.environments); this.store.setReviewAppDetails(resp.data.review_app); this.store.setPagination(resp.headers); } }, filterNilValues(obj) { return omitBy(obj, (value) => value === undefined || value === null); }, /** * Handles URL and query parameter changes. * When the user uses the pagination or the tabs, * - update URL * - Make API request to the server with new parameters * - Update the polling function * - Update the internal state */ updateContent(parameters) { this.updateInternalState(parameters); // fetch new data return this.service .fetchEnvironments(this.requestData) .then((response) => { this.successCallback(response); this.poll.enable({ data: this.requestData, response }); }) .catch(() => { this.errorCallback(); // restart polling this.poll.restart(); }); }, errorCallback() { this.isLoading = false; createFlash({ message: s__('Environments|An error occurred while fetching the environments.'), }); }, postAction({ endpoint, errorMessage = s__('Environments|An error occurred while making the request.'), }) { if (!this.isMakingRequest) { this.isLoading = true; this.service .postAction(endpoint) .then(() => { // Originally, the detail page buttons were implemented as