summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/feature_flags/components/new_environments_dropdown.vue
blob: 2888746005ef854f72b53f8e17ee0562c821e30b (plain)
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
104
105
106
<script>
import { debounce } from 'lodash';
import {
  GlDropdown,
  GlDropdownDivider,
  GlDropdownItem,
  GlIcon,
  GlLoadingIcon,
  GlSearchBoxByType,
} from '@gitlab/ui';
import axios from '~/lib/utils/axios_utils';
import { __, sprintf } from '~/locale';
import { deprecatedCreateFlash as createFlash } from '~/flash';

export default {
  components: {
    GlDropdown,
    GlDropdownDivider,
    GlDropdownItem,
    GlSearchBoxByType,
    GlIcon,
    GlLoadingIcon,
  },
  props: {
    endpoint: {
      type: String,
      required: true,
    },
  },
  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.endpoint, { params: { query: this.environmentSearch } })
        .then(({ data }) => {
          this.results = data || [];
        })
        .catch(() => {
          createFlash(__('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"
      class="gl-m-3"
      @focus="fetchEnvironments"
      @keyup="fetchEnvironments"
    />
    <gl-loading-icon v-if="isLoading" />
    <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>