All files / app/assets/javascripts/search/topbar/components project_filter.vue

92.31% Statements 12/13
100% Branches 14/14
80% Functions 4/5
92.31% Lines 12/13

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  3x 3x 3x 3x                                   13x         13x 1x             4x 2x       4x           4x             48x                              
<script>
import { mapState, mapActions, mapGetters } from 'vuex';
import { visitUrl, setUrlParams } from '~/lib/utils/url_utility';
import { ANY_OPTION, GROUP_DATA, PROJECT_DATA } from '../constants';
import SearchableDropdown from './searchable_dropdown.vue';
 
export default {
  name: 'ProjectFilter',
  components: {
    SearchableDropdown,
  },
  props: {
    initialData: {
      type: Object,
      required: false,
      default: () => null,
    },
  },
  computed: {
    ...mapState(['query', 'projects', 'fetchingProjects']),
    ...mapGetters(['frequentProjects']),
    selectedProject() {
      return this.initialData ? this.initialData : ANY_OPTION;
    },
  },
  created() {
    // This tracks projects searched via the top nav search bar
    if (this.query.nav_source === 'navbar' && this.initialData?.id) {
      this.setFrequentProject(this.initialData);
    }
  },
  methods: {
    ...mapActions(['fetchProjects', 'setFrequentProject', 'loadFrequentProjects']),
    handleProjectChange(project) {
      // If project.id is null we are clearing the filter and don't need to store that in LS.
      if (project.id) {
        this.setFrequentProject(project);
      }
 
      // This determines if we need to update the group filter or not
      const queryParams = {
        ...(project.namespace?.id && { [GROUP_DATA.queryParam]: project.namespace.id }),
        [PROJECT_DATA.queryParam]: project.id,
        nav_source: null,
      };
 
      visitUrl(setUrlParams(queryParams));
    },
  },
  PROJECT_DATA,
};
</script>
 
<template>
  <searchable-dropdown
    data-testid="project-filter"
    :header-text="$options.PROJECT_DATA.headerText"
    :name="$options.PROJECT_DATA.name"
    :full-name="$options.PROJECT_DATA.fullName"
    :loading="fetchingProjects"
    :selected-item="selectedProject"
    :items="projects"
    :frequent-items="frequentProjects"
    @first-open="loadFrequentProjects"
    @search="fetchProjects"
    @change="handleProjectChange"
  />
</template>