Welcome to mirror list, hosted at ThFree Co, Russian Federation.

group_select.vue « components « invite_members « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1369deae3f96be453c3258c4d04c49fd48ff0836 (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
107
108
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<script>
import { GlAvatarLabeled, GlCollapsibleListbox } from '@gitlab/ui';
import { debounce } from 'lodash';
import { s__ } from '~/locale';
import { getGroups, getDescendentGroups } from '~/rest_api';
import { normalizeHeaders, parseIntPagination } from '~/lib/utils/common_utils';
import { SEARCH_DELAY, GROUP_FILTERS } from '../constants';

export default {
  name: 'GroupSelect',
  components: {
    GlAvatarLabeled,
    GlCollapsibleListbox,
  },
  model: {
    prop: 'selectedGroup',
  },
  props: {
    selectedGroup: {
      type: Object,
      required: true,
    },
    groupsFilter: {
      type: String,
      required: false,
      default: GROUP_FILTERS.ALL,
    },
    parentGroupId: {
      type: Number,
      required: false,
      default: null,
    },
    invalidGroups: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      isFetching: false,
      groups: [],
      searchTerm: '',
      pagination: {},
      infiniteScrollLoading: false,
    };
  },
  computed: {
    toggleText() {
      return this.selectedGroup.name || this.$options.i18n.dropdownText;
    },
    isFetchResultEmpty() {
      return this.groups.length === 0;
    },
    infiniteScroll() {
      return Boolean(this.pagination.nextPage);
    },
  },
  mounted() {
    this.retrieveGroups();
  },
  methods: {
    retrieveGroups: debounce(async function debouncedRetrieveGroups() {
      this.isFetching = true;

      try {
        const response = await this.fetchGroups();
        this.pagination = this.processPagination(response);
        this.groups = this.processGroups(response);
      } catch {
        this.onApiError();
      } finally {
        this.isFetching = false;
      }
    }, SEARCH_DELAY),
    processGroups({ data }) {
      const rawGroups = data.map((group) => ({
        // `value` is needed for `GlCollapsibleListbox`
        value: group.id,
        id: group.id,
        name: group.full_name,
        path: group.path,
        avatarUrl: group.avatar_url,
      }));

      return this.filterOutInvalidGroups(rawGroups);
    },
    processPagination({ headers }) {
      return parseIntPagination(normalizeHeaders(headers));
    },
    filterOutInvalidGroups(groups) {
      return groups.filter((group) => this.invalidGroups.indexOf(group.id) === -1);
    },
    onSelect(id) {
      this.$emit('input', this.groups.find((group) => group.value === id) || {});
    },
    onSearch(searchTerm) {
      this.searchTerm = searchTerm;
      this.retrieveGroups();
    },
    fetchGroups(options = {}) {
      const combinedOptions = {
        ...this.$options.defaultFetchOptions,
        ...options,
      };

      switch (this.groupsFilter) {
        case GROUP_FILTERS.DESCENDANT_GROUPS:
          return getDescendentGroups(this.parentGroupId, this.searchTerm, combinedOptions);
        default:
          return getGroups(this.searchTerm, combinedOptions);
      }
    },
    async onBottomReached() {
      this.infiniteScrollLoading = true;

      try {
        const response = await this.fetchGroups({ page: this.pagination.page + 1 });
        this.pagination = this.processPagination(response);
        this.groups.push(...this.processGroups(response));
      } catch {
        this.onApiError();
      } finally {
        this.infiniteScrollLoading = false;
      }
    },
    onApiError() {
      this.$emit('error', this.$options.i18n.errorMessage);
    },
  },
  i18n: {
    dropdownText: s__('GroupSelect|Select a group'),
    searchPlaceholder: s__('GroupSelect|Search groups'),
    emptySearchResult: s__('GroupSelect|No matching results'),
    errorMessage: s__(
      'GroupSelect|An error occurred fetching the groups. Please refresh the page to try again.',
    ),
  },
  defaultFetchOptions: {
    exclude_internal: true,
    active: true,
    order_by: 'similarity',
  },
};
</script>
<template>
  <div>
    <gl-collapsible-listbox
      data-testid="group-select-dropdown"
      :selected="selectedGroup.value"
      :items="groups"
      :toggle-text="toggleText"
      searchable
      :search-placeholder="$options.i18n.searchPlaceholder"
      block
      fluid-width
      is-check-centered
      :searching="isFetching"
      :no-results-text="$options.i18n.emptySearchResult"
      :infinite-scroll="infiniteScroll"
      :infinite-scroll-loading="infiniteScrollLoading"
      :total-items="pagination.total"
      @bottom-reached="onBottomReached"
      @select="onSelect"
      @search="onSearch"
    >
      <template #list-item="{ item }">
        <gl-avatar-labeled
          :label="item.name"
          :src="item.avatarUrl"
          :entity-id="item.value"
          :entity-name="item.name"
          :size="32"
        />
      </template>
    </gl-collapsible-listbox>
  </div>
</template>