Skip to content
Snippets Groups Projects
Commit aa7040f6 authored by L. Ellenbeck's avatar L. Ellenbeck Committed by Petar Hristov
Browse files

New: new search pagination (coscine/issues#1863)

parent 34990db2
Branches
Tags
2 merge requests!155Release: Sprint/2022 24 :robot:,!144New: new search pagination (coscine/issues#1863)
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
<b-form-select <b-form-select
v-model="selectedProject" v-model="selectedProject"
:options="allProjectsOptions" :options="allProjectsOptions"
@change="queryData(searchText)" @change="queryData(searchText, selectedPage, paginationPageSize)"
> >
<template #first> <template #first>
<b-form-select-option :value="null"> <b-form-select-option :value="null">
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
<b-form-select <b-form-select
v-model="selectedResource" v-model="selectedResource"
:options="projectResourcesOptions" :options="projectResourcesOptions"
@change="queryData(searchText)" @change="queryData(searchText, selectedPage, paginationPageSize)"
> >
<template #first> <template #first>
<b-form-select-option :value="null"> <b-form-select-option :value="null">
...@@ -56,7 +56,7 @@ ...@@ -56,7 +56,7 @@
id="searchButton" id="searchButton"
variant="primary" variant="primary"
:disabled="resultsViewLoading" :disabled="resultsViewLoading"
@click="queryData(searchText)" @click="queryData(searchText, selectedPage, paginationPageSize)"
> >
{{ $t("page.search.search") }} {{ $t("page.search.search") }}
<b-spinner v-if="resultsViewLoading" variant="secondary" /> <b-spinner v-if="resultsViewLoading" variant="secondary" />
...@@ -132,10 +132,10 @@ ...@@ -132,10 +132,10 @@
</template> </template>
<b-table <b-table
id="resultsView" id="resultsView"
:items="filteredResults" :items="searchResults"
:fields="resultsViewFields" :fields="resultsViewFields"
:per-page="paginationPerPage" :per-page="pagination ? pagination.PageSize : 0"
:current-page="paginationCurrentPage" :current-page="1"
thead-class="d-none" thead-class="d-none"
style="min-height: 100%" style="min-height: 100%"
small small
...@@ -161,9 +161,7 @@ ...@@ -161,9 +161,7 @@
<!-- Show footer if there are results and only on the last page --> <!-- Show footer if there are results and only on the last page -->
<div <div
v-if=" v-if="
foot.items.length > 0 && foot.items.length > 0 && pagination && !pagination.HasNext
paginationCurrentPage ===
Math.ceil(paginationTotalRows / paginationPerPage)
" "
class="p-2 text-center text-muted border-top" class="p-2 text-center text-muted border-top"
> >
...@@ -183,16 +181,16 @@ ...@@ -183,16 +181,16 @@
<b-col align-self="center" class="p-0"> <b-col align-self="center" class="p-0">
<b-pagination <b-pagination
id="pagination" id="pagination"
v-model="paginationCurrentPage" v-model="selectedPage"
:total-rows="paginationTotalRows" :total-rows="pagination ? pagination.TotalCount : 0"
:per-page="paginationPerPage" :per-page="pagination ? pagination.PageSize : 0"
aria-controls="resultsView" aria-controls="resultsView"
align="center" align="center"
/> />
</b-col> </b-col>
<b-col align-self="center" class="p-0"> <b-col align-self="center" class="p-0">
<b-form-select <b-form-select
v-model="paginationPerPage" v-model="paginationPageSize"
:options="paginationPerPageOptions" :options="paginationPerPageOptions"
style="max-width: 5rem" style="max-width: 5rem"
/> />
...@@ -216,10 +214,8 @@ import useResourceStore from "@/modules/resource/store"; ...@@ -216,10 +214,8 @@ import useResourceStore from "@/modules/resource/store";
import type { ProjectObject } from "@coscine/api-client/dist/types/Coscine.Api.Project"; import type { ProjectObject } from "@coscine/api-client/dist/types/Coscine.Api.Project";
import type { ResourceObject } from "@coscine/api-client/dist/types/Coscine.Api.Resources"; import type { ResourceObject } from "@coscine/api-client/dist/types/Coscine.Api.Resources";
import type { import type { ItemSearchResult } from "@coscine/api-client/dist/types/Coscine.Api.Search";
ItemSearchResult, import { Pagination } from "../types";
SearchResult,
} from "@coscine/api-client/dist/types/Coscine.Api.Search";
export default defineComponent({ export default defineComponent({
components: { components: {
...@@ -249,9 +245,9 @@ export default defineComponent({ ...@@ -249,9 +245,9 @@ export default defineComponent({
resultsViewFields: ["type"], resultsViewFields: ["type"],
resultsViewLoading: true, resultsViewLoading: true,
paginationCurrentPage: 1, paginationPerPageOptions: [5, 10, 20, 50],
paginationPerPage: 100, selectedPage: 1,
paginationPerPageOptions: [5, 10, 20, 50, 100], paginationPageSize: 10
}; };
}, },
...@@ -294,30 +290,34 @@ export default defineComponent({ ...@@ -294,30 +290,34 @@ export default defineComponent({
}); });
}, },
paginationTotalRows(): number { paginationTotalRows(): number {
return this.filteredResults.length; if(this.searchStore.pagination?.TotalCount){
return this.searchStore.pagination.TotalCount;
} else return 0;
},
pagination(): Pagination | null {
return this.searchStore.pagination;
}, },
resultsForTable(): ItemSearchResult[] { resultsForTable(): ItemSearchResult[] {
const result: ItemSearchResult[] = []; const result: ItemSearchResult[] = [];
if (this.searchResults) { if (this.searchResults) {
if (this.searchResults.items) {
result.push( result.push(
...this.searchResults.items.filter((item) => ...this.searchResults.filter((item) =>
this.isFilteredItem(item) this.isFilteredItem(item)
) )
); );
} }
}
return result; return result;
}, },
searchResults(): SearchResult | null { searchResults(): ItemSearchResult[] | null {
return this.searchStore.searchResults; return this.searchStore.searchResults;
}, },
}, },
watch: { watch: {
searchText() { searchText() {
this.queryData(this.searchText); this.queryData(this.searchText, this.selectedPage, this.paginationPageSize);
}, },
selectedProject() { selectedProject() {
this.selectedResource = null; this.selectedResource = null;
this.projectResources = null; this.projectResources = null;
...@@ -326,8 +326,17 @@ export default defineComponent({ ...@@ -326,8 +326,17 @@ export default defineComponent({
this.retrieveResources(); this.retrieveResources();
this.retrieveSubProjects(); this.retrieveSubProjects();
}, },
resultsForTable() {
this.assignResultsFilter(); // resultsForTable() {
// this.assignResultsFilter();
// },
selectedPage() {
this.queryData(this.searchText, this.selectedPage, this.paginationPageSize);
},
paginationPageSize() {
this.queryData(this.searchText, this.selectedPage, this.paginationPageSize);
}, },
}, },
...@@ -362,11 +371,11 @@ export default defineComponent({ ...@@ -362,11 +371,11 @@ export default defineComponent({
const query = Object.fromEntries(urlSearchParams.entries()); const query = Object.fromEntries(urlSearchParams.entries());
return query !== null ? decodeURIComponent(query.q) : ""; return query !== null ? decodeURIComponent(query.q) : "";
}, },
async queryData(query: string) { async queryData(query: string, pageNumber: number, pageSize: number) {
this.resultsViewLoading = true; this.resultsViewLoading = true;
// attach search query, resulting in "/search?q=<searchTerm>" // attach search query, resulting in "/search?q=<searchTerm>"
this.$router.push({ name: "search", query: { q: query } }); this.$router.push({ name: "search", query: { q: query } });
await this.searchStore.retrieveSearchResults(query, this.selectedProject); await this.searchStore.retrieveSearchResults(query, pageNumber, pageSize, this.selectedProject);
this.resultsViewLoading = false; this.resultsViewLoading = false;
}, },
async retrieveResources() { async retrieveResources() {
......
...@@ -20,6 +20,7 @@ export const useSearchStore = defineStore({ ...@@ -20,6 +20,7 @@ export const useSearchStore = defineStore({
*/ */
state: (): SearchState => ({ state: (): SearchState => ({
searchResults: null, searchResults: null,
pagination: null
}), }),
/* /*
...@@ -45,6 +46,8 @@ export const useSearchStore = defineStore({ ...@@ -45,6 +46,8 @@ export const useSearchStore = defineStore({
actions: { actions: {
async retrieveSearchResults( async retrieveSearchResults(
query: string, query: string,
pageNumber: number,
pageSize: number,
projectObject?: ProjectObject | null projectObject?: ProjectObject | null
): Promise<boolean> { ): Promise<boolean> {
const notificationStore = useNotificationStore(); const notificationStore = useNotificationStore();
...@@ -53,7 +56,12 @@ export const useSearchStore = defineStore({ ...@@ -53,7 +56,12 @@ export const useSearchStore = defineStore({
if (projectObject && projectObject.id) { if (projectObject && projectObject.id) {
adaptedQuery = `(${adaptedQuery}) + (belongsToProject: "https://purl.org/coscine/projects/${projectObject.id}")`; adaptedQuery = `(${adaptedQuery}) + (belongsToProject: "https://purl.org/coscine/projects/${projectObject.id}")`;
} }
const response = await SearchApi.searchSearch(adaptedQuery); const response = await SearchApi.searchSearch(adaptedQuery, undefined, undefined, undefined, pageNumber, pageSize);
if(response.headers["x-pagination"]){
this.pagination = JSON.parse(response.headers["x-pagination"]);
}
this.searchResults = response.data; this.searchResults = response.data;
return true; return true;
} catch (error) { } catch (error) {
......
import type { SearchResult } from "@coscine/api-client/dist/types/Coscine.Api.Search"; import type { ItemSearchResult } from "@coscine/api-client/dist/types/Coscine.Api.Search";
export interface Pagination {
TotalCount: number;
PageSize: number;
CurrentPage: number;
TotalPages: number;
HasNext: boolean;
HasPrevious: boolean;
}
export interface SearchState { export interface SearchState {
/* /*
...@@ -6,5 +15,6 @@ export interface SearchState { ...@@ -6,5 +15,6 @@ export interface SearchState {
STATE TYPE DEFINITION STATE TYPE DEFINITION
-------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------
*/ */
searchResults: SearchResult | null; pagination: Pagination | null;
searchResults: ItemSearchResult[] | null;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment