From 4a6d56f638e3ef0121850e27b80dd5b0ebebbc21 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 14 Oct 2021 10:20:38 +0100 Subject: [PATCH] Improve pagination state logic to avoid potential windows where state can get out of sync --- packages/builder/src/helpers/fetchTableData.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/builder/src/helpers/fetchTableData.js b/packages/builder/src/helpers/fetchTableData.js index 3623426fbd..8b413f3611 100644 --- a/packages/builder/src/helpers/fetchTableData.js +++ b/packages/builder/src/helpers/fetchTableData.js @@ -48,7 +48,6 @@ export const fetchTableData = opts => { const fetchPage = async bookmark => { lastBookmark = bookmark const { tableId, limit, sortColumn, sortOrder, paginate } = options - store.update($store => ({ ...$store, loading: true })) const res = await API.post(`/api/${options.tableId}/search`, { tableId, query, @@ -59,7 +58,6 @@ export const fetchTableData = opts => { paginate, bookmark, }) - store.update($store => ({ ...$store, loading: false, loaded: true })) return await res.json() } @@ -103,7 +101,7 @@ export const fetchTableData = opts => { if (!schema) { return } - store.update($store => ({ ...$store, schema })) + store.update($store => ({ ...$store, schema, loading: true })) // Work out what sort type to use if (!sortColumn || !schema[sortColumn]) { @@ -135,6 +133,7 @@ export const fetchTableData = opts => { } // Fetch next page + store.update($store => ({ ...$store, loading: true })) const page = await fetchPage(state.bookmarks[state.pageNumber + 1]) // Update state @@ -148,6 +147,7 @@ export const fetchTableData = opts => { pageNumber: pageNumber + 1, rows: page.rows, bookmarks, + loading: false, } }) } @@ -160,6 +160,7 @@ export const fetchTableData = opts => { } // Fetch previous page + store.update($store => ({ ...$store, loading: true })) const page = await fetchPage(state.bookmarks[state.pageNumber - 1]) // Update state @@ -168,6 +169,7 @@ export const fetchTableData = opts => { ...$store, pageNumber: $store.pageNumber - 1, rows: page.rows, + loading: false, } }) }