From cf7497d820dc52e1070ca955e6dccaac1b209a59 Mon Sep 17 00:00:00 2001 From: Vesa Luusua Date: Tue, 5 Dec 2017 14:45:25 +0200 Subject: [PATCH] Refetch first page if totalItems has changed between pagination calls. --- src/containers/OrderPage/OrderPage.duck.js | 17 +++++++++++++++++ src/containers/SalePage/SalePage.duck.js | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/containers/OrderPage/OrderPage.duck.js b/src/containers/OrderPage/OrderPage.duck.js index 8b927114..b8684c99 100644 --- a/src/containers/OrderPage/OrderPage.duck.js +++ b/src/containers/OrderPage/OrderPage.duck.js @@ -205,8 +205,25 @@ const fetchMessages = (txId, page) => (dispatch, getState, sdk) => { const denormalizedMessages = denormalisedEntities(entities, 'message', messageIds); const { totalItems, totalPages, page: fetchedPage } = response.data.meta; const pagination = { totalItems, totalPages, page: fetchedPage }; + const totalMessages = getState().OrderPage.totalMessages; + // Original fetchMessages call succeeded dispatch(fetchMessagesSuccess(denormalizedMessages, pagination)); + + // Check if totalItems has changed between fetched pagination pages + // if totalItems has changed, fetch first page again to include new incoming messages. + // TODO if there're more than 100 incoming messages, + // this should loop through most recent pages instead of fetching just the first one. + if (totalItems > totalMessages && page > 1) { + dispatch(fetchMessages(txId, 1)) + .then(() => { + // Original fetch was enough as a response for user action, + // this just includes new incoming messages + }) + .catch(() => { + // Background update, no need to to do anything atm. + }); + } }) .catch(e => { dispatch(fetchMessagesError(storableError(e))); diff --git a/src/containers/SalePage/SalePage.duck.js b/src/containers/SalePage/SalePage.duck.js index f61bc650..5e43f19d 100644 --- a/src/containers/SalePage/SalePage.duck.js +++ b/src/containers/SalePage/SalePage.duck.js @@ -283,8 +283,25 @@ const fetchMessages = (txId, page) => (dispatch, getState, sdk) => { const denormalizedMessages = denormalisedEntities(entities, 'message', messageIds); const { totalItems, totalPages, page: fetchedPage } = response.data.meta; const pagination = { totalItems, totalPages, page: fetchedPage }; + const totalMessages = getState().OrderPage.totalMessages; + // Original fetchMessages call succeeded dispatch(fetchMessagesSuccess(denormalizedMessages, pagination)); + + // Check if totalItems has changed between fetched pagination pages + // if totalItems has changed, fetch first page again to include new incoming messages. + // TODO if there're more than 100 incoming messages, + // this should loop through most recent pages instead of fetching just the first one. + if (totalItems > totalMessages && page > 1) { + dispatch(fetchMessages(txId, 1)) + .then(() => { + // Original fetch was enough as a response for user action, + // this just includes new incoming messages + }) + .catch(() => { + // Background update, no need to to do anything atm. + }); + } }) .catch(e => { dispatch(fetchMessagesError(storableError(e)));