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

70% Statements 14/20
60% Branches 6/10
72.73% Functions 8/11
70% Lines 14/20

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 134 135 136 137 138 139 140 141 142 143  11x               11x 11x 11x 11x                                                               10x                   11x                           10x     10x                 10x   10x         2x 1x                 112x                                                                                          
<script>
import {
  GlDropdown,
  GlDropdownItem,
  GlDropdownText,
  GlSearchBoxByType,
  GlIntersectionObserver,
  GlLoadingIcon,
} from '@gitlab/ui';
import { mapActions, mapState, mapGetters } from 'vuex';
import { s__ } from '~/locale';
import { featureAccessLevel } from '~/pages/projects/shared/permissions/constants';
import { ListType } from '../constants';
 
export default {
  name: 'ProjectSelect',
  i18n: {
    headerTitle: s__(`BoardNewIssue|Projects`),
    dropdownText: s__(`BoardNewIssue|Select a project`),
    searchPlaceholder: s__(`BoardNewIssue|Search projects`),
    emptySearchResult: s__(`BoardNewIssue|No matching results`),
  },
  defaultFetchOptions: {
    with_issues_enabled: true,
    with_shared: false,
    include_subgroups: true,
    order_by: 'similarity',
  },
  components: {
    GlIntersectionObserver,
    GlLoadingIcon,
    GlDropdown,
    GlDropdownItem,
    GlDropdownText,
    GlSearchBoxByType,
  },
  inject: ['groupId'],
  props: {
    list: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      initialLoading: true,
      selectedProject: {},
      searchTerm: '',
    };
  },
  computed: {
    ...mapState(['groupProjectsFlags']),
    ...mapGetters(['activeGroupProjects']),
    selectedProjectName() {
      return this.selectedProject.name || this.$options.i18n.dropdownText;
    },
    fetchOptions() {
      const additionalAttrs = {};
      if (this.list.type && this.list.type !== ListType.backlog) {
        additionalAttrs.min_access_level = featureAccessLevel.EVERYONE;
      }
 
      return {
        ...this.$options.defaultFetchOptions,
        ...additionalAttrs,
      };
    },
    isFetchResultEmpty() {
      return this.activeGroupProjects.length === 0;
    },
    hasNextPage() {
      return this.groupProjectsFlags.pageInfo?.hasNextPage;
    },
  },
  watch: {
    searchTerm() {
      this.fetchGroupProjects({ search: this.searchTerm });
    },
  },
  mounted() {
    this.fetchGroupProjects({});
 
    this.initialLoading = false;
  },
  methods: {
    ...mapActions(['fetchGroupProjects', 'setSelectedProject']),
    selectProject(projectId) {
      this.selectedProject = this.activeGroupProjects.find((project) => project.id === projectId);
      this.setSelectedProject(this.selectedProject);
    },
    loadMoreProjects() {
      this.fetchGroupProjects({ search: this.searchTerm, fetchNext: true });
    },
  },
};
</script>
 
<template>
  <div>
    <label class="gl-font-weight-bold gl-mt-3" data-testid="header-label">{{
      $options.i18n.headerTitle
    }}</label>
    <gl-dropdown
      data-testid="project-select-dropdown"
      :text="selectedProjectName"
      :header-text="$options.i18n.headerTitle"
      block
      menu-class="gl-w-full!"
      :loading="initialLoading"
    >
      <gl-search-box-by-type
        v-model.trim="searchTerm"
        debounce="250"
        :placeholder="$options.i18n.searchPlaceholder"
      />
      <gl-dropdown-item
        v-for="project in activeGroupProjects"
        v-show="!groupProjectsFlags.isLoading"
        :key="project.id"
        :name="project.name"
        @click="selectProject(project.id)"
      >
        {{ project.nameWithNamespace }}
      </gl-dropdown-item>
      <gl-dropdown-text
        v-show="groupProjectsFlags.isLoading"
        data-testid="dropdown-text-loading-icon"
      >
        <gl-loading-icon class="gl-mx-auto" size="sm" />
      </gl-dropdown-text>
      <gl-dropdown-text
        v-if="isFetchResultEmpty && !groupProjectsFlags.isLoading"
        data-testid="empty-result-message"
      >
        <span class="gl-text-gray-500">{{ $options.i18n.emptySearchResult }}</span>
      </gl-dropdown-text>
      <gl-intersection-observer v-if="hasNextPage" @appear="loadMoreProjects">
        <gl-loading-icon v-if="groupProjectsFlags.isLoadingMore" size="md" />
      </gl-intersection-observer>
    </gl-dropdown>
  </div>
</template>