From 7a6fc100dd30824934d71f37a411154ca7d043cc Mon Sep 17 00:00:00 2001 From: guerler Date: Sat, 21 Oct 2023 14:59:26 +0300 Subject: [PATCH] Parse data and total count as tuple --- client/src/components/Grid/GridList.vue | 10 ++++------ client/src/components/Grid/configs/types.ts | 10 +--------- client/src/components/Grid/configs/visualizations.js | 6 ++++-- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/client/src/components/Grid/GridList.vue b/client/src/components/Grid/GridList.vue index 8f497f853880..73f9f85d46cb 100644 --- a/client/src/components/Grid/GridList.vue +++ b/client/src/components/Grid/GridList.vue @@ -79,21 +79,19 @@ function applyFilter(filter: string, value: string, quoted = false) { async function getGridData() { if (gridConfig.value) { try { - const response = await gridConfig.value.getData( + const [responseData, responseTotal] = await gridConfig.value.getData( currentPage.value, props.perPage, sortBy.value, sortDesc.value, searchTerm.value ); - if (response.headers.total_matches) { - totalRows.value = parseInt(response.headers.total_matches); - } - gridData.value = response.data; + gridData.value = responseData; + totalRows.value = responseTotal; errorMessage.value = ""; loading.value = false; } catch (e) { - errorMessage.value = "Failed to obtain grid data."; + errorMessage.value = `Failed to obtain grid data: ${e}`; loading.value = false; } } diff --git a/client/src/components/Grid/configs/types.ts b/client/src/components/Grid/configs/types.ts index b433b9d73b68..e78f73ce0fbf 100644 --- a/client/src/components/Grid/configs/types.ts +++ b/client/src/components/Grid/configs/types.ts @@ -1,5 +1,3 @@ -import type { AxiosResponse } from "axios"; - import Filtering from "@/utils/filtering"; interface Action { @@ -33,13 +31,7 @@ export interface Config { actions: Array; fields: Array; filtering: Filtering; - getData: ( - currentPage: number, - perPage: number, - sortBy: string, - sortDesc: boolean, - search: string - ) => Promise; + getData: (currentPage: number, perPage: number, sortBy: string, sortDesc: boolean, search: string) => Promise; item: string; plural: string; sortBy: string; diff --git a/client/src/components/Grid/configs/visualizations.js b/client/src/components/Grid/configs/visualizations.js index 73c7f05044f7..dee45ad963e4 100644 --- a/client/src/components/Grid/configs/visualizations.js +++ b/client/src/components/Grid/configs/visualizations.js @@ -7,13 +7,15 @@ import { errorMessageAsString, rethrowSimple } from "@/utils/simple-error"; /** * Request and return data from server */ -function getData(currentPage, perPage, sortBy, sortDesc, searchTerm) { +async function getData(currentPage, perPage, sortBy, sortDesc, searchTerm) { const offset = perPage * (currentPage - 1); let q = `/api/visualizations/detailed?limit=${perPage}&offset=${offset}&sort_by=${sortBy}&sort_desc=${sortDesc}`; if (searchTerm) { q += `&search=${searchTerm}`; } - return axios.get(withPrefix(q)); + const response = await axios.get(withPrefix(q)); + const responseTotal = parseInt(response.headers.total_matches); + return [response.data, responseTotal]; } /**