All files / app/assets/javascripts/invite_members/components project_select.vue

86.66% Statements 13/15
66.66% Branches 4/6
86.66% Functions 13/15
86.66% Lines 13/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                      2x                                               6x                   10x           10x         5x       6x       11x 11x 11x   10x                   7x     11x       11x                                                                                              
<script>
import { GlAvatarLabeled, GlCollapsibleListbox } 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, PROJECT_SELECT_LABEL_ID } from '../constants';
 
// We can have GlCollapsibleListbox dropdown panel with full
// width once we implement
// https://gitlab.com/gitlab-org/gitlab-ui/-/issues/2133
// https://gitlab.com/gitlab-org/gitlab/-/issues/390411
export default {
  name: 'ProjectSelect',
  components: {
    GlAvatarLabeled,
    GlCollapsibleListbox,
  },
  model: {
    prop: 'selectedProjectId',
  },
  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: [],
      selectedProjectId: '',
      searchTerm: '',
      errorMessage: '',
    };
  },
  computed: {
    selectedProjectName() {
      return this.selectedProject.nameWithNamespace || this.$options.i18n.dropdownText;
    },
    isFetchResultEmpty() {
      return this.projects.length === 0 && !this.isFetching;
    },
    selectedProject() {
      return this.projects.find((prj) => prj.id === this.selectedProjectId) || {};
    },
  },
  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),
            text: project.name_with_namespace,
            value: project.id,
          }));
        })
        .catch(() => {
          // To be displayed in GlCollapsibleListbox once we implement
          // https://gitlab.com/gitlab-org/gitlab-ui/-/issues/2132
          // https://gitlab.com/gitlab-org/gitlab/-/issues/389974
          this.errorMessage = this.$options.i18n.errorFetchingProjects;
        })
        .finally(() => {
          this.isFetching = false;
        });
    }, SEARCH_DELAY),
    fetchProjects() {
      return getProjects(this.searchTerm, this.$options.defaultFetchOptions);
    },
    selectProject() {
      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.',
    ),
    projectSelectLabelId: PROJECT_SELECT_LABEL_ID,
  },
  defaultFetchOptions: {
    exclude_internal: true,
    active: true,
  },
};
</script>
<template>
  <gl-collapsible-listbox
    v-model="selectedProjectId"
    searchable
    :items="projects"
    :searching="isFetching"
    :toggle-text="selectedProjectName"
    :toggle-aria-labelled-by="$options.projectSelectLabelId"
    :search-placeholder="$options.i18n.searchPlaceholder"
    :no-results-text="$options.i18n.emptySearchResult"
    data-testid="project-select-dropdown"
    class="gl-collapsible-listbox-w-full"
    @search="searchTerm = $event"
    @select="selectProject"
  >
    <template #list-item="{ item }">
      <gl-avatar-labeled
        :label="item.text"
        :src="item.avatarUrl"
        :entity-id="item.id"
        :entity-name="item.name"
        :size="32"
      />
    </template>
  </gl-collapsible-listbox>
</template>