Welcome to mirror list, hosted at ThFree Co, Russian Federation.

project_select.vue « components « invite_members « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b7a3918813b0bf80c30c0fb6d234aa178211ccaf (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<script>
import {
  GlAvatarLabeled,
  GlDropdown,
  GlDropdownItem,
  GlDropdownText,
  GlSearchBoxByType,
} from '@gitlab/ui';
import { debounce } from 'lodash';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import { s__ } from '~/locale';
import { getProjects } from '~/rest_api';
import { SEARCH_DELAY, GROUP_FILTERS } from '../constants';

export default {
  name: 'ProjectSelect',
  components: {
    GlAvatarLabeled,
    GlDropdown,
    GlDropdownItem,
    GlDropdownText,
    GlSearchBoxByType,
  },
  model: {
    prop: 'selectedProject',
  },
  props: {
    groupsFilter: {
      type: String,
      required: false,
      default: GROUP_FILTERS.ALL,
      validator: (value) => Object.values(GROUP_FILTERS).includes(value),
    },
    parentGroupId: {
      type: Number,
      required: false,
      default: 0,
    },
  },
  data() {
    return {
      isFetching: false,
      projects: [],
      selectedProject: {},
      searchTerm: '',
      errorMessage: '',
    };
  },
  computed: {
    selectedProjectName() {
      return this.selectedProject.name || this.$options.i18n.dropdownText;
    },
    isFetchResultEmpty() {
      return this.projects.length === 0 && !this.isFetching;
    },
  },
  watch: {
    searchTerm() {
      this.retrieveProjects();
    },
  },
  mounted() {
    this.retrieveProjects();
  },
  methods: {
    retrieveProjects: debounce(function debouncedRetrieveProjects() {
      this.isFetching = true;
      this.errorMessage = '';
      return this.fetchProjects()
        .then((response) => {
          this.projects = response.data.map((project) => ({
            ...convertObjectPropsToCamelCase(project),
            name: project.name_with_namespace,
          }));
        })
        .catch(() => {
          this.errorMessage = this.$options.i18n.errorFetchingProjects;
        })
        .finally(() => {
          this.isFetching = false;
        });
    }, SEARCH_DELAY),
    fetchProjects() {
      return getProjects(this.searchTerm, this.$options.defaultFetchOptions);
    },
    selectProject(project) {
      this.selectedProject = project;

      this.$emit('input', this.selectedProject);
    },
  },
  i18n: {
    dropdownText: s__('ProjectSelect|Select a project'),
    searchPlaceholder: s__('ProjectSelect|Search projects'),
    emptySearchResult: s__('ProjectSelect|No matching results'),
    errorFetchingProjects: s__(
      'ProjectSelect|There was an error fetching the projects. Please try again.',
    ),
  },
  defaultFetchOptions: {
    exclude_internal: true,
    active: true,
  },
};
</script>
<template>
  <div>
    <gl-dropdown
      data-testid="project-select-dropdown"
      :text="selectedProjectName"
      toggle-class="gl-mb-2"
      block
      menu-class="gl-w-full!"
    >
      <gl-search-box-by-type
        v-model="searchTerm"
        :is-loading="isFetching"
        :placeholder="$options.i18n.searchPlaceholder"
        data-qa-selector="project_select_dropdown_search_field"
      />
      <gl-dropdown-item
        v-for="project in projects"
        :key="project.id"
        :name="project.name"
        @click="selectProject(project)"
      >
        <gl-avatar-labeled
          :label="project.name"
          :src="project.avatarUrl"
          :entity-id="project.id"
          :entity-name="project.name"
          :size="32"
        />
      </gl-dropdown-item>
      <gl-dropdown-text v-if="errorMessage" data-testid="error-message">
        <span class="gl-text-gray-500">{{ errorMessage }}</span>
      </gl-dropdown-text>
      <gl-dropdown-text v-else-if="isFetchResultEmpty" data-testid="empty-result-message">
        <span class="gl-text-gray-500">{{ $options.i18n.emptySearchResult }}</span>
      </gl-dropdown-text>
    </gl-dropdown>
  </div>
</template>