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

36.36% Statements 8/22
80% Branches 4/5
46.66% Functions 7/15
38.09% Lines 8/21

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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163        13x                                                                           11x                       11x           11x                 11x           22x     22x                   11x                                                                                                                                                    
<script>
import { GlCollapsibleListbox } from '@gitlab/ui';
import { s__ } from '~/locale';
import groupProjectsQuery from '../graphql/group_projects.query.graphql';
import { setError } from '../graphql/cache_updates';
 
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`),
    errorFetchingProjects: s__(
      'Boards|An error occurred while fetching group projects. Please try again.',
    ),
  },
  defaultFetchOptions: {
    with_issues_enabled: true,
    with_shared: false,
    include_subgroups: true,
    order_by: 'similarity',
  },
  components: {
    GlCollapsibleListbox,
  },
  inject: ['groupId', 'fullPath'],
  model: {
    prop: 'selectedProject',
    event: 'selectProject',
  },
  props: {
    list: {
      type: Object,
      required: true,
    },
    selectedProject: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      initialLoading: true,
      selectedProjectId: '',
      searchTerm: '',
      projects: {},
      isLoadingMore: false,
    };
  },
  apollo: {
    projects: {
      query: groupProjectsQuery,
      variables() {
        return {
          fullPath: this.fullPath,
          search: this.searchTerm,
        };
      },
      update(data) {
        return data.group.projects;
      },
      error(error) {
        setError({
          error,
          message: this.$options.i18n.errorFetchingProjects,
        });
      },
      result() {
        this.initialLoading = false;
      },
    },
  },
  computed: {
    isLoading() {
      return this.$apollo.queries.projects.loading && !this.isLoadingMore;
    },
    activeGroupProjects() {
      return (
        this.projects?.nodes
          ?.filter((p) => !p.archived)
          .map((project) => ({
            value: project.id,
            text: project.nameWithNamespace,
          })) || []
      );
    },
    selectedProjectName() {
      return this.selectedProject.name || this.$options.i18n.dropdownText;
    },
    isFetchResultEmpty() {
      return this.activeGroupProjects.length === 0;
    },
    hasNextPage() {
      return this.projects.pageInfo?.hasNextPage;
    },
  },
  watch: {
    endCursor() {
      return this.projects.pageInfo?.endCursor;
    },
  },
  methods: {
    selectProject(projectId) {
      this.selectedProjectId = projectId;
      this.$emit(
        'selectProject',
        this.projects.nodes.find((project) => project.id === projectId),
      );
    },
    async loadMoreProjects() {
      Iif (!this.hasNextPage) return;
      this.isLoadingMore = true;
      try {
        await this.$apollo.queries.projects.fetchMore({
          variables: {
            fullPath: this.fullPath,
            search: this.searchTerm,
            after: this.endCursor,
          },
        });
      } catch (error) {
        setError({
          error,
          message: this.$options.i18n.errorFetchingProjects,
        });
      } finally {
        this.isLoadingMore = false;
      }
    },
    onSearch(query) {
      this.searchTerm = query;
    },
  },
};
</script>
 
<template>
  <div>
    <label class="gl-font-weight-bold gl-mt-3" data-testid="header-label">{{
      $options.i18n.headerTitle
    }}</label>
    <gl-collapsible-listbox
      v-model="selectedProjectId"
      block
      searchable
      infinite-scroll
      data-testid="project-select-dropdown"
      :items="activeGroupProjects"
      :toggle-text="selectedProjectName"
      :header-text="$options.i18n.headerTitle"
      :loading="initialLoading"
      :searching="isLoading"
      :search-placeholder="$options.i18n.searchPlaceholder"
      :no-results-text="$options.i18n.emptySearchResult"
      :infinite-scroll-loading="isLoadingMore"
      @select="selectProject"
      @search="onSearch"
      @bottom-reached="loadMoreProjects"
    />
  </div>
</template>