From a6ed0c811dbe7027a2d41e5c7a7c0207196715fa Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 12 Nov 2022 09:38:24 +0100 Subject: [PATCH] Updated getShortUrlVisits action so that it expects a signle DTO param --- src/visits/ShortUrlVisits.tsx | 12 +++++++----- src/visits/reducers/shortUrlVisits.ts | 10 ++++++---- test/visits/reducers/shortUrlVisits.test.ts | 11 +++++++---- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/visits/ShortUrlVisits.tsx b/src/visits/ShortUrlVisits.tsx index 5ef1b005..3e17ae46 100644 --- a/src/visits/ShortUrlVisits.tsx +++ b/src/visits/ShortUrlVisits.tsx @@ -1,13 +1,12 @@ import { useEffect } from 'react'; import { useLocation, useParams } from 'react-router-dom'; import { boundToMercureHub } from '../mercure/helpers/boundToMercureHub'; -import { ShlinkVisitsParams } from '../api/types'; import { parseQuery } from '../utils/helpers/query'; import { Topics } from '../mercure/helpers/Topics'; import { ShortUrlDetail } from '../short-urls/reducers/shortUrlDetail'; import { useGoBack } from '../utils/helpers/hooks'; import { ReportExporter } from '../common/services/ReportExporter'; -import { ShortUrlVisits as ShortUrlVisitsState } from './reducers/shortUrlVisits'; +import { LoadShortUrlVisits, ShortUrlVisits as ShortUrlVisitsState } from './reducers/shortUrlVisits'; import { ShortUrlVisitsHeader } from './ShortUrlVisitsHeader'; import { VisitsStats } from './VisitsStats'; import { NormalizedVisit, VisitsParams } from './types'; @@ -17,7 +16,7 @@ import { urlDecodeShortCode } from '../short-urls/helpers'; import { ShortUrlIdentifier } from '../short-urls/data'; export interface ShortUrlVisitsProps extends CommonVisitsProps { - getShortUrlVisits: (shortCode: string, query?: ShlinkVisitsParams, doIntervalFallback?: boolean) => void; + getShortUrlVisits: (params: LoadShortUrlVisits) => void; shortUrlVisits: ShortUrlVisitsState; getShortUrlDetail: (shortUrl: ShortUrlIdentifier) => void; shortUrlDetail: ShortUrlDetail; @@ -37,8 +36,11 @@ export const ShortUrlVisits = ({ exportVisits }: ReportExporter) => boundToMercu const { search } = useLocation(); const goBack = useGoBack(); const { domain } = parseQuery<{ domain?: string }>(search); - const loadVisits = (params: VisitsParams, doIntervalFallback?: boolean) => - getShortUrlVisits(urlDecodeShortCode(shortCode), { ...toApiParams(params), domain }, doIntervalFallback); + const loadVisits = (params: VisitsParams, doIntervalFallback?: boolean) => getShortUrlVisits({ + shortCode: urlDecodeShortCode(shortCode), + query: { ...toApiParams(params), domain }, + doIntervalFallback, + }); const exportCsv = (visits: NormalizedVisit[]) => exportVisits( `short-url_${shortUrlDetail.shortUrl?.shortUrl.replace(/https?:\/\//g, '')}_visits.csv`, visits, diff --git a/src/visits/reducers/shortUrlVisits.ts b/src/visits/reducers/shortUrlVisits.ts index 0c34a1f8..9c2a9e9c 100644 --- a/src/visits/reducers/shortUrlVisits.ts +++ b/src/visits/reducers/shortUrlVisits.ts @@ -11,7 +11,7 @@ import { ApiErrorAction } from '../../api/types/actions'; import { isBetween } from '../../utils/helpers/date'; import { getVisitsWithLoader, lastVisitLoaderForLoader } from './common'; import { createNewVisits, CreateVisitsAction } from './visitCreation'; -import { VisitsFallbackIntervalAction, VisitsInfo, VisitsLoadProgressChangedAction } from './types'; +import { LoadVisits, VisitsFallbackIntervalAction, VisitsInfo, VisitsLoadProgressChangedAction } from './types'; const REDUCER_PREFIX = 'shlink/shortUrlVisits'; export const GET_SHORT_URL_VISITS_START = `${REDUCER_PREFIX}/getShortUrlVisits/pending`; @@ -29,6 +29,10 @@ interface ShortUrlVisitsAction extends Action, ShortUrlIdentifier { query?: ShlinkVisitsParams; } +export interface LoadShortUrlVisits extends LoadVisits { + shortCode: string; +} + type ShortUrlVisitsCombinedAction = ShortUrlVisitsAction & VisitsLoadProgressChangedAction & VisitsFallbackIntervalAction @@ -80,9 +84,7 @@ export default buildReducer({ }, initialState); export const getShortUrlVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => ( - shortCode: string, - query: ShlinkVisitsParams = {}, - doIntervalFallback = false, + { shortCode, query = {}, doIntervalFallback = false }: LoadShortUrlVisits, ) => async (dispatch: Dispatch, getState: GetState) => { const { getShortUrlVisits: shlinkGetShortUrlVisits } = buildShlinkApiClient(getState); const visitsLoader = async (page: number, itemsPerPage: number) => shlinkGetShortUrlVisits( diff --git a/test/visits/reducers/shortUrlVisits.test.ts b/test/visits/reducers/shortUrlVisits.test.ts index e5c301c0..9f5d017b 100644 --- a/test/visits/reducers/shortUrlVisits.test.ts +++ b/test/visits/reducers/shortUrlVisits.test.ts @@ -167,7 +167,7 @@ describe('shortUrlVisitsReducer', () => { it('dispatches start and error when promise is rejected', async () => { const ShlinkApiClient = buildApiClientMock(Promise.reject({})); - await getShortUrlVisits(() => ShlinkApiClient)('abc123')(dispatchMock, getState); + await getShortUrlVisits(() => ShlinkApiClient)({ shortCode: 'abc123' })(dispatchMock, getState); expect(dispatchMock).toHaveBeenCalledTimes(2); expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_VISITS_START }); @@ -191,7 +191,7 @@ describe('shortUrlVisitsReducer', () => { }, })); - await getShortUrlVisits(() => ShlinkApiClient)(shortCode, query)(dispatchMock, getState); + await getShortUrlVisits(() => ShlinkApiClient)({ shortCode, query })(dispatchMock, getState); expect(dispatchMock).toHaveBeenCalledTimes(2); expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_VISITS_START }); @@ -214,7 +214,7 @@ describe('shortUrlVisitsReducer', () => { }, })); - await getShortUrlVisits(() => ShlinkApiClient)('abc123')(dispatchMock, getState); + await getShortUrlVisits(() => ShlinkApiClient)({ shortCode: 'abc123' })(dispatchMock, getState); expect(ShlinkApiClient.getShortUrlVisits).toHaveBeenCalledTimes(expectedRequests); expect(dispatchMock).toHaveBeenNthCalledWith(3, expect.objectContaining({ @@ -246,7 +246,10 @@ describe('shortUrlVisitsReducer', () => { .mockResolvedValueOnce(buildVisitsResult(lastVisits)); const ShlinkApiClient = Mock.of({ getShortUrlVisits: getShlinkShortUrlVisits }); - await getShortUrlVisits(() => ShlinkApiClient)('abc123', {}, true)(dispatchMock, getState); + await getShortUrlVisits(() => ShlinkApiClient)({ shortCode: 'abc123', doIntervalFallback: true })( + dispatchMock, + getState, + ); expect(dispatchMock).toHaveBeenCalledTimes(2); expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_VISITS_START });