summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/filtered_search_bar/tokens/branch_token.vue
blob: 4ecfc1cf40c0ca1886fc4afeaccb06733dba8cd0 (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
<script>
import { GlFilteredSearchSuggestion } from '@gitlab/ui';
import createFlash from '~/flash';
import { __ } from '~/locale';
import BaseToken from '~/vue_shared/components/filtered_search_bar/tokens/base_token.vue';

export default {
  components: {
    BaseToken,
    GlFilteredSearchSuggestion,
  },
  props: {
    active: {
      type: Boolean,
      required: true,
    },
    config: {
      type: Object,
      required: true,
    },
    value: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      branches: this.config.initialBranches || [],
      loading: false,
    };
  },
  computed: {
    defaultBranches() {
      return this.config.defaultBranches || [];
    },
  },
  methods: {
    getActiveBranch(branches, data) {
      return branches.find((branch) => branch.name.toLowerCase() === data.toLowerCase());
    },
    fetchBranches(searchTerm) {
      this.loading = true;
      this.config
        .fetchBranches(searchTerm)
        .then(({ data }) => {
          this.branches = data;
        })
        .catch(() => {
          createFlash({ message: __('There was a problem fetching branches.') });
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>

<template>
  <base-token
    :active="active"
    :config="config"
    :value="value"
    :default-suggestions="defaultBranches"
    :suggestions="branches"
    :suggestions-loading="loading"
    :get-active-token-value="getActiveBranch"
    @fetch-suggestions="fetchBranches"
    v-on="$listeners"
  >
    <template #view="{ viewTokenProps: { inputValue, activeTokenValue } }">
      {{ activeTokenValue ? activeTokenValue.name : inputValue }}
    </template>
    <template #suggestions-list="{ suggestions }">
      <gl-filtered-search-suggestion
        v-for="branch in suggestions"
        :key="branch.id"
        :value="branch.name"
      >
        <div class="gl-display-flex">
          <span class="gl-display-inline-block gl-mr-3 gl-p-3"></span>
          {{ branch.name }}
        </div>
      </gl-filtered-search-suggestion>
    </template>
  </base-token>
</template>