All files / app/assets/javascripts/boards/components board_filtered_search.vue

77.68% Statements 87/112
67.5% Branches 54/80
92.86% Functions 13/14
77.68% Lines 87/112

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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374    5x 5x 5x 5x 5x 5x 5x                                   8x       9x           14x                             14x   14x 2x           14x 1x           14x 2x           14x 1x           14x 3x 4x             14x 1x           14x 1x           14x 1x           14x             14x 1x           14x             14x             14x             14x             14x             14x             14x             14x                 14x             14x             14x             14x             14x       14x     5x                             5x   5x 5x                                 5x                                     9x 1x           5x   5x           5x     18x 5x 13x     5x     10x 10x 10x   10x 11x   1x 1x   3x 2x   1x   3x   1x 1x   2x 2x   1x 1x   1x 1x   1x 1x               1x 1x                       10x 1x     10x       10x           57x                    
<script>
import { pickBy, isEmpty } from 'lodash';
import { mapActions } from 'vuex';
import { getIdFromGraphQLId, isGid } from '~/graphql_shared/utils';
import { updateHistory, setUrlParams } from '~/lib/utils/url_utility';
import { __ } from '~/locale';
import { FILTERED_SEARCH_TERM } from '~/vue_shared/components/filtered_search_bar/constants';
import FilteredSearch from '~/vue_shared/components/filtered_search_bar/filtered_search_bar_root.vue';
import { AssigneeFilterType } from '~/boards/constants';
 
