Commit 7c214e2e authored by Natalia Tepluhina's avatar Natalia Tepluhina

Merge branch 'fc-fix-branch-switcher-apollo-3-migration' into 'master'

Refactor branch switcher for apollo 3 migration

See merge request gitlab-org/gitlab!79551
parents c6eb1760 318618d5
...@@ -9,7 +9,6 @@ import { ...@@ -9,7 +9,6 @@ import {
GlTooltipDirective, GlTooltipDirective,
} from '@gitlab/ui'; } from '@gitlab/ui';
import { produce } from 'immer'; import { produce } from 'immer';
import { fetchPolicies } from '~/lib/graphql';
import { historyPushState } from '~/lib/utils/common_utils'; import { historyPushState } from '~/lib/utils/common_utils';
import { setUrlParams } from '~/lib/utils/url_utility'; import { setUrlParams } from '~/lib/utils/url_utility';
import { __ } from '~/locale'; import { __ } from '~/locale';
...@@ -63,8 +62,6 @@ export default { ...@@ -63,8 +62,6 @@ export default {
return { return {
availableBranches: [], availableBranches: [],
branchSelected: null, branchSelected: null,
filteredBranches: [],
isSearchingBranches: false,
pageLimit: this.paginationLimit, pageLimit: this.paginationLimit,
pageCounter: 0, pageCounter: 0,
searchTerm: '', searchTerm: '',
...@@ -76,10 +73,9 @@ export default { ...@@ -76,10 +73,9 @@ export default {
query: getAvailableBranchesQuery, query: getAvailableBranchesQuery,
variables() { variables() {
return { return {
limit: this.paginationLimit,
offset: 0, offset: 0,
projectFullPath: this.projectFullPath, projectFullPath: this.projectFullPath,
searchPattern: '*', ...this.availableBranchesVariables,
}; };
}, },
update(data) { update(data) {
...@@ -116,14 +112,24 @@ export default { ...@@ -116,14 +112,24 @@ export default {
}, },
}, },
computed: { computed: {
branches() { availableBranchesVariables() {
return this.searchTerm.length > 0 ? this.filteredBranches : this.availableBranches; if (this.searchTerm.length > 0) {
return {
limit: this.totalBranches,
searchPattern: `*${this.searchTerm}*`,
};
}
return {
limit: this.paginationLimit,
searchPattern: '*',
};
}, },
enableBranchSwitcher() { enableBranchSwitcher() {
return this.branches.length > 0 || this.searchTerm.length > 0; return this.availableBranches.length > 0 || this.searchTerm.length > 0;
}, },
isBranchesLoading() { isBranchesLoading() {
return this.$apollo.queries.availableBranches.loading || this.isSearchingBranches; return this.$apollo.queries.availableBranches.loading;
}, },
}, },
watch: { watch: {
...@@ -134,38 +140,21 @@ export default { ...@@ -134,38 +140,21 @@ export default {
}, },
}, },
methods: { methods: {
availableBranchesQueryVars(varsOverride = {}) {
if (this.searchTerm.length > 0) {
return {
limit: this.totalBranches,
offset: 0,
projectFullPath: this.projectFullPath,
searchPattern: `*${this.searchTerm}*`,
...varsOverride,
};
}
return {
limit: this.paginationLimit,
offset: this.pageCounter * this.paginationLimit,
projectFullPath: this.projectFullPath,
searchPattern: '*',
...varsOverride,
};
},
// if there is no searchPattern, paginate by {paginationLimit} branches // if there is no searchPattern, paginate by {paginationLimit} branches
fetchNextBranches() { fetchNextBranches() {
if ( if (
this.isBranchesLoading || this.isBranchesLoading ||
this.searchTerm.length > 0 || this.searchTerm.length > 0 ||
this.branches.length >= this.totalBranches this.availableBranches.length >= this.totalBranches
) { ) {
return; return;
} }
this.$apollo.queries.availableBranches this.$apollo.queries.availableBranches
.fetchMore({ .fetchMore({
variables: this.availableBranchesQueryVars(), variables: {
offset: this.pageCounter * this.paginationLimit,
},
updateQuery(previousResult, { fetchMoreResult }) { updateQuery(previousResult, { fetchMoreResult }) {
const previousBranches = previousResult.project.repository.branchNames; const previousBranches = previousResult.project.repository.branchNames;
const newBranches = fetchMoreResult.project.repository.branchNames; const newBranches = fetchMoreResult.project.repository.branchNames;
...@@ -204,23 +193,6 @@ export default { ...@@ -204,23 +193,6 @@ export default {
async setSearchTerm(newSearchTerm) { async setSearchTerm(newSearchTerm) {
this.pageCounter = 0; this.pageCounter = 0;
this.searchTerm = newSearchTerm.trim(); this.searchTerm = newSearchTerm.trim();
if (this.searchTerm === '') {
this.pageLimit = this.paginationLimit;
return;
}
this.isSearchingBranches = true;
const fetchResults = await this.$apollo
.query({
query: getAvailableBranchesQuery,
fetchPolicy: fetchPolicies.NETWORK_ONLY,
variables: this.availableBranchesQueryVars(),
})
.catch(this.showFetchError);
this.isSearchingBranches = false;
this.filteredBranches = fetchResults?.data?.project?.repository?.branchNames || [];
}, },
showFetchError() { showFetchError() {
this.$emit('showError', { this.$emit('showError', {
...@@ -255,14 +227,14 @@ export default { ...@@ -255,14 +227,14 @@ export default {
</gl-dropdown-section-header> </gl-dropdown-section-header>
<gl-infinite-scroll <gl-infinite-scroll
:fetched-items="branches.length" :fetched-items="availableBranches.length"
:max-list-height="250" :max-list-height="250"
data-qa-selector="branch_menu_container" data-qa-selector="branch_menu_container"
@bottomReached="fetchNextBranches" @bottomReached="fetchNextBranches"
> >
<template #items> <template #items>
<gl-dropdown-item <gl-dropdown-item
v-for="branch in branches" v-for="branch in availableBranches"
:key="branch" :key="branch"
:is-checked="currentBranch === branch" :is-checked="currentBranch === branch"
:is-check-item="true" :is-check-item="true"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment