summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/projects/settings_service_desk/components/service_desk_template_dropdown.vue
blob: 315f0743b5362fac76dde432345e37966dbb4f3c (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<script>
import fuzzaldrinPlus from 'fuzzaldrin-plus';
import { GlDropdown, GlDropdownSectionHeader, GlDropdownItem, GlSearchBoxByType } from '@gitlab/ui';
import { __ } from '~/locale';

export default {
  components: {
    GlDropdown,
    GlDropdownSectionHeader,
    GlDropdownItem,
    GlSearchBoxByType,
  },
  props: {
    selectedTemplate: {
      type: String,
      required: false,
      default: '',
    },
    templates: {
      type: Array,
      required: true,
    },
    selectedFileTemplateProjectId: {
      type: Number,
      required: false,
      default: null,
    },
  },
  data() {
    return {
      searchTerm: '',
    };
  },
  computed: {
    templateOptions() {
      if (this.searchTerm) {
        const filteredTemplates = [];
        for (let i = 0; i < this.templates.length; i += 2) {
          const sectionName = this.templates[i];
          const availableTemplates = this.templates[i + 1];

          const matchedTemplates = fuzzaldrinPlus.filter(availableTemplates, this.searchTerm, {
            key: 'name',
          });

          if (matchedTemplates.length > 0) {
            filteredTemplates.push(sectionName, matchedTemplates);
          }
        }

        return filteredTemplates;
      }

      return this.templates;
    },
  },
  methods: {
    templateClick(template) {
      // Clicking on the same template should unselect it
      if (
        template.name === this.selectedTemplate &&
        template.project_id === this.selectedFileTemplateProjectId
      ) {
        this.$emit('change', {
          selectedFileTemplateProjectId: null,
          selectedTemplate: null,
        });
        return;
      }

      this.$emit('change', {
        selectedFileTemplateProjectId: template.project_id,
        selectedTemplate: template.key,
      });
    },
  },
  i18n: {
    defaultDropdownText: __('Choose a template'),
  },
};
</script>
<template>
  <gl-dropdown
    id="service-desk-template-select"
    :text="selectedTemplate || $options.i18n.defaultDropdownText"
    :header-text="$options.i18n.defaultDropdownText"
    data-qa-selector="service_desk_template_dropdown"
    :block="true"
    class="service-desk-template-select"
    toggle-class="gl-m-0"
  >
    <template #header>
      <gl-search-box-by-type v-model.trim="searchTerm" />
    </template>
    <template v-for="item in templateOptions">
      <gl-dropdown-section-header v-if="!Array.isArray(item)" :key="item">
        {{ item }}
      </gl-dropdown-section-header>
      <template v-else>
        <gl-dropdown-item
          v-for="template in item"
          :key="template.key"
          is-check-item
          :is-checked="
            template.project_id === selectedFileTemplateProjectId &&
            template.name === selectedTemplate
          "
          @click="() => templateClick(template)"
        >
          {{ template.name }}
        </gl-dropdown-item>
      </template>
    </template>
  </gl-dropdown>
</template>