All files / app/assets/javascripts/milestones/components milestone_combobox.vue

86.84% Statements 33/38
100% Branches 16/16
76.47% Functions 13/17
86.84% Lines 33/38

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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250  2x                   2x 2x 2x 2x   2x                                                                                   52x                                   62x 62x   62x 35x     27x 6x     21x 21x           33x         33x           33x                 114x     62x 2x                   52x 1x     52x 52x 52x 52x                                 1x 1x     1x     10x 10x                   683x                                                                                                                                                              
<script>
import {
  GlDropdown,
  GlDropdownDivider,
  GlDropdownSectionHeader,
  GlDropdownItem,
  GlLoadingIcon,
  GlSearchBoxByType,
  GlIcon,
} from '@gitlab/ui';
import { debounce, isEqual } from 'lodash';
import { mapActions, mapGetters, mapState } from 'vuex';
import { s__, __, sprintf } from '~/locale';
import createStore from '../stores';
import MilestoneResultsSection from './milestone_results_section.vue';
 
const SEARCH_DEBOUNCE_MS = 250;
 
export default {
  name: 'MilestoneCombobox',
  store: createStore(),
  components: {
    GlDropdown,
    GlDropdownDivider,
    GlDropdownSectionHeader,
    GlDropdownItem,
    GlLoadingIcon,
    GlSearchBoxByType,
    GlIcon,
    MilestoneResultsSection,
  },
  props: {
    value: {
      type: Array,
      required: false,
      default: () => [],
    },
    projectId: {
      type: String,
      required: true,
    },
    groupId: {
      type: String,
      required: false,
      default: '',
    },
    groupMilestonesAvailable: {
      type: Boolean,
      required: false,
      default: false,
    },
    extraLinks: {
      type: Array,
      default: () => [],
      required: false,
    },
  },
  data() {
    return {
      searchQuery: '',
    };
  },
  translations: {
    milestone: s__('MilestoneCombobox|Milestone'),
    selectMilestone: s__('MilestoneCombobox|Select milestone'),
    noMilestone: s__('MilestoneCombobox|No milestone'),
    noResultsLabel: s__('MilestoneCombobox|No matching results'),
    searchMilestones: s__('MilestoneCombobox|Search Milestones'),
    searchErrorMessage: s__('MilestoneCombobox|An error occurred while searching for milestones'),
    projectMilestones: s__('MilestoneCombobox|Project milestones'),
    groupMilestones: s__('MilestoneCombobox|Group milestones'),
  },
  computed: {
    ...mapState(['matches', 'selectedMilestones']),
    ...mapGetters(['isLoading', 'groupMilestonesEnabled']),
    selectedMilestonesLabel() {
      const { selectedMilestones } = this;
      const firstMilestoneName = selectedMilestones[0];
 
      if (selectedMilestones.length === 0) {
        return this.$options.translations.noMilestone;
      }
 
      if (selectedMilestones.length === 1) {
        return firstMilestoneName;
      }
 
      const numberOfOtherMilestones = selectedMilestones.length - 1;
      return sprintf(__('%{firstMilestoneName} + %{numberOfOtherMilestones} more'), {
        firstMilestoneName,
        numberOfOtherMilestones,
      });
    },
    showProjectMilestoneSection() {
      return Boolean(
        this.matches.projectMilestones.totalCount > 0 || this.matches.projectMilestones.error,
      );
    },
    showGroupMilestoneSection() {
      return (
        this.groupMilestonesEnabled &&
        Boolean(this.matches.groupMilestones.totalCount > 0 || this.matches.groupMilestones.error)
      );
    },
    showNoResults() {
      return !this.showProjectMilestoneSection && !this.showGroupMilestoneSection;
    },
  },
  watch: {
    // Keep the Vuex store synchronized if the parent
    // component updates the selected milestones through v-model
    value: {
      immediate: true,
      handler() {
        const milestoneTitles = this.value.map((milestone) =>
          milestone.title ? milestone.title : milestone,
        );
        if (!isEqual(milestoneTitles, this.selectedMilestones)) {
          this.setSelectedMilestones(milestoneTitles);
        }
      },
    },
  },
  created() {
    // This method is defined here instead of in `methods`
    // because we need to access the .cancel() method
    // lodash attaches to the function, which is
    // made inaccessible by Vue.
    this.debouncedSearch = debounce(function search() {
      this.search(this.searchQuery);
    }, SEARCH_DEBOUNCE_MS);
 
    this.setProjectId(this.projectId);
    this.setGroupId(this.groupId);
    this.setGroupMilestonesAvailable(this.groupMilestonesAvailable);
    this.fetchMilestones();
  },
  methods: {
    ...mapActions([
      'setProjectId',
      'setGroupId',
      'setGroupMilestonesAvailable',
      'setSelectedMilestones',
      'clearSelectedMilestones',
      'toggleMilestones',
      'search',
      'fetchMilestones',
    ]),
    focusSearchBox() {
      this.$refs.searchBox.$el.querySelector('input').focus();
    },
    onSearchBoxEnter() {
      this.debouncedSearch.cancel();
      this.search(this.searchQuery);
    },
    onSearchBoxInput() {
      this.debouncedSearch();
    },
    selectMilestone(milestone) {
      this.toggleMilestones(milestone);
      this.$emit('input', this.selectedMilestones);
    },
    selectNoMilestone() {
      this.clearSelectedMilestones();
      this.$emit('input', this.selectedMilestones);
    },
  },
};
</script>
 
