summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/search/topbar/components/project_filter.vue
blob: b2dd79fcfa38e30276f1c3a72d1558f83b74d408 (plain)
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
<script>
import { mapState, mapActions } 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(['projects', 'fetchingProjects']),
    selectedProject() {
      return this.initialData ? this.initialData : ANY_OPTION;
    },
  },
  methods: {
    ...mapActions(['fetchProjects']),
    handleProjectChange(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,
      };

      visitUrl(setUrlParams(queryParams));
    },
  },
  PROJECT_DATA,
};
</script>

<template>
  <searchable-dropdown
    data-testid="project-filter"
    :header-text="$options.PROJECT_DATA.headerText"
    :selected-display-value="$options.PROJECT_DATA.selectedDisplayValue"
    :items-display-value="$options.PROJECT_DATA.itemsDisplayValue"
    :loading="fetchingProjects"
    :selected-item="selectedProject"
    :items="projects"
    @search="fetchProjects"
    @change="handleProjectChange"
  />
</template>