All files / ee/app/assets/javascripts/analytics/repository_analytics/components select_projects_dropdown.vue

89.65% Statements 26/29
33.33% Branches 1/3
90.9% Functions 20/22
89.65% Lines 26/29

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          4x                                           18x         12x                 18x 18x     6x                       18x                 30x         40x 39x   1x     1x     1x     30x             40x         2x 2x     1x   1x     1x 1x     6x     2x             2x 2x         2x                                                                          
<script>
import { GlCollapsibleListbox } from '@gitlab/ui';
import produce from 'immer';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import { __, n__ } from '~/locale';
import getGroupProjects from '../graphql/queries/get_group_projects.query.graphql';
 
export default {
  i18n: {
    selectAllLabel: __('Select all'),
    clearAllLabel: __('Clear all'),
    projectDropdownHeader: __('Projects'),
    projectDropdownAllProjects: __('All projects'),
  },
  name: 'SelectProjectsDropdown',
  components: {
    GlCollapsibleListbox,
  },
  inject: {
    groupFullPath: {
      default: '',
    },
  },
  apollo: {
    groupProjects: {
      query: getGroupProjects,
      variables() {
        return {
          groupFullPath: this.groupFullPath,
        };
      },
      update(data) {
        return (
          data.group?.projects?.nodes?.map((project) => ({
            ...project,
            parsedId: getIdFromGraphQLId(project.id),
            isSelected: false,
          })) || []
        );
      },
      result({ data }) {
        this.projectsPageInfo = data?.group?.projects?.pageInfo || {};
        this.selectedProjectsIds = data?.group?.projects?.nodes?.map(({ id }) => id) || [];
      },
      error() {
        this.handleError();
      },
    },
  },
  props: {
    placement: {
      type: String,
      required: false,
      default: 'left',
    },
  },
  data() {
    return {
      groupProjects: [],
      projectsPageInfo: {},
      projectSearchTerm: '',
      selectedProjectsIds: [],
    };
  },
  computed: {
    filteredProjects() {
      return this.groupProjects.filter((project) =>
        project.name.toLowerCase().includes(this.projectSearchTerm.toLowerCase()),
      );
    },
    dropdownPlaceholder() {
      if (this.selectedProjectsIds.length === this.groupProjects.length) {
        return __('All projects selected');
      }
      Iif (this.selectedProjectsIds.length) {
        return n__('%d project selected', '%d projects selected', this.selectedProjectsIds.length);
      }
      return __('Select projects');
    },
    groupProjectsIds() {
      return this.groupProjects.map(({ id }) => id);
    },
    listBoxItems() {
      return this.filteredProjects.map((project) => ({
        value: project.id,
        text: project.name,
        ...project,
      }));
    },
    loading() {
      return this.$apollo.queries.groupProjects.loading;
    },
  },
  methods: {
    clickDropdownProject(ids) {
      this.selectedProjectsIds = ids;
      this.$emit('select-project', ids);
    },
    clickSelectAllProjects() {
      this.selectedProjectsIds = this.groupProjectsIds;
 
      this.$emit('select-all-projects', this.selectedProjectsIds);
    },
    resetAllProjects() {
      this.selectedProjectsIds = [];
      this.$emit('select-all-projects', []);
    },
    handleError() {
      this.$emit('projects-query-error');
    },
    loadMoreProjects() {
      this.$apollo.queries.groupProjects
        .fetchMore({
          variables: {
            groupFullPath: this.groupFullPath,
            after: this.projectsPageInfo.endCursor,
          },
          updateQuery(previousResult, { fetchMoreResult }) {
            const results = produce(fetchMoreResult, (draftData) => {
              draftData.group.projects.nodes = [
                ...previousResult.group.projects.nodes,
                ...draftData.group.projects.nodes,
              ];
            });
            return results;
          },
        })
        .catch(() => {
          this.handleError();
        });
    },
    setProjectSearchTerm(term = '') {
      this.projectSearchTerm = term.trim();
    },
  },
};
</script>
 
<template>
  <gl-collapsible-listbox
    block
    multiple
    searchable
    is-check-centered
    :placement="placement"
    :header-text="$options.i18n.projectDropdownHeader"
    :items="listBoxItems"
    :infinite-scroll="projectsPageInfo.hasNextPage"
    :infinite-scroll-loading="loading"
    :loading="loading"
    :selected="selectedProjectsIds"
    :show-select-all-button-label="$options.i18n.selectAllLabel"
    :reset-button-label="$options.i18n.clearAllLabel"
    :toggle-text="dropdownPlaceholder"
    @bottom-reached="loadMoreProjects"
    @reset="resetAllProjects"
    @search="setProjectSearchTerm"
    @select="clickDropdownProject"
    @select-all="clickSelectAllProjects"
  />
</template>