All files / ee/app/assets/javascripts/boards/components group_select.vue

72% Statements 18/25
80% Branches 8/10
85% Functions 17/20
72% Lines 18/25

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 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 178 179 180 181 182            3x                                                                     9x                     9x           9x 9x                       9x     9x     18x 18x                 18x 18x     18x         18x     18x     18x         9x       9x             9x                                                                                                                              
<script>
import { GlCollapsibleListbox } from '@gitlab/ui';
import { debounce } from 'lodash';
import { DEFAULT_DEBOUNCE_AND_THROTTLE_MS } from '~/lib/utils/constants';
import { s__ } from '~/locale';
import { setError } from '~/boards/graphql/cache_updates';
import subgroupsQuery from '../graphql/sub_groups.query.graphql';
 
export default {
  name: 'GroupSelect',
  i18n: {
    headerTitle: s__(`BoardNewEpic|Groups`),
    searchPlaceholder: s__(`BoardNewEpic|Search groups`),
    emptySearchResult: s__(`BoardNewEpic|No matching results`),
    errorFetchingGroups: s__('Boards|An error occurred while fetching groups. Please try again.'),
  },
  defaultFetchOptions: {
    with_issues_enabled: true,
    with_shared: false,
    include_subgroups: true,
    order_by: 'similarity',
  },
  components: {
    GlCollapsibleListbox,
  },
  inject: ['groupId', 'fullPath'],
  model: {
    prop: 'selectedGroup',
    event: 'selectGroup',
  },
  props: {
    list: {
      type: Object,
      required: true,
    },
    selectedGroup: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      initialLoading: true,
      isLoadingMore: false,
      searchTerm: '',
      group: {},
    };
  },
  apollo: {
    group: {
      query: subgroupsQuery,
      variables() {
        return {
          search: this.searchTerm,
          fullPath: this.fullPath,
        };
      },
      result({ data }) {
        this.initialLoading = false;
        this.selectGroup(data.group.id);
      },
      error(error) {
        setError({
          error,
          message: this.$options.i18n.errorFetchingGroups,
        });
      },
    },
  },
  computed: {
    selectedGroupName() {
      return this.selectedGroup.name || s__('BoardNewEpic|Loading groups');
    },
    selectedGroupId() {
      return this.selectedGroup?.id || '';
    },
    currentGroup() {
      const { id, name, fullName, __typename } = this.group;
      return {
        __typename,
        id,
        name,
        fullName,
        fullPath: this.group.fullPath,
      };
    },
    subGroups() {
      const subgroups = this.group.descendantGroups?.nodes || [];
      return [this.currentGroup, ...subgroups];
    },
    subGroupsListBox() {
      return this.subGroups
        .filter(({ name }) => Boolean(name))
        .map(({ id, name, ...group }) => ({ value: id, text: name, ...group }));
    },
    isLoading() {
      return this.$apollo.queries.group.loading && !this.isLoadingMore;
    },
    pageInfo() {
      return this.group.descendantGroups?.pageInfo;
    },
    hasNextPage() {
      return this.pageInfo?.hasNextPage;
    },
  },
  watch: {
    hasNextPage() {
      return this.pageInfo?.hasNextPage;
    },
  },
  created() {
    this.debouncedSearch = debounce(this.setSearchTerm, DEFAULT_DEBOUNCE_AND_THROTTLE_MS);
  },
  methods: {
    setSearchTerm(term = '') {
      this.searchTerm = term.trim();
    },
    selectGroup(groupId) {
      this.$emit(
        'selectGroup',
        this.subGroups.find((group) => group.id === groupId),
      );
    },
    async loadMoreGroups() {
      this.isLoadingMore = true;
 
      try {
        await this.$apollo.queries.group.fetchMore({
          variables: {
            search: this.searchTerm,
            fullPath: this.fullPath,
            after: this.pageInfo?.endCursor,
          },
        });
      } catch (error) {
        setError({
          error,
          message: this.$options.i18n.errorFetchingGroups,
        });
      } finally {
        this.isLoadingMore = false;
      }
    },
  },
};
</script>
 
<template>
  <div>
    <label
      for="descendant-group-select"
      class="gl-font-weight-bold gl-mt-3"
      data-testid="header-label"
      >{{ $options.i18n.headerTitle }}</label
    >
    <gl-collapsible-listbox
      id="descendant-group-select"
      block
      fluid-width
      data-testid="project-select-dropdown"
      infinite-scroll
      searchable
      :infinite-scroll-loading="isLoading"
      :no-results-text="$options.i18n.emptySearchResult"
      :selected="selectedGroupId"
      :searching="isLoading"
      :search-placeholder="$options.i18n.searchPlaceholder"
      :header-text="$options.i18n.headerTitle"
      :items="subGroupsListBox"
      :loading="initialLoading"
      :toggle-text="selectedGroupName"
      @bottom-reached="loadMoreGroups"
      @search="debouncedSearch"
      @select="selectGroup"
    >
      <template #list-item="{ item }">
        {{ item.fullName || item.name }}
      </template>
    </gl-collapsible-listbox>
  </div>
</template>