<template>
  <gl-dropdown v-bind="$attrs" class="milestone-combobox" @shown="focusSearchBox">
    <template #button-content>
      <span data-testid="milestone-combobox-button-content" class="gl-flex-grow-1 text-muted">{{
        selectedMilestonesLabel
      }}</span>
      <gl-icon name="chevron-down" />
    </template>
 
    <gl-dropdown-section-header>
      <span class="text-center d-block">{{ $options.translations.selectMilestone }}</span>
    </gl-dropdown-section-header>
 
    <gl-dropdown-divider />
 
    <gl-search-box-by-type
      ref="searchBox"
      v-model.trim="searchQuery"
      class="gl-m-3"
      :placeholder="this.$options.translations.searchMilestones"
      @input="onSearchBoxInput"
      @keydown.enter.prevent="onSearchBoxEnter"
    />
 
    <gl-dropdown-item @click="selectNoMilestone()">
      <span :class="{ 'gl-pl-6': true, 'selected-item': selectedMilestones.length === 0 }">
        {{ $options.translations.noMilestone }}
      </span>
    </gl-dropdown-item>
 
    <gl-dropdown-divider />
 
    <template v-if="isLoading">
      <gl-loading-icon size="sm" />
      <gl-dropdown-divider />
    </template>
    <template v-else-if="showNoResults">
      <div class="dropdown-item-space">
        <span data-testid="milestone-combobox-no-results" class="gl-pl-6">{{
          $options.translations.noResultsLabel
        }}</span>
      </div>
      <gl-dropdown-divider />
    </template>
    <template v-else>
      <milestone-results-section
        v-if="showProjectMilestoneSection"
        :section-title="$options.translations.projectMilestones"
        :total-count="matches.projectMilestones.totalCount"
        :items="matches.projectMilestones.list"
        :selected-milestones="selectedMilestones"
        :error="matches.projectMilestones.error"
        :error-message="$options.translations.searchErrorMessage"
        data-testid="project-milestones-section"
        @selected="selectMilestone($event)"
      />
 
      <milestone-results-section
        v-if="showGroupMilestoneSection"
        :section-title="$options.translations.groupMilestones"
        :total-count="matches.groupMilestones.totalCount"
        :items="matches.groupMilestones.list"
        :selected-milestones="selectedMilestones"
        :error="matches.groupMilestones.error"
        :error-message="$options.translations.searchErrorMessage"
        data-testid="group-milestones-section"
        @selected="selectMilestone($event)"
      />
    </template>
    <gl-dropdown-item
      v-for="(item, idx) in extraLinks"
      :key="idx"
      :href="item.url"
      data-testid="milestone-combobox-extra-links"
    >
      <span class="gl-pl-6">{{ item.text }}</span>
    </gl-dropdown-item>
  </gl-dropdown>
</template>