All files / app/assets/javascripts/feature_flags/components new_environments_dropdown.vue

94.12% Statements 16/17
75% Branches 3/4
88.89% Functions 8/9
94.12% Lines 16/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 103  6x                 6x 6x 6x                         30x                       3x         2x 2x 2x   9x 9x 9x     7x     2x         9x                 227x                                                                    
<script>
import {
  GlDropdown,
  GlDropdownDivider,
  GlDropdownItem,
  GlIcon,
  GlLoadingIcon,
  GlSearchBoxByType,
} from '@gitlab/ui';
import { debounce } from 'lodash';
import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { __, sprintf } from '~/locale';
 
export default {
  components: {
    GlDropdown,
    GlDropdownDivider,
    GlDropdownItem,
    GlSearchBoxByType,
    GlIcon,
    GlLoadingIcon,
  },
  inject: ['environmentsEndpoint'],
  data() {
    return {
      environmentSearch: '',
      results: [],
      isLoading: false,
    };
  },
  translations: {
    addEnvironmentsLabel: __('Add environment'),
    noResultsLabel: __('No matching results'),
  },
  computed: {
    createEnvironmentLabel() {
      return sprintf(__('Create %{environment}'), { environment: this.environmentSearch });
    },
  },
  methods: {
    addEnvironment(newEnvironment) {
      this.$emit('add', newEnvironment);
      this.environmentSearch = '';
      this.results = [];
    },
    fetchEnvironments: debounce(function debouncedFetchEnvironments() {
      this.isLoading = true;
      axios
        .get(this.environmentsEndpoint, { params: { query: this.environmentSearch } })
        .then(({ data }) => {
          this.results = data || [];
        })
        .catch(() => {
          createFlash({
            message: __('Something went wrong on our end. Please try again.'),
          });
        })
        .finally(() => {
          this.isLoading = false;
        });
    }, 250),
    setFocus() {
      this.$refs.searchBox.focusInput();
    },
  },
};
</script>
<template>
  <gl-dropdown class="js-new-environments-dropdown" @shown="setFocus">
    <template #button-content>
      <span class="d-md-none mr-1">
        {{ $options.translations.addEnvironmentsLabel }}
      </span>
      <gl-icon class="d-none d-md-inline-flex" name="plus" />
    </template>
    <gl-search-box-by-type
      ref="searchBox"
      v-model.trim="environmentSearch"
      @focus="fetchEnvironments"
      @keyup="fetchEnvironments"
    />
    <gl-loading-icon v-if="isLoading" size="sm" />
    <gl-dropdown-item
      v-for="environment in results"
      v-else-if="results.length"
      :key="environment"
      @click="addEnvironment(environment)"
    >
      {{ environment }}
    </gl-dropdown-item>
    <template v-else-if="environmentSearch.length">
      <span ref="noResults" class="text-secondary gl-p-3">
        {{ $options.translations.noMatchingResults }}
      </span>
      <gl-dropdown-divider />
      <gl-dropdown-item @click="addEnvironment(environmentSearch)">
        {{ createEnvironmentLabel }}
      </gl-dropdown-item>
    </template>
  </gl-dropdown>
</template>