summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/merge_requests/components/target_project_dropdown.vue
blob: cd2e25793f4d2fa6f860165dd479603193f07d4c (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 { GlListbox } from '@gitlab/ui';
import { debounce } from 'lodash';
import { createAlert } from '~/flash';
import { __ } from '~/locale';
import axios from '~/lib/utils/axios_utils';

export default {
  components: {
    GlListbox,
  },
  inject: {
    targetProjectsPath: {
      type: String,
      required: true,
    },
    currentProject: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      currentProject: this.currentProject,
      selected: this.currentProject.value,
      isLoading: false,
      projects: [],
    };
  },
  methods: {
    async fetchProjects(search = '') {
      this.isLoading = true;

      try {
        const { data } = await axios.get(this.targetProjectsPath, {
          params: { search },
        });

        this.projects = data.map((p) => ({
          value: `${p.id}`,
          text: p.full_path.replace(/^\//, ''),
          refsUrl: p.refs_url,
        }));
        this.isLoading = false;
      } catch {
        createAlert({
          message: __('Error fetching target projects. Please try again.'),
          primaryButton: { text: __('Try again'), clickHandler: () => this.fetchProjects(search) },
        });
      }
    },
    searchProjects: debounce(function searchProjects(search) {
      this.fetchProjects(search);
    }, 500),
    selectProject(projectId) {
      this.currentProject = this.projects.find((p) => p.value === projectId);

      this.$emit('project-selected', this.currentProject.refsUrl);
    },
  },
};
</script>

<template>
  <div>
    <input
      id="merge_request_target_project_id"
      type="hidden"
      :value="currentProject.value"
      name="merge_request[target_project_id]"
      data-testid="target-project-input"
    />
    <gl-listbox
      v-model="selected"
      :items="projects"
      :toggle-text="currentProject.text"
      :header-text="__('Select target project')"
      :searching="isLoading"
      searchable
      class="gl-w-full dropdown-target-project"
      toggle-class="gl-align-items-flex-start! gl-justify-content-start! mr-compare-dropdown js-target-project"
      @shown="fetchProjects"
      @search="searchProjects"
      @select="selectProject"
    />
  </div>
</template>