Commit 8a7a9d1d authored by Emily Ring's avatar Emily Ring Committed by Natalia Tepluhina

Add pagination to Terraform list view

Updated GraphQL query and terraform_list.vue to include pagination
parent 6a4151e4
<script> <script>
import { GlAlert, GlBadge, GlLoadingIcon, GlTab, GlTabs } from '@gitlab/ui'; import { GlAlert, GlBadge, GlKeysetPagination, GlLoadingIcon, GlTab, GlTabs } from '@gitlab/ui';
import getStatesQuery from '../graphql/queries/get_states.query.graphql'; import getStatesQuery from '../graphql/queries/get_states.query.graphql';
import EmptyState from './empty_state.vue'; import EmptyState from './empty_state.vue';
import StatesTable from './states_table.vue'; import StatesTable from './states_table.vue';
import { MAX_LIST_COUNT } from '../constants';
export default { export default {
apollo: { apollo: {
...@@ -11,12 +12,14 @@ export default { ...@@ -11,12 +12,14 @@ export default {
variables() { variables() {
return { return {
projectPath: this.projectPath, projectPath: this.projectPath,
...this.cursor,
}; };
}, },
update: data => { update: data => {
return { return {
count: data?.project?.terraformStates?.count, count: data?.project?.terraformStates?.count,
list: data?.project?.terraformStates?.nodes, list: data?.project?.terraformStates?.nodes,
pageInfo: data?.project?.terraformStates?.pageInfo,
}; };
}, },
error() { error() {
...@@ -28,6 +31,7 @@ export default { ...@@ -28,6 +31,7 @@ export default {
EmptyState, EmptyState,
GlAlert, GlAlert,
GlBadge, GlBadge,
GlKeysetPagination,
GlLoadingIcon, GlLoadingIcon,
GlTab, GlTab,
GlTabs, GlTabs,
...@@ -43,10 +47,26 @@ export default { ...@@ -43,10 +47,26 @@ export default {
type: String, type: String,
}, },
}, },
data() {
return {
cursor: {
first: MAX_LIST_COUNT,
after: null,
last: null,
before: null,
},
};
},
computed: { computed: {
isLoading() { isLoading() {
return this.$apollo.queries.states.loading; return this.$apollo.queries.states.loading;
}, },
pageInfo() {
return this.states?.pageInfo || {};
},
showPagination() {
return this.pageInfo.hasPreviousPage || this.pageInfo.hasNextPage;
},
statesCount() { statesCount() {
return this.states?.count; return this.states?.count;
}, },
...@@ -54,6 +74,25 @@ export default { ...@@ -54,6 +74,25 @@ export default {
return this.states?.list; return this.states?.list;
}, },
}, },
methods: {
updatePagination(item) {
if (item === this.pageInfo.endCursor) {
this.cursor = {
first: MAX_LIST_COUNT,
after: item,
last: null,
before: null,
};
} else {
this.cursor = {
first: null,
after: null,
last: MAX_LIST_COUNT,
before: item,
};
}
},
},
}; };
</script> </script>
...@@ -71,7 +110,17 @@ export default { ...@@ -71,7 +110,17 @@ export default {
<gl-loading-icon v-if="isLoading" size="md" class="gl-mt-3" /> <gl-loading-icon v-if="isLoading" size="md" class="gl-mt-3" />
<div v-else-if="statesList"> <div v-else-if="statesList">
<states-table v-if="statesCount" :states="statesList" /> <div v-if="statesCount">
<states-table :states="statesList" />
<div v-if="showPagination" class="gl-display-flex gl-justify-content-center gl-mt-5">
<gl-keyset-pagination
v-bind="pageInfo"
@prev="updatePagination"
@next="updatePagination"
/>
</div>
</div>
<empty-state v-else :image="emptyStateImage" /> <empty-state v-else :image="emptyStateImage" />
</div> </div>
......
export const MAX_LIST_COUNT = 25;
#import "../fragments/state.fragment.graphql" #import "../fragments/state.fragment.graphql"
#import "~/graphql_shared/fragments/pageInfo.fragment.graphql"
query getStates($projectPath: ID!) { query getStates($projectPath: ID!, $first: Int, $last: Int, $before: String, $after: String) {
project(fullPath: $projectPath) { project(fullPath: $projectPath) {
terraformStates { terraformStates(first: $first, last: $last, before: $before, after: $after) {
count count
nodes { nodes {
...State ...State
} }
pageInfo {
...PageInfo
}
} }
} }
} }
---
title: Add pagination to Terraform list view
merge_request: 47412
author:
type: changed
import { GlAlert, GlBadge, GlLoadingIcon, GlTab } from '@gitlab/ui'; import { GlAlert, GlBadge, GlKeysetPagination, GlLoadingIcon, GlTab } from '@gitlab/ui';
import { createLocalVue, shallowMount } from '@vue/test-utils'; import { createLocalVue, shallowMount } from '@vue/test-utils';
import createMockApollo from 'jest/helpers/mock_apollo_helper'; import createMockApollo from 'jest/helpers/mock_apollo_helper';
import VueApollo from 'vue-apollo'; import VueApollo from 'vue-apollo';
...@@ -39,6 +39,7 @@ describe('TerraformList', () => { ...@@ -39,6 +39,7 @@ describe('TerraformList', () => {
const findBadge = () => wrapper.find(GlBadge); const findBadge = () => wrapper.find(GlBadge);
const findEmptyState = () => wrapper.find(EmptyState); const findEmptyState = () => wrapper.find(EmptyState);
const findPaginationButtons = () => wrapper.find(GlKeysetPagination);
const findStatesTable = () => wrapper.find(StatesTable); const findStatesTable = () => wrapper.find(StatesTable);
const findTab = () => wrapper.find(GlTab); const findTab = () => wrapper.find(GlTab);
...@@ -73,6 +74,12 @@ describe('TerraformList', () => { ...@@ -73,6 +74,12 @@ describe('TerraformList', () => {
terraformStates: { terraformStates: {
nodes: states, nodes: states,
count: states.length, count: states.length,
pageInfo: {
hasNextPage: true,
hasPreviousPage: false,
startCursor: 'prev',
endCursor: 'next',
},
}, },
}); });
...@@ -84,8 +91,33 @@ describe('TerraformList', () => { ...@@ -84,8 +91,33 @@ describe('TerraformList', () => {
expect(findBadge().text()).toBe('2'); expect(findBadge().text()).toBe('2');
}); });
it('renders the states table', () => { it('renders the states table and pagination buttons', () => {
expect(findStatesTable().exists()).toBe(true); expect(findStatesTable().exists()).toBe(true);
expect(findPaginationButtons().exists()).toBe(true);
});
describe('when list has no additional pages', () => {
beforeEach(() => {
createWrapper({
terraformStates: {
nodes: states,
count: states.length,
pageInfo: {
hasNextPage: false,
hasPreviousPage: false,
startCursor: '',
endCursor: '',
},
},
});
return wrapper.vm.$nextTick();
});
it('renders the states table without pagination buttons', () => {
expect(findStatesTable().exists()).toBe(true);
expect(findPaginationButtons().exists()).toBe(false);
});
}); });
}); });
...@@ -95,6 +127,7 @@ describe('TerraformList', () => { ...@@ -95,6 +127,7 @@ describe('TerraformList', () => {
terraformStates: { terraformStates: {
nodes: [], nodes: [],
count: 0, count: 0,
pageInfo: null,
}, },
}); });
......
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