From 30c1827a47b5bfd93c5f85cb570d99fb53177259 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 17 Apr 2024 09:37:13 +0200 Subject: [PATCH] Send orphan visits type to server when listing --- CHANGELOG.md | 3 ++- src/visits/reducers/orphanVisits.ts | 6 +++++- test/visits/reducers/orphanVisits.test.ts | 19 ++++++++++++++----- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef6574dc..4b669a96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org). -## [Unreleased] +## [0.6.2] - 2024-04.17 ### Added * *Nothing* @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Fixed * Make sure project dependencies are not bundled with package. * [#244](https://github.com/shlinkio/shlink-web-component/issues/244) Display `visitedUrl` in visits table if the visit object has it, regardless of it being an orphan visit or not. +* [#327](https://github.com/shlinkio/shlink-web-component/issues/327) Ensure orphan visits type is sent to the server, to enable server-side filtering when consumed Shlink supports it. ## [0.6.1] - 2024-04-10 diff --git a/src/visits/reducers/orphanVisits.ts b/src/visits/reducers/orphanVisits.ts index 22c7e44b..a7ab6947 100644 --- a/src/visits/reducers/orphanVisits.ts +++ b/src/visits/reducers/orphanVisits.ts @@ -34,7 +34,11 @@ export const getOrphanVisits = (apiClientFactory: () => ShlinkApiClient) => crea const apiClient = apiClientFactory(); const { doIntervalFallback = false } = options; - const visitsLoader = async (query: ShlinkVisitsParams) => apiClient.getOrphanVisits(query).then( + const visitsLoader = async (query: ShlinkVisitsParams) => apiClient.getOrphanVisits({ + ...query, + type: orphanVisitsType, // Send type to the server, in case it supports filtering by type + }).then( + // We still try to filter locally, for Shlink older than 4.0.0 (resp) => filterOrphanVisitsByType(resp, orphanVisitsType), ); const lastVisitLoader = lastVisitLoaderForLoader(doIntervalFallback, (q) => apiClient.getOrphanVisits(q)); diff --git a/test/visits/reducers/orphanVisits.test.ts b/test/visits/reducers/orphanVisits.test.ts index 44a2178e..528a421f 100644 --- a/test/visits/reducers/orphanVisits.test.ts +++ b/test/visits/reducers/orphanVisits.test.ts @@ -204,24 +204,28 @@ describe('orphanVisitsReducer', () => { dateRange: { startDate: subDays(now, 1), endDate: addDays(now, 1) }, loadPrevInterval: true, expectsPrevVisits: true, + orphanVisitsType: 'base_url' as const, }, // Undefined date range and loadPrevInterval: true -> prev visits are NOT loaded { dateRange: undefined, loadPrevInterval: true, expectsPrevVisits: false, + orphanVisitsType: 'regular_404' as const, }, // Empty date range and loadPrevInterval: true -> prev visits are NOT loaded { dateRange: {}, loadPrevInterval: true, expectsPrevVisits: false, + orphanVisitsType: 'invalid_short_url' as const, }, // Start date only and loadPrevInterval: true -> prev visits are NOT loaded { dateRange: { startDate: subDays(now, 2) }, loadPrevInterval: true, expectsPrevVisits: true, + orphanVisitsType: 'invalid_short_url' as const, }, // End date only and loadPrevInterval: true -> prev visits are NOT loaded { @@ -236,18 +240,22 @@ describe('orphanVisitsReducer', () => { expectsPrevVisits: false, }, ])('returns visits from prev interval when requested and possible', async ( - { dateRange, loadPrevInterval, expectsPrevVisits }, + { dateRange, loadPrevInterval, expectsPrevVisits, orphanVisitsType }, ) => { const getVisitsParam: LoadOrphanVisits = { params: { dateRange }, options: { loadPrevInterval }, + orphanVisitsType, }; - const prevVisits = expectsPrevVisits ? visitsMocks.map( - ({ date, ...rest }, index) => ({ ...rest, date: dateForVisit(index + 1 + visitsMocks.length) }), + const expectedVisits = visitsMocks.map( + (visit) => (orphanVisitsType ? { ...visit, type: orphanVisitsType } : visit), + ); + const prevVisits = expectsPrevVisits ? expectedVisits.map( + ({ date, ...rest }, index) => ({ ...rest, date: dateForVisit(index + 1 + expectedVisits.length) }), ) : undefined; getOrphanVisitsCall.mockResolvedValue({ - data: visitsMocks, + data: expectedVisits, pagination: { currentPage: 1, pagesCount: 1, @@ -259,9 +267,10 @@ describe('orphanVisitsReducer', () => { expect(dispatchMock).toHaveBeenCalledTimes(2); expect(dispatchMock).toHaveBeenLastCalledWith(expect.objectContaining({ - payload: { visits: visitsMocks, prevVisits, ...getVisitsParam }, + payload: { visits: expectedVisits, prevVisits, ...getVisitsParam }, })); expect(getOrphanVisitsCall).toHaveBeenCalledTimes(expectsPrevVisits ? 2 : 1); + expect(getOrphanVisitsCall).toHaveBeenCalledWith(expect.objectContaining({ type: orphanVisitsType })); }); }); });