summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/runner/components/runner_filtered_search_bar.vue
blob: a9dfec35479766b5ea1cc5d790e2690545304dad (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
89
90
91
92
93
94
95
96
97
98
99
<script>
import { cloneDeep } from 'lodash';
import { __ } from '~/locale';
import FilteredSearch from '~/vue_shared/components/filtered_search_bar/filtered_search_bar_root.vue';
import { searchValidator } from '~/runner/runner_search_utils';
import { CREATED_DESC, CREATED_ASC, CONTACTED_DESC, CONTACTED_ASC } from '../constants';

const sortOptions = [
  {
    id: 1,
    title: __('Created date'),
    sortDirection: {
      descending: CREATED_DESC,
      ascending: CREATED_ASC,
    },
  },
  {
    id: 2,
    title: __('Last contact'),
    sortDirection: {
      descending: CONTACTED_DESC,
      ascending: CONTACTED_ASC,
    },
  },
];

export default {
  components: {
    FilteredSearch,
  },
  props: {
    value: {
      type: Object,
      required: true,
      validator: searchValidator,
    },
    tokens: {
      type: Array,
      required: false,
      default: () => [],
    },
    namespace: {
      type: String,
      required: true,
    },
  },
  data() {
    // filtered_search_bar_root.vue may mutate the inital
    // filters. Use `cloneDeep` to prevent those mutations
    // from affecting this component
    const { filters, sort } = cloneDeep(this.value);
    return {
      initialFilterValue: filters,
      initialSortBy: sort,
    };
  },
  methods: {
    onFilter(filters) {
      // Apply new filters, from page 1
      this.$emit('input', {
        ...this.value,
        filters,
        pagination: { page: 1 },
      });
    },
    onSort(sort) {
      // Apply new sort, from page 1
      this.$emit('input', {
        ...this.value,
        sort,
        pagination: { page: 1 },
      });
    },
  },
  sortOptions,
};
</script>
<template>
  <div
    class="gl-bg-gray-10 gl-p-5 gl-border-solid gl-border-gray-100 gl-border-0 gl-border-t-1 gl-border-b-1"
  >
    <filtered-search
      v-bind="$attrs"
      :namespace="namespace"
      recent-searches-storage-key="runners-search"
      :sort-options="$options.sortOptions"
      :initial-filter-value="initialFilterValue"
      :tokens="tokens"
      :initial-sort-by="initialSortBy"
      :search-input-placeholder="__('Search or filter results...')"
      data-testid="runners-filtered-search"
      @onFilter="onFilter"
      @onSort="onSort"
    />
    <div class="gl-text-right" data-testid="runner-count">
      <slot name="runner-count"></slot>
    </div>
  </div>
</template>