export default {
  i18n: {
    search: __('Search'),
    label: __('Label'),
    author: __('Author'),
  },
  components: { FilteredSearch },
  inject: ['initialFilterParams'],
  props: {
    tokens: {
      type: Array,
      required: true,
    },
    eeFilters: {
      required: false,
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      filterParams: this.initialFilterParams,
    };
  },
  computed: {
    getFilteredSearchValue() {
      const {
        authorUsername,
        labelName,
        assigneeUsername,
        assigneeId,
        search,
        milestoneTitle,
        iterationId,
        types,
        weight,
        epicId,
        myReactionEmoji,
        releaseTag,
        confidential,
      } = this.filterParams;
      const filteredSearchValue = [];
 
      if (authorUsername) {
        filteredSearchValue.push({
          type: 'author',
          value: { data: authorUsername, operator: '=' },
        });
      }
 
      if (assigneeUsername) {
        filteredSearchValue.push({
          type: 'assignee',
          value: { data: assigneeUsername, operator: '=' },
        });
      }
 
      if (assigneeId) {
        filteredSearchValue.push({
          type: 'assignee',
          value: { data: assigneeId, operator: '=' },
        });
      }
 
      if (types) {
        filteredSearchValue.push({
          type: 'type',
          value: { data: types, operator: '=' },
        });
      }
 
      if (labelName?.length) {
        filteredSearchValue.push(
          ...labelName.map((label) => ({
            type: 'label',
            value: { data: label, operator: '=' },
          })),
        );
      }
 
      if (milestoneTitle) {
        filteredSearchValue.push({
          type: 'milestone',
          value: { data: milestoneTitle, operator: '=' },
        });
      }
 
      if (iterationId) {
        filteredSearchValue.push({
          type: 'iteration',
          value: { data: iterationId, operator: '=' },
        });
      }
 
      if (weight) {
        filteredSearchValue.push({
          type: 'weight',
          value: { data: weight, operator: '=' },
        });
      }
 
I      if (myReactionEmoji) {
        filteredSearchValue.push({
          type: 'my-reaction',
          value: { data: myReactionEmoji, operator: '=' },
        });
      }
 
      if (releaseTag) {
        filteredSearchValue.push({
          type: 'release',
          value: { data: releaseTag, operator: '=' },
        });
      }
 
I      if (confidential !== undefined) {
        filteredSearchValue.push({
          type: 'confidential',
          value: { data: confidential },
        });
      }
 
I      if (epicId) {
        filteredSearchValue.push({
          type: 'epic',
          value: { data: epicId, operator: '=' },
        });
      }
 
I      if (this.filterParams['not[authorUsername]']) {
        filteredSearchValue.push({
          type: 'author',
          value: { data: this.filterParams['not[authorUsername]'], operator: '!=' },
        });
      }
 
I      if (this.filterParams['not[milestoneTitle]']) {
        filteredSearchValue.push({
          type: 'milestone',
          value: { data: this.filterParams['not[milestoneTitle]'], operator: '!=' },
        });
      }
 
I      if (this.filterParams['not[iteration_id]']) {
        filteredSearchValue.push({
          type: 'iteration_id',
          value: { data: this.filterParams['not[iteration_id]'], operator: '!=' },
        });
      }
 
I      if (this.filterParams['not[weight]']) {
        filteredSearchValue.push({
          type: 'weight',
          value: { data: this.filterParams['not[weight]'], operator: '!=' },
        });
      }
 
I      if (this.filterParams['not[assigneeUsername]']) {
        filteredSearchValue.push({
          type: 'assignee',
          value: { data: this.filterParams['not[assigneeUsername]'], operator: '!=' },
        });
      }
 
I      if (this.filterParams['not[labelName]']) {
        filteredSearchValue.push(
          ...this.filterParams['not[labelName]'].map((label) => ({
            type: 'label',
            value: { data: label, operator: '!=' },
          })),
        );
      }
 
I      if (this.filterParams['not[types]']) {
        filteredSearchValue.push({
          type: 'type',
          value: { data: this.filterParams['not[types]'], operator: '!=' },
        });
      }
 
I      if (this.filterParams['not[epicId]']) {
        filteredSearchValue.push({
          type: 'epic',
          value: { data: this.filterParams['not[epicId]'], operator: '!=' },
        });
      }
 
I      if (this.filterParams['not[myReactionEmoji]']) {
        filteredSearchValue.push({
          type: 'my-reaction',
          value: { data: this.filterParams['not[myReactionEmoji]'], operator: '!=' },
        });
      }
 
I      if (this.filterParams['not[releaseTag]']) {
        filteredSearchValue.push({
          type: 'release',
          value: { data: this.filterParams['not[releaseTag]'], operator: '!=' },
        });
      }
 
I      if (search) {
        filteredSearchValue.push(search);
      }
 
      return filteredSearchValue;
    },
    urlParams() {
      const {
        authorUsername,
        labelName,
        assigneeUsername,
        assigneeId,
        search,
        milestoneTitle,
        types,
        weight,
        epicId,
        myReactionEmoji,
        iterationId,
        releaseTag,
        confidential,
      } = this.filterParams;
      let notParams = {};
 
E      if (Object.prototype.hasOwnProperty.call(this.filterParams, 'not')) {
        notParams = pickBy(
          {
            'not[label_name][]': this.filterParams.not.labelName,
            'not[author_username]': this.filterParams.not.authorUsername,
            'not[assignee_username]': this.filterParams.not.assigneeUsername,
            'not[types]': this.filterParams.not.types,
            'not[milestone_title]': this.filterParams.not.milestoneTitle,
            'not[weight]': this.filterParams.not.weight,
            'not[epic_id]': this.filterParams.not.epicId,
            'not[my_reaction_emoji]': this.filterParams.not.myReactionEmoji,
            'not[iteration_id]': this.filterParams.not.iterationId,
            'not[release_tag]': this.filterParams.not.releaseTag,
          },
          undefined,
        );
      }
 
      return {
        ...notParams,
        author_username: authorUsername,
        'label_name[]': labelName,
        assignee_username: assigneeUsername,
        assignee_id: assigneeId,
        milestone_title: milestoneTitle,
        iteration_id: iterationId,
        search,
        types,
        weight,
        epic_id: isGid(epicId) ? getIdFromGraphQLId(epicId) : epicId,
        my_reaction_emoji: myReactionEmoji,
        release_tag: releaseTag,
        confidential,
      };
    },
  },
  created() {
    if (!isEmpty(this.eeFilters)) {
      this.filterParams = this.eeFilters;
    }
  },
  methods: {
    ...mapActions(['performSearch']),
    handleFilter(filters) {
      this.filterParams = this.getFilterParams(filters);
 
      updateHistory({
        url: setUrlParams(this.urlParams, window.location.href, true, false, true),
        title: document.title,
        replace: true,
      });
 
      this.performSearch();
    },
    getFilterParams(filters = []) {
      const notFilters = filters.filter((item) => item.value.operator === '!=');
      const equalsFilters = filters.filter(
        (item) => item?.value?.operator === '=' || item.type === FILTERED_SEARCH_TERM,
      );
 
      return { ...this.generateParams(equalsFilters), not: { ...this.generateParams(notFilters) } };
    },
    generateParams(filters = []) {
      const filterParams = {};
      const labels = [];
      const plainText = [];
 
      filters.forEach((filter) => {
        switch (filter.type) {
          case 'author':
            filterParams.authorUsername = filter.value.data;
            break;
          case 'assignee':
            if (Object.values(AssigneeFilterType).includes(filter.value.data)) {
              filterParams.assigneeId = filter.value.data;
            } else {
              filterParams.assigneeUsername = filter.value.data;
            }
            break;
          case 'type':
            filterParams.types = filter.value.data;
            break;
          case 'label':
            labels.push(filter.value.data);
            break;
          case 'milestone':
            filterParams.milestoneTitle = filter.value.data;
            break;
          case 'iteration':
            filterParams.iterationId = filter.value.data;
            break;
          case 'weight':
            filterParams.weight = filter.value.data;
            break;
          case 'epic':
            filterParams.epicId = filter.value.data;
            break;
          case 'my-reaction':
            filterParams.myReactionEmoji = filter.value.data;
            break;
          case 'release':
            filterParams.releaseTag = filter.value.data;
            break;
          case 'confidential':
            filterParams.confidential = filter.value.data;
            break;
          case 'filtered-search-term':
            if (filter.value.data) plainText.push(filter.value.data);
            break;
          default:
            break;
        }
      });
 
      if (labels.length) {
        filterParams.labelName = labels;
      }
 
I      if (plainText.length) {
        filterParams.search = plainText.join(' ');
      }
 
      return filterParams;
    },
  },
};
</script>
 
<template>
  <filtered-search
    class="gl-w-full"
    namespace=""
    :tokens="tokens"
    :search-input-placeholder="$options.i18n.search"
    :initial-filter-value="getFilteredSearchValue"
    @onFilter="handleFilter"
  />
</template>