summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/invite_members/components/group_select.vue
blob: 4a72e97db8ce322bb5b35cff2914868d1f062a5c (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
<script>
import { GlDropdown, GlDropdownItem, GlDropdownText, GlSearchBoxByType } from '@gitlab/ui';
import { debounce } from 'lodash';
import Api from '~/api';
import { s__ } from '~/locale';
import { SEARCH_DELAY } from '../constants';

export default {
  name: 'GroupSelect',
  components: {
    GlDropdown,
    GlDropdownItem,
    GlDropdownText,
    GlSearchBoxByType,
  },
  model: {
    prop: 'selectedGroup',
  },
  data() {
    return {
      isFetching: false,
      groups: [],
      selectedGroup: {},
      searchTerm: '',
    };
  },
  computed: {
    selectedGroupName() {
      return this.selectedGroup.name || this.$options.i18n.dropdownText;
    },
    isFetchResultEmpty() {
      return this.groups.length === 0;
    },
  },
  watch: {
    searchTerm() {
      this.retrieveGroups();
    },
  },
  mounted() {
    this.retrieveGroups();
  },
  methods: {
    retrieveGroups: debounce(function debouncedRetrieveGroups() {
      this.isFetching = true;
      return Api.groups(this.searchTerm, this.$options.defaultFetchOptions)
        .then((response) => {
          this.groups = response.map((group) => ({
            id: group.id,
            name: group.full_name,
            path: group.path,
          }));
          this.isFetching = false;
        })
        .catch(() => {
          this.isFetching = false;
        });
    }, SEARCH_DELAY),
    selectGroup(group) {
      this.selectedGroup = group;

      this.$emit('input', this.selectedGroup);
    },
  },
  i18n: {
    dropdownText: s__('GroupSelect|Select a group'),
    searchPlaceholder: s__('GroupSelect|Search groups'),
    emptySearchResult: s__('GroupSelect|No matching results'),
  },
  defaultFetchOptions: {
    exclude_internal: true,
    active: true,
  },
};
</script>
<template>
  <div>
    <gl-dropdown
      data-testid="group-select-dropdown"
      :text="selectedGroupName"
      block
      menu-class="gl-w-full!"
    >
      <gl-search-box-by-type
        v-model.trim="searchTerm"
        :is-loading="isFetching"
        :placeholder="$options.i18n.searchPlaceholder"
        data-qa-selector="group_select_dropdown_search_field"
      />
      <gl-dropdown-item
        v-for="group in groups"
        :key="group.id"
        :name="group.name"
        @click="selectGroup(group)"
      >
        {{ group.name }}
      </gl-dropdown-item>
      <gl-dropdown-text v-if="isFetchResultEmpty && !isFetching" data-testid="empty-result-message">
        <span class="gl-text-gray-500">{{ $options.i18n.emptySearchResult }}</span>
      </gl-dropdown-text>
    </gl-dropdown>
  </div>
</template>