All files / ee/app/assets/javascripts/external_issues_list/components external_issues_list_root.vue

94.33% Statements 50/53
84.44% Branches 38/45
90.47% Functions 19/21
96.07% Lines 49/51

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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316                                          2x                                                                                 24x                                 56x     56x     27x                   32x                               48x     32x                             31x 1x     30x 30x 6x 6x     24x 24x 24x 24x     1x           56x                                   56x 56x   56x 14x               56x 14x               56x     7x   7x     1x 1x     1x     1x 1x     5x 5x 5x   5x 8x   8x   4x 4x   4x 4x           5x 3x     5x 3x     5x                                                                                                                                                              
<script>
import { GlButton, GlIcon, GlLink, GlSprintf, GlAlert } from '@gitlab/ui';
import * as Sentry from '~/sentry/sentry_browser_wrapper';
import SafeHtml from '~/vue_shared/directives/safe_html';
 
import IssuableList from '~/vue_shared/issuable/list/components/issuable_list_root.vue';
import {
  issuableListTabs,
  availableSortOptions,
  DEFAULT_PAGE_SIZE,
} from '~/vue_shared/issuable/list/constants';
import { STATUS_ALL, STATUS_CLOSED, STATUS_OPEN } from '~/issues/constants';
import { i18n } from '~/issues/list/constants';
import {
  FILTERED_SEARCH_TERM,
  OPERATORS_IS,
  TOKEN_TITLE_LABEL,
  TOKEN_TYPE_LABEL,
} from '~/vue_shared/components/filtered_search_bar/constants';
import LabelToken from '~/vue_shared/components/filtered_search_bar/tokens/label_token.vue';
 
import ExternalIssuesListEmptyState from './external_issues_list_empty_state.vue';
 
export default {
  name: 'ExternalIssuesList',
  issuableListTabs,
  availableSortOptions,
  defaultPageSize: DEFAULT_PAGE_SIZE,
  components: {
    GlButton,
    GlIcon,
    GlLink,
    GlSprintf,
    GlAlert,
    IssuableList,
    ExternalIssuesListEmptyState,
  },
  directives: {
    SafeHtml,
  },
  inject: [
    'initialState',
    'initialSortBy',
    'page',
    'issuesFetchPath',
    'projectFullPath',
    'issueCreateUrl',
    'getIssuesQuery',
    'externalIssuesLogo',
    'externalIssueTrackerName',
    'searchInputPlaceholderText',
    'recentSearchesStorageKey',
    'createNewIssueText',
  ],
  props: {
    initialFilterParams: {
      type: Object,
      required: false,
      default: () => ({}),
    },
  },
  data() {
    return {
      issues: [],
      totalIssues: 0,
      currentState: this.initialState,
      filterParams: this.initialFilterParams,
      sortedBy: this.initialSortBy,
      currentPage: this.page,
      issuesCount: {
        [STATUS_OPEN]: 0,
        [STATUS_CLOSED]: 0,
        [STATUS_ALL]: 0,
      },
      errorMessage: null,
    };
  },
  computed: {
    issuesListLoading() {
      return this.$apollo.queries.externalIssues.loading;
    },
    showPaginationControls() {
      return Boolean(!this.issuesListLoading && this.issues.length && this.totalIssues > 1);
    },
    hasFiltersApplied() {
      return Boolean(
        this.filterParams.project ||
          this.filterParams.status ||
          this.filterParams.authorUsername ||
          this.filterParams.assigneeUsername ||
          this.filterParams.labels ||
          this.filterParams.search,
      );
    },
    urlParams() {
      return {
        project: this.filterParams.project,
        status: this.filterParams.status,
        author_username: this.filterParams.authorUsername,
        assignee_username: this.filterParams.assigneeUsername,
        'labels[]': this.filterParams.labels,
        search: this.filterParams.search,
        ...(this.currentPage === 1 ? {} : { page: this.currentPage }),
        ...(this.sortedBy === this.initialSortBy ? {} : { sort: this.sortedBy }),
        ...(this.currentState === this.initialState ? {} : { state: this.currentState }),
      };
    },
  },
  apollo: {
    externalIssues: {
      query() {
        return this.getIssuesQuery;
      },
      variables() {
        return {
          issuesFetchPath: this.issuesFetchPath,
          page: this.currentPage, // navigation attributes
          sort: this.sortedBy, // navigation attributes
          state: this.currentState, // navigation attributes
          project: this.filterParams.project, // filter attributes
          status: this.filterParams.status, // filter attributes
          authorUsername: this.filterParams.authorUsername, // filter attributes
          assigneeUsername: this.filterParams.assigneeUsername, // filter attributes
          labels: this.filterParams.labels, // filter attributes
          search: this.filterParams.search, // filter attributes
        };
      },
      result({ data, error }) {
        // let error() callback handle errors
        if (error) {
          return;
        }
 
        const { pageInfo, nodes, errors } = data?.externalIssues ?? {};
        if (errors?.length > 0) {
          this.onExternalIssuesQueryError(new Error(errors[0]));
          return;
        }
 
        this.issues = nodes;
        this.currentPage = pageInfo.page;
        this.totalIssues = pageInfo.total;
        this.issuesCount[this.currentState] = nodes.length;
      },
      error(error) {
        this.onExternalIssuesQueryError(error, i18n.errorFetchingIssues);
      },
    },
  },
  methods: {
    getFilteredSearchTokens() {
      return [
        {
          type: TOKEN_TYPE_LABEL,
          icon: 'labels',
          symbol: '~',
          title: TOKEN_TITLE_LABEL,
          unique: false,
          token: LabelToken,
          operators: OPERATORS_IS,
          defaultLabels: [],
          suggestionsDisabled: true,
          fetchLabels: () => {
            return Promise.resolve([]);
          },
        },
      ];
    },
    getFilteredSearchValue() {
      const { labels, search } = this.filterParams || {};
      const filteredSearchValue = [];
 
      if (labels) {
        filteredSearchValue.push(
          ...labels.map((label) => ({
            type: TOKEN_TYPE_LABEL,
            value: { data: label },
          })),
        );
      }
 
      if (search) {
        filteredSearchValue.push({
          type: FILTERED_SEARCH_TERM,
          value: {
            data: search,
          },
        });
      }
 
      return filteredSearchValue;
    },
    onExternalIssuesQueryError(error, message) {
      this.errorMessage = message || error.message;
 
      Sentry.captureException(error);
    },
    onIssuableListClickTab(selectedIssueState) {
      this.currentPage = 1;
      this.currentState = selectedIssueState;
    },
    onIssuableListPageChange(selectedPage) {
      this.currentPage = selectedPage;
    },
    onIssuableListSort(selectedSort) {
      this.currentPage = 1;
      this.sortedBy = selectedSort;
    },
    onIssuableListFilter(filters = []) {
      const filterParams = {};
      const labels = [];
      const plainText = [];
 
      filters.forEach((filter) => {
        Iif (!filter.value.data) return;
 
        switch (filter.type) {
          case TOKEN_TYPE_LABEL:
            labels.push(filter.value.data);
            break;
          case FILTERED_SEARCH_TERM:
            plainText.push(filter.value.data);
            break;
          default:
            break;
        }
      });
 
      if (plainText.length) {
        filterParams.search = plainText.join(' ');
      }
 
      if (labels.length) {
        filterParams.labels = labels;
      }
 
      this.filterParams = {
        ...filterParams,
        project: this.filterParams.project,
        status: this.filterParams.status,
        authorUsername: this.filterParams.authorUsername,
        assigneeUsername: this.filterParams.assigneeUsername,
      };
    },
  },
  alertSafeHtmlConfig: { ALLOW_TAGS: ['a'] },
};
</script>
 
