From a13654355114c00b9e4c1bea6a2283e1cee575df Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 19 Sep 2020 10:50:49 +0200 Subject: [PATCH] Fixed tags visits loading not being cancelled when the visits component gets unmounted --- src/visits/reducers/common.ts | 7 ++----- src/visits/reducers/shortUrlVisits.ts | 3 ++- src/visits/reducers/tagVisits.ts | 3 ++- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/visits/reducers/common.ts b/src/visits/reducers/common.ts index 4270d12e..f7e0afbd 100644 --- a/src/visits/reducers/common.ts +++ b/src/visits/reducers/common.ts @@ -1,7 +1,6 @@ import { flatten, prop, range, splitEvery } from 'ramda'; import { Action, Dispatch } from 'redux'; import { ShlinkPaginator, ShlinkVisits } from '../../utils/services/types'; -import { GetState } from '../../container/types'; import { Visit } from '../types'; const ITEMS_PER_PAGE = 5000; @@ -25,7 +24,7 @@ export const getVisitsWithLoader = async & { visits: V extraFinishActionData: Partial, actionMap: ActionMap, dispatch: Dispatch, - getState: GetState, + shouldCancel: () => boolean, ) => { dispatch({ type: actionMap.start }); @@ -33,9 +32,7 @@ export const getVisitsWithLoader = async & { visits: V Promise.all(pages.map(async (page) => visitsLoader(page, ITEMS_PER_PAGE).then(prop('data')))).then(flatten); const loadPagesBlocks = async (pagesBlocks: number[][], index = 0): Promise => { - const { shortUrlVisits: { cancelLoad } } = getState(); - - if (cancelLoad) { + if (shouldCancel()) { return []; } diff --git a/src/visits/reducers/shortUrlVisits.ts b/src/visits/reducers/shortUrlVisits.ts index a7ee4407..4cc689b2 100644 --- a/src/visits/reducers/shortUrlVisits.ts +++ b/src/visits/reducers/shortUrlVisits.ts @@ -69,6 +69,7 @@ export const getShortUrlVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) shortCode, { ...query, page, itemsPerPage }, ); + const shouldCancel = () => getState().shortUrlVisits.cancelLoad; const extraFinishActionData: Partial = { shortCode, domain: query.domain }; const actionMap = { start: GET_SHORT_URL_VISITS_START, @@ -78,7 +79,7 @@ export const getShortUrlVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) progress: GET_SHORT_URL_VISITS_PROGRESS_CHANGED, }; - return getVisitsWithLoader(visitsLoader, extraFinishActionData, actionMap, dispatch, getState); + return getVisitsWithLoader(visitsLoader, extraFinishActionData, actionMap, dispatch, shouldCancel); }; export const cancelGetShortUrlVisits = buildActionCreator(GET_SHORT_URL_VISITS_CANCEL); diff --git a/src/visits/reducers/tagVisits.ts b/src/visits/reducers/tagVisits.ts index 04fcc498..691c8b14 100644 --- a/src/visits/reducers/tagVisits.ts +++ b/src/visits/reducers/tagVisits.ts @@ -60,6 +60,7 @@ export const getTagVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => (t tag, { ...query, page, itemsPerPage }, ); + const shouldCancel = () => getState().tagVisits.cancelLoad; const extraFinishActionData: Partial = { tag }; const actionMap = { start: GET_TAG_VISITS_START, @@ -69,7 +70,7 @@ export const getTagVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => (t progress: GET_TAG_VISITS_PROGRESS_CHANGED, }; - return getVisitsWithLoader(visitsLoader, extraFinishActionData, actionMap, dispatch, getState); + return getVisitsWithLoader(visitsLoader, extraFinishActionData, actionMap, dispatch, shouldCancel); }; export const cancelGetTagVisits = buildActionCreator(GET_TAG_VISITS_CANCEL);