All files / ee/app/assets/javascripts/analytics/merge_request_analytics/components filter_bar.vue

100% Statements 6/6
50% Branches 6/12
100% Functions 15/15
100% Lines 6/6

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  2x                                                                                                         19x                                                                                                                                       26x                   26x                                                     1x   1x                                                          
<script>
// eslint-disable-next-line no-restricted-imports
import { mapActions, mapState } from 'vuex';
import {
  OPERATORS_IS,
  OPTIONS_NONE_ANY,
  TOKEN_TITLE_ASSIGNEE,
  TOKEN_TITLE_AUTHOR,
  TOKEN_TITLE_LABEL,
  TOKEN_TITLE_MILESTONE,
  TOKEN_TITLE_SOURCE_BRANCH,
  TOKEN_TITLE_TARGET_BRANCH,
  TOKEN_TYPE_ASSIGNEE,
  TOKEN_TYPE_AUTHOR,
  TOKEN_TYPE_LABEL,
  TOKEN_TYPE_MILESTONE,
  TOKEN_TYPE_SOURCE_BRANCH,
  TOKEN_TYPE_TARGET_BRANCH,
} from '~/vue_shared/components/filtered_search_bar/constants';
import FilteredSearchBar from '~/vue_shared/components/filtered_search_bar/filtered_search_bar_root.vue';
import {
  prepareTokens,
  processFilters,
  filterToQueryObject,
} from '~/vue_shared/components/filtered_search_bar/filtered_search_utils';
import UserToken from '~/vue_shared/components/filtered_search_bar/tokens/user_token.vue';
import BranchToken from '~/vue_shared/components/filtered_search_bar/tokens/branch_token.vue';
import LabelToken from '~/vue_shared/components/filtered_search_bar/tokens/label_token.vue';
import MilestoneToken from '~/vue_shared/components/filtered_search_bar/tokens/milestone_token.vue';
import UrlSync from '~/vue_shared/components/url_sync.vue';
import { MAX_LABEL_SUGGESTIONS } from '../constants';
 
export default {
  name: 'FilterBar',
  components: {
    FilteredSearchBar,
    UrlSync,
  },
  inject: ['fullPath', 'type'],
  computed: {
    ...mapState('filters', {
      selectedSourceBranch: (state) => state.branches.source.selected,
      selectedTargetBranch: (state) => state.branches.target.selected,
      selectedMilestone: (state) => state.milestones.selected,
      selectedAuthor: (state) => state.authors.selected,
      selectedAssignee: (state) => state.assignees.selected,
      selectedLabelList: (state) => state.labels.selectedList,
      milestonesData: (state) => state.milestones.data,
      labelsData: (state) => state.labels.data,
      assigneesData: (state) => state.assignees.data,
      authorsData: (state) => state.authors.data,
      branchesData: (state) => state.branches.data,
    }),
    tokens() {
      return [
        {
          icon: 'branch',
          title: TOKEN_TITLE_SOURCE_BRANCH,
          type: TOKEN_TYPE_SOURCE_BRANCH,
          token: BranchToken,
          initialBranches: this.branchesData,
          unique: true,
          operators: OPERATORS_IS,
          fetchBranches: this.fetchBranches,
        },
        {
          icon: 'branch',
          title: TOKEN_TITLE_TARGET_BRANCH,
          type: TOKEN_TYPE_TARGET_BRANCH,
          token: BranchToken,
          initialBranches: this.branchesData,
          unique: true,
          operators: OPERATORS_IS,
          fetchBranches: this.fetchBranches,
        },
        {
          icon: 'milestone',
          title: TOKEN_TITLE_MILESTONE,
          type: TOKEN_TYPE_MILESTONE,
          token: MilestoneToken,
          initialMilestones: this.milestonesData,
          unique: true,
          symbol: '%',
          fetchMilestones: this.fetchMilestones,
        },
        {
          icon: 'labels',
          title: TOKEN_TITLE_LABEL,
          type: TOKEN_TYPE_LABEL,
          token: LabelToken,
          defaultLabels: OPTIONS_NONE_ANY,
          initialLabels: this.labelsData,
          unique: false,
          symbol: '~',
          fetchLabels: this.fetchLabels,
          maxSuggestions: MAX_LABEL_SUGGESTIONS,
        },
        {
          icon: 'pencil',
          title: TOKEN_TITLE_AUTHOR,
          type: TOKEN_TYPE_AUTHOR,
          token: UserToken,
          dataType: 'user',
          initialUsers: this.authorsData,
          unique: true,
          operators: OPERATORS_IS,
          fetchUsers: this.fetchAuthors,
        },
        {
          icon: 'user',
          title: TOKEN_TITLE_ASSIGNEE,
          type: TOKEN_TYPE_ASSIGNEE,
          token: UserToken,
          dataType: 'user',
          initialUsers: this.assigneesData,
          unique: false,
          operators: OPERATORS_IS,
          fetchUsers: this.fetchAssignees,
        },
      ];
    },
    query() {
      return filterToQueryObject({
        source_branch_name: this.selectedSourceBranch,
        target_branch_name: this.selectedTargetBranch,
        milestone_title: this.selectedMilestone,
        label_name: this.selectedLabelList,
        author_username: this.selectedAuthor,
        assignee_username: this.selectedAssignee,
      });
    },
    initialFilterValue() {
      return prepareTokens({
        [TOKEN_TYPE_SOURCE_BRANCH]: this.selectedSourceBranch,
        [TOKEN_TYPE_TARGET_BRANCH]: this.selectedTargetBranch,
        [TOKEN_TYPE_MILESTONE]: this.selectedMilestone,
        [TOKEN_TYPE_AUTHOR]: this.selectedAuthor,
        [TOKEN_TYPE_ASSIGNEE]: this.selectedAssignee,
        [TOKEN_TYPE_LABEL]: this.selectedLabelList,
      });
    },
  },
  methods: {
    ...mapActions('filters', [
      'setFilters',
      'fetchBranches',
      'fetchMilestones',
      'fetchAuthors',
      'fetchAssignees',
      'fetchLabels',
    ]),
    handleFilter(filters) {
      const {
        [TOKEN_TYPE_SOURCE_BRANCH]: sourceBranch,
        [TOKEN_TYPE_TARGET_BRANCH]: targetBranch,
        [TOKEN_TYPE_MILESTONE]: milestone,
        [TOKEN_TYPE_AUTHOR]: author,
        [TOKEN_TYPE_ASSIGNEE]: assignee,
        [TOKEN_TYPE_LABEL]: labels,
      } = processFilters(filters);
 
      this.setFilters({
        selectedSourceBranch: sourceBranch ? sourceBranch[0] : null,
        selectedTargetBranch: targetBranch ? targetBranch[0] : null,
        selectedAuthor: author ? author[0] : null,
        selectedMilestone: milestone ? milestone[0] : null,
        selectedAssignee: assignee ? assignee[0] : null,
        selectedLabelList: labels || [],
      });
    },
  },
};
</script>
 
<template>
  <div>
    <filtered-search-bar
      class="gl-flex-grow-1"
      :namespace="fullPath"
      recent-searches-storage-key="merge-request-analytics"
      :search-input-placeholder="__('Filter results')"
      :tokens="tokens"
      :initial-filter-value="initialFilterValue"
      suggestions-list-class="gl-z-index-9999"
      terms-as-tokens
      @onFilter="handleFilter"
    />
    <url-sync :query="query" />
  </div>
</template>