summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/branches/components/sort_dropdown.vue
blob: ddb4c5c001589fc08d7b5d37b8bf990bdd6cde6f (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
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
<script>
import { GlDropdown, GlDropdownItem, GlSearchBoxByClick } from '@gitlab/ui';
import { mergeUrlParams, visitUrl, getParameterValues } from '~/lib/utils/url_utility';
import { s__ } from '~/locale';

const OVERVIEW_MODE = 'overview';

export default {
  i18n: {
    searchPlaceholder: s__('Branches|Filter by branch name'),
  },
  components: {
    GlDropdown,
    GlDropdownItem,
    GlSearchBoxByClick,
  },
  inject: ['projectBranchesFilteredPath', 'sortOptions', 'mode'],
  data() {
    return {
      selectedKey: 'updated_desc',
      searchTerm: '',
    };
  },
  computed: {
    shouldShowDropdown() {
      return this.mode !== OVERVIEW_MODE;
    },
    selectedSortMethodName() {
      return this.sortOptions[this.selectedKey];
    },
  },
  created() {
    const sortValue = getParameterValues('sort');
    const searchValue = getParameterValues('search');

    if (sortValue.length > 0) {
      [this.selectedKey] = sortValue;
    }

    if (searchValue.length > 0) {
      [this.searchTerm] = searchValue;
    }
  },
  methods: {
    isSortMethodSelected(sortKey) {
      return sortKey === this.selectedKey;
    },
    visitUrlFromOption(sortKey) {
      this.selectedKey = sortKey;
      const urlParams = {};

      if (this.mode !== OVERVIEW_MODE) {
        urlParams.sort = sortKey;
      }

      urlParams.search = this.searchTerm.length > 0 ? this.searchTerm : null;

      const newUrl = mergeUrlParams(urlParams, this.projectBranchesFilteredPath);
      visitUrl(newUrl);
    },
  },
};
</script>
<template>
  <div class="gl-display-flex gl-pr-4">
    <gl-search-box-by-click
      v-model="searchTerm"
      :placeholder="$options.i18n.searchPlaceholder"
      class="gl-pr-4"
      data-testid="branch-search"
      @submit="visitUrlFromOption(selectedKey)"
    />
    <gl-dropdown
      v-if="shouldShowDropdown"
      :text="selectedSortMethodName"
      data-testid="branches-dropdown"
    >
      <gl-dropdown-item
        v-for="(value, key) in sortOptions"
        :key="key"
        :is-checked="isSortMethodSelected(key)"
        is-check-item
        @click="visitUrlFromOption(key)"
        >{{ value }}</gl-dropdown-item
      >
    </gl-dropdown>
  </div>
</template>