<template>
  <gl-alert v-if="errorMessage" class="gl-mt-3" variant="danger" :dismissible="false">
    <span v-safe-html:[$options.alertSafeHtmlConfig]="errorMessage"></span>
  </gl-alert>
  <issuable-list
    v-else
    :namespace="projectFullPath"
    :tabs="$options.issuableListTabs"
    :current-tab="currentState"
    :search-input-placeholder="searchInputPlaceholderText"
    :search-tokens="getFilteredSearchTokens()"
    :sort-options="$options.availableSortOptions"
    :initial-filter-value="getFilteredSearchValue()"
    :initial-sort-by="sortedBy"
    :issuables="issues"
    :issuables-loading="issuesListLoading"
    :show-pagination-controls="showPaginationControls"
    :default-page-size="$options.defaultPageSize"
    :total-items="totalIssues"
    :current-page="currentPage"
    :previous-page="currentPage - 1"
    :next-page="currentPage + 1"
    :url-params="urlParams"
    label-filter-param="labels"
    :recent-searches-storage-key="recentSearchesStorageKey"
    @click-tab="onIssuableListClickTab"
    @page-change="onIssuableListPageChange"
    @sort="onIssuableListSort"
    @filter="onIssuableListFilter"
  >
    <template #nav-actions>
      <gl-button :href="issueCreateUrl" target="_blank" class="gl-my-5">
        {{ createNewIssueText }}
        <gl-icon name="external-link" />
      </gl-button>
    </template>
    <template #reference="{ issuable }">
      <span
        v-safe-html="externalIssuesLogo"
        class="gl-display-inline-flex gl-vertical-align-text-bottom"
      ></span>
      <span v-if="issuable">
        {{ issuable.references ? issuable.references.relative : issuable.id }}
      </span>
    </template>
    <template #author="{ author }">
      <gl-sprintf :message="`%{authorName} in ${externalIssueTrackerName}`">
        <template #authorName>
          <gl-link class="author-link js-user-link" target="_blank" :href="author.webUrl">
            {{ author.name }}
          </gl-link>
        </template>
      </gl-sprintf>
    </template>
    <template #status="{ issuable }">
      <template v-if="issuable"> {{ issuable.status }} </template>
    </template>
    <template #empty-state>
      <external-issues-list-empty-state
        :current-state="currentState"
        :issues-count="issuesCount"
        :has-filters-applied="hasFiltersApplied"
      />
    </template>
  </issuable-list>
</template>