mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 09:30:31 +03:00
Added logic in reducers to fallback to a different date interval if default one returns no visits
This commit is contained in:
parent
a30687e4ea
commit
e22856ff74
7 changed files with 193 additions and 21 deletions
|
@ -1,9 +1,10 @@
|
||||||
import { flatten, prop, range, splitEvery } from 'ramda';
|
import { flatten, prop, range, splitEvery } from 'ramda';
|
||||||
import { Action, Dispatch } from 'redux';
|
import { Action, Dispatch } from 'redux';
|
||||||
import { ShlinkPaginator, ShlinkVisits } from '../../api/types';
|
import { ShlinkPaginator, ShlinkVisits, ShlinkVisitsParams } from '../../api/types';
|
||||||
import { Visit } from '../types';
|
import { Visit } from '../types';
|
||||||
import { parseApiError } from '../../api/utils';
|
import { parseApiError } from '../../api/utils';
|
||||||
import { ApiErrorAction } from '../../api/types/actions';
|
import { ApiErrorAction } from '../../api/types/actions';
|
||||||
|
import { dateToMatchingInterval } from '../../utils/dates/types';
|
||||||
|
|
||||||
const ITEMS_PER_PAGE = 5000;
|
const ITEMS_PER_PAGE = 5000;
|
||||||
const PARALLEL_REQUESTS_COUNT = 4;
|
const PARALLEL_REQUESTS_COUNT = 4;
|
||||||
|
@ -13,16 +14,19 @@ const isLastPage = ({ currentPage, pagesCount }: ShlinkPaginator): boolean => cu
|
||||||
const calcProgress = (total: number, current: number): number => current * 100 / total;
|
const calcProgress = (total: number, current: number): number => current * 100 / total;
|
||||||
|
|
||||||
type VisitsLoader = (page: number, itemsPerPage: number) => Promise<ShlinkVisits>;
|
type VisitsLoader = (page: number, itemsPerPage: number) => Promise<ShlinkVisits>;
|
||||||
|
type LastVisitLoader = () => Promise<Visit | undefined>;
|
||||||
interface ActionMap {
|
interface ActionMap {
|
||||||
start: string;
|
start: string;
|
||||||
large: string;
|
large: string;
|
||||||
finish: string;
|
finish: string;
|
||||||
error: string;
|
error: string;
|
||||||
progress: string;
|
progress: string;
|
||||||
|
fallbackToInterval: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getVisitsWithLoader = async <T extends Action<string> & { visits: Visit[] }>(
|
export const getVisitsWithLoader = async <T extends Action<string> & { visits: Visit[] }>(
|
||||||
visitsLoader: VisitsLoader,
|
visitsLoader: VisitsLoader,
|
||||||
|
lastVisitLoader: LastVisitLoader,
|
||||||
extraFinishActionData: Partial<T>,
|
extraFinishActionData: Partial<T>,
|
||||||
actionMap: ActionMap,
|
actionMap: ActionMap,
|
||||||
dispatch: Dispatch,
|
dispatch: Dispatch,
|
||||||
|
@ -69,10 +73,25 @@ export const getVisitsWithLoader = async <T extends Action<string> & { visits: V
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const visits = await loadVisits();
|
const [ visits, lastVisit ] = await Promise.all([ loadVisits(), lastVisitLoader() ]);
|
||||||
|
|
||||||
dispatch({ ...extraFinishActionData, visits, type: actionMap.finish });
|
dispatch(
|
||||||
|
!visits.length && lastVisit
|
||||||
|
? { type: actionMap.fallbackToInterval, fallbackInterval: dateToMatchingInterval(lastVisit.date) }
|
||||||
|
: { ...extraFinishActionData, visits, type: actionMap.finish },
|
||||||
|
);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
dispatch<ApiErrorAction>({ type: actionMap.error, errorData: parseApiError(e) });
|
dispatch<ApiErrorAction>({ type: actionMap.error, errorData: parseApiError(e) });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const lastVisitLoaderForLoader = (
|
||||||
|
doFallbackRange: boolean,
|
||||||
|
loader: (params: ShlinkVisitsParams) => Promise<ShlinkVisits>,
|
||||||
|
): LastVisitLoader => {
|
||||||
|
if (!doFallbackRange) {
|
||||||
|
return async () => Promise.resolve(undefined);
|
||||||
|
}
|
||||||
|
|
||||||
|
return async () => loader({ page: 1, itemsPerPage: 1 }).then((result) => result.data[0]);
|
||||||
|
};
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
import { Action, Dispatch } from 'redux';
|
import { Action, Dispatch } from 'redux';
|
||||||
import { OrphanVisit, OrphanVisitType, Visit, VisitsInfo, VisitsLoadProgressChangedAction } from '../types';
|
import {
|
||||||
|
OrphanVisit,
|
||||||
|
OrphanVisitType,
|
||||||
|
Visit,
|
||||||
|
VisitsFallbackIntervalAction,
|
||||||
|
VisitsInfo,
|
||||||
|
VisitsLoadProgressChangedAction,
|
||||||
|
} from '../types';
|
||||||
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
|
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
|
||||||
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
||||||
import { GetState } from '../../container/types';
|
import { GetState } from '../../container/types';
|
||||||
|
@ -7,7 +14,7 @@ import { ShlinkVisitsParams } from '../../api/types';
|
||||||
import { isOrphanVisit } from '../types/helpers';
|
import { isOrphanVisit } from '../types/helpers';
|
||||||
import { ApiErrorAction } from '../../api/types/actions';
|
import { ApiErrorAction } from '../../api/types/actions';
|
||||||
import { isBetween } from '../../utils/helpers/date';
|
import { isBetween } from '../../utils/helpers/date';
|
||||||
import { getVisitsWithLoader } from './common';
|
import { getVisitsWithLoader, lastVisitLoaderForLoader } from './common';
|
||||||
import { CREATE_VISITS, CreateVisitsAction } from './visitCreation';
|
import { CREATE_VISITS, CreateVisitsAction } from './visitCreation';
|
||||||
|
|
||||||
/* eslint-disable padding-line-between-statements */
|
/* eslint-disable padding-line-between-statements */
|
||||||
|
@ -17,6 +24,7 @@ export const GET_ORPHAN_VISITS = 'shlink/orphanVisits/GET_ORPHAN_VISITS';
|
||||||
export const GET_ORPHAN_VISITS_LARGE = 'shlink/orphanVisits/GET_ORPHAN_VISITS_LARGE';
|
export const GET_ORPHAN_VISITS_LARGE = 'shlink/orphanVisits/GET_ORPHAN_VISITS_LARGE';
|
||||||
export const GET_ORPHAN_VISITS_CANCEL = 'shlink/orphanVisits/GET_ORPHAN_VISITS_CANCEL';
|
export const GET_ORPHAN_VISITS_CANCEL = 'shlink/orphanVisits/GET_ORPHAN_VISITS_CANCEL';
|
||||||
export const GET_ORPHAN_VISITS_PROGRESS_CHANGED = 'shlink/orphanVisits/GET_ORPHAN_VISITS_PROGRESS_CHANGED';
|
export const GET_ORPHAN_VISITS_PROGRESS_CHANGED = 'shlink/orphanVisits/GET_ORPHAN_VISITS_PROGRESS_CHANGED';
|
||||||
|
export const GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL = 'shlink/orphanVisits/GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL';
|
||||||
/* eslint-enable padding-line-between-statements */
|
/* eslint-enable padding-line-between-statements */
|
||||||
|
|
||||||
export interface OrphanVisitsAction extends Action<string> {
|
export interface OrphanVisitsAction extends Action<string> {
|
||||||
|
@ -26,6 +34,7 @@ export interface OrphanVisitsAction extends Action<string> {
|
||||||
|
|
||||||
type OrphanVisitsCombinedAction = OrphanVisitsAction
|
type OrphanVisitsCombinedAction = OrphanVisitsAction
|
||||||
& VisitsLoadProgressChangedAction
|
& VisitsLoadProgressChangedAction
|
||||||
|
& VisitsFallbackIntervalAction
|
||||||
& CreateVisitsAction
|
& CreateVisitsAction
|
||||||
& ApiErrorAction;
|
& ApiErrorAction;
|
||||||
|
|
||||||
|
@ -41,10 +50,11 @@ const initialState: VisitsInfo = {
|
||||||
export default buildReducer<VisitsInfo, OrphanVisitsCombinedAction>({
|
export default buildReducer<VisitsInfo, OrphanVisitsCombinedAction>({
|
||||||
[GET_ORPHAN_VISITS_START]: () => ({ ...initialState, loading: true }),
|
[GET_ORPHAN_VISITS_START]: () => ({ ...initialState, loading: true }),
|
||||||
[GET_ORPHAN_VISITS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }),
|
[GET_ORPHAN_VISITS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }),
|
||||||
[GET_ORPHAN_VISITS]: (_, { visits, query }) => ({ ...initialState, visits, query }),
|
[GET_ORPHAN_VISITS]: (state, { visits, query }) => ({ ...state, visits, query, loading: false, error: false }),
|
||||||
[GET_ORPHAN_VISITS_LARGE]: (state) => ({ ...state, loadingLarge: true }),
|
[GET_ORPHAN_VISITS_LARGE]: (state) => ({ ...state, loadingLarge: true }),
|
||||||
[GET_ORPHAN_VISITS_CANCEL]: (state) => ({ ...state, cancelLoad: true }),
|
[GET_ORPHAN_VISITS_CANCEL]: (state) => ({ ...state, cancelLoad: true }),
|
||||||
[GET_ORPHAN_VISITS_PROGRESS_CHANGED]: (state, { progress }) => ({ ...state, progress }),
|
[GET_ORPHAN_VISITS_PROGRESS_CHANGED]: (state, { progress }) => ({ ...state, progress }),
|
||||||
|
[GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL]: (state, { fallbackInterval }) => ({ ...state, fallbackInterval }),
|
||||||
[CREATE_VISITS]: (state, { createdVisits }) => {
|
[CREATE_VISITS]: (state, { createdVisits }) => {
|
||||||
const { visits, query = {} } = state;
|
const { visits, query = {} } = state;
|
||||||
const { startDate, endDate } = query;
|
const { startDate, endDate } = query;
|
||||||
|
@ -62,6 +72,7 @@ const matchesType = (visit: OrphanVisit, orphanVisitsType?: OrphanVisitType) =>
|
||||||
export const getOrphanVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
export const getOrphanVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
||||||
query: ShlinkVisitsParams = {},
|
query: ShlinkVisitsParams = {},
|
||||||
orphanVisitsType?: OrphanVisitType,
|
orphanVisitsType?: OrphanVisitType,
|
||||||
|
doFallbackRange = false,
|
||||||
) => async (dispatch: Dispatch, getState: GetState) => {
|
) => async (dispatch: Dispatch, getState: GetState) => {
|
||||||
const { getOrphanVisits } = buildShlinkApiClient(getState);
|
const { getOrphanVisits } = buildShlinkApiClient(getState);
|
||||||
const visitsLoader = async (page: number, itemsPerPage: number) => getOrphanVisits({ ...query, page, itemsPerPage })
|
const visitsLoader = async (page: number, itemsPerPage: number) => getOrphanVisits({ ...query, page, itemsPerPage })
|
||||||
|
@ -70,6 +81,7 @@ export const getOrphanVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) =>
|
||||||
|
|
||||||
return { ...result, data: visits };
|
return { ...result, data: visits };
|
||||||
});
|
});
|
||||||
|
const lastVisitLoader = lastVisitLoaderForLoader(doFallbackRange, getOrphanVisits);
|
||||||
const shouldCancel = () => getState().orphanVisits.cancelLoad;
|
const shouldCancel = () => getState().orphanVisits.cancelLoad;
|
||||||
const extraFinishActionData: Partial<OrphanVisitsAction> = { query };
|
const extraFinishActionData: Partial<OrphanVisitsAction> = { query };
|
||||||
const actionMap = {
|
const actionMap = {
|
||||||
|
@ -78,9 +90,10 @@ export const getOrphanVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) =>
|
||||||
finish: GET_ORPHAN_VISITS,
|
finish: GET_ORPHAN_VISITS,
|
||||||
error: GET_ORPHAN_VISITS_ERROR,
|
error: GET_ORPHAN_VISITS_ERROR,
|
||||||
progress: GET_ORPHAN_VISITS_PROGRESS_CHANGED,
|
progress: GET_ORPHAN_VISITS_PROGRESS_CHANGED,
|
||||||
|
fallbackToInterval: GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL,
|
||||||
};
|
};
|
||||||
|
|
||||||
return getVisitsWithLoader(visitsLoader, extraFinishActionData, actionMap, dispatch, shouldCancel);
|
return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, actionMap, dispatch, shouldCancel);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const cancelGetOrphanVisits = buildActionCreator(GET_ORPHAN_VISITS_CANCEL);
|
export const cancelGetOrphanVisits = buildActionCreator(GET_ORPHAN_VISITS_CANCEL);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { Action, Dispatch } from 'redux';
|
import { Action, Dispatch } from 'redux';
|
||||||
import { shortUrlMatches } from '../../short-urls/helpers';
|
import { shortUrlMatches } from '../../short-urls/helpers';
|
||||||
import { Visit, VisitsInfo, VisitsLoadProgressChangedAction } from '../types';
|
import { Visit, VisitsFallbackIntervalAction, VisitsInfo, VisitsLoadProgressChangedAction } from '../types';
|
||||||
import { ShortUrlIdentifier } from '../../short-urls/data';
|
import { ShortUrlIdentifier } from '../../short-urls/data';
|
||||||
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
|
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
|
||||||
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
||||||
|
@ -8,7 +8,7 @@ import { GetState } from '../../container/types';
|
||||||
import { ShlinkVisitsParams } from '../../api/types';
|
import { ShlinkVisitsParams } from '../../api/types';
|
||||||
import { ApiErrorAction } from '../../api/types/actions';
|
import { ApiErrorAction } from '../../api/types/actions';
|
||||||
import { isBetween } from '../../utils/helpers/date';
|
import { isBetween } from '../../utils/helpers/date';
|
||||||
import { getVisitsWithLoader } from './common';
|
import { getVisitsWithLoader, lastVisitLoaderForLoader } from './common';
|
||||||
import { CREATE_VISITS, CreateVisitsAction } from './visitCreation';
|
import { CREATE_VISITS, CreateVisitsAction } from './visitCreation';
|
||||||
|
|
||||||
/* eslint-disable padding-line-between-statements */
|
/* eslint-disable padding-line-between-statements */
|
||||||
|
@ -18,6 +18,7 @@ export const GET_SHORT_URL_VISITS = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS'
|
||||||
export const GET_SHORT_URL_VISITS_LARGE = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_LARGE';
|
export const GET_SHORT_URL_VISITS_LARGE = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_LARGE';
|
||||||
export const GET_SHORT_URL_VISITS_CANCEL = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_CANCEL';
|
export const GET_SHORT_URL_VISITS_CANCEL = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_CANCEL';
|
||||||
export const GET_SHORT_URL_VISITS_PROGRESS_CHANGED = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_PROGRESS_CHANGED';
|
export const GET_SHORT_URL_VISITS_PROGRESS_CHANGED = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_PROGRESS_CHANGED';
|
||||||
|
export const GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL';
|
||||||
/* eslint-enable padding-line-between-statements */
|
/* eslint-enable padding-line-between-statements */
|
||||||
|
|
||||||
export interface ShortUrlVisits extends VisitsInfo, ShortUrlIdentifier {}
|
export interface ShortUrlVisits extends VisitsInfo, ShortUrlIdentifier {}
|
||||||
|
@ -29,6 +30,7 @@ interface ShortUrlVisitsAction extends Action<string>, ShortUrlIdentifier {
|
||||||
|
|
||||||
type ShortUrlVisitsCombinedAction = ShortUrlVisitsAction
|
type ShortUrlVisitsCombinedAction = ShortUrlVisitsAction
|
||||||
& VisitsLoadProgressChangedAction
|
& VisitsLoadProgressChangedAction
|
||||||
|
& VisitsFallbackIntervalAction
|
||||||
& CreateVisitsAction
|
& CreateVisitsAction
|
||||||
& ApiErrorAction;
|
& ApiErrorAction;
|
||||||
|
|
||||||
|
@ -46,16 +48,19 @@ const initialState: ShortUrlVisits = {
|
||||||
export default buildReducer<ShortUrlVisits, ShortUrlVisitsCombinedAction>({
|
export default buildReducer<ShortUrlVisits, ShortUrlVisitsCombinedAction>({
|
||||||
[GET_SHORT_URL_VISITS_START]: () => ({ ...initialState, loading: true }),
|
[GET_SHORT_URL_VISITS_START]: () => ({ ...initialState, loading: true }),
|
||||||
[GET_SHORT_URL_VISITS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }),
|
[GET_SHORT_URL_VISITS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }),
|
||||||
[GET_SHORT_URL_VISITS]: (_, { visits, query, shortCode, domain }) => ({
|
[GET_SHORT_URL_VISITS]: (state, { visits, query, shortCode, domain }) => ({
|
||||||
...initialState,
|
...state,
|
||||||
visits,
|
visits,
|
||||||
shortCode,
|
shortCode,
|
||||||
domain,
|
domain,
|
||||||
query,
|
query,
|
||||||
|
loading: false,
|
||||||
|
error: false,
|
||||||
}),
|
}),
|
||||||
[GET_SHORT_URL_VISITS_LARGE]: (state) => ({ ...state, loadingLarge: true }),
|
[GET_SHORT_URL_VISITS_LARGE]: (state) => ({ ...state, loadingLarge: true }),
|
||||||
[GET_SHORT_URL_VISITS_CANCEL]: (state) => ({ ...state, cancelLoad: true }),
|
[GET_SHORT_URL_VISITS_CANCEL]: (state) => ({ ...state, cancelLoad: true }),
|
||||||
[GET_SHORT_URL_VISITS_PROGRESS_CHANGED]: (state, { progress }) => ({ ...state, progress }),
|
[GET_SHORT_URL_VISITS_PROGRESS_CHANGED]: (state, { progress }) => ({ ...state, progress }),
|
||||||
|
[GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL]: (state, { fallbackInterval }) => ({ ...state, fallbackInterval }),
|
||||||
[CREATE_VISITS]: (state, { createdVisits }) => {
|
[CREATE_VISITS]: (state, { createdVisits }) => {
|
||||||
const { shortCode, domain, visits, query = {} } = state;
|
const { shortCode, domain, visits, query = {} } = state;
|
||||||
const { startDate, endDate } = query;
|
const { startDate, endDate } = query;
|
||||||
|
@ -73,12 +78,17 @@ export default buildReducer<ShortUrlVisits, ShortUrlVisitsCombinedAction>({
|
||||||
export const getShortUrlVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
export const getShortUrlVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
||||||
shortCode: string,
|
shortCode: string,
|
||||||
query: ShlinkVisitsParams = {},
|
query: ShlinkVisitsParams = {},
|
||||||
|
doFallbackRange = false,
|
||||||
) => async (dispatch: Dispatch, getState: GetState) => {
|
) => async (dispatch: Dispatch, getState: GetState) => {
|
||||||
const { getShortUrlVisits } = buildShlinkApiClient(getState);
|
const { getShortUrlVisits } = buildShlinkApiClient(getState);
|
||||||
const visitsLoader = async (page: number, itemsPerPage: number) => getShortUrlVisits(
|
const visitsLoader = async (page: number, itemsPerPage: number) => getShortUrlVisits(
|
||||||
shortCode,
|
shortCode,
|
||||||
{ ...query, page, itemsPerPage },
|
{ ...query, page, itemsPerPage },
|
||||||
);
|
);
|
||||||
|
const lastVisitLoader = lastVisitLoaderForLoader(
|
||||||
|
doFallbackRange,
|
||||||
|
async (params) => getShortUrlVisits(shortCode, { ...params, domain: query.domain }),
|
||||||
|
);
|
||||||
const shouldCancel = () => getState().shortUrlVisits.cancelLoad;
|
const shouldCancel = () => getState().shortUrlVisits.cancelLoad;
|
||||||
const extraFinishActionData: Partial<ShortUrlVisitsAction> = { shortCode, query, domain: query.domain };
|
const extraFinishActionData: Partial<ShortUrlVisitsAction> = { shortCode, query, domain: query.domain };
|
||||||
const actionMap = {
|
const actionMap = {
|
||||||
|
@ -87,9 +97,10 @@ export const getShortUrlVisits = (buildShlinkApiClient: ShlinkApiClientBuilder)
|
||||||
finish: GET_SHORT_URL_VISITS,
|
finish: GET_SHORT_URL_VISITS,
|
||||||
error: GET_SHORT_URL_VISITS_ERROR,
|
error: GET_SHORT_URL_VISITS_ERROR,
|
||||||
progress: GET_SHORT_URL_VISITS_PROGRESS_CHANGED,
|
progress: GET_SHORT_URL_VISITS_PROGRESS_CHANGED,
|
||||||
|
fallbackToInterval: GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL,
|
||||||
};
|
};
|
||||||
|
|
||||||
return getVisitsWithLoader(visitsLoader, extraFinishActionData, actionMap, dispatch, shouldCancel);
|
return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, actionMap, dispatch, shouldCancel);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const cancelGetShortUrlVisits = buildActionCreator(GET_SHORT_URL_VISITS_CANCEL);
|
export const cancelGetShortUrlVisits = buildActionCreator(GET_SHORT_URL_VISITS_CANCEL);
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import { Action, Dispatch } from 'redux';
|
import { Action, Dispatch } from 'redux';
|
||||||
import { Visit, VisitsInfo, VisitsLoadProgressChangedAction } from '../types';
|
import { Visit, VisitsFallbackIntervalAction, VisitsInfo, VisitsLoadProgressChangedAction } from '../types';
|
||||||
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
|
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
|
||||||
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
||||||
import { GetState } from '../../container/types';
|
import { GetState } from '../../container/types';
|
||||||
import { ShlinkVisitsParams } from '../../api/types';
|
import { ShlinkVisitsParams } from '../../api/types';
|
||||||
import { ApiErrorAction } from '../../api/types/actions';
|
import { ApiErrorAction } from '../../api/types/actions';
|
||||||
import { isBetween } from '../../utils/helpers/date';
|
import { isBetween } from '../../utils/helpers/date';
|
||||||
import { getVisitsWithLoader } from './common';
|
import { getVisitsWithLoader, lastVisitLoaderForLoader } from './common';
|
||||||
import { CREATE_VISITS, CreateVisitsAction } from './visitCreation';
|
import { CREATE_VISITS, CreateVisitsAction } from './visitCreation';
|
||||||
|
|
||||||
/* eslint-disable padding-line-between-statements */
|
/* eslint-disable padding-line-between-statements */
|
||||||
|
@ -16,6 +16,7 @@ export const GET_TAG_VISITS = 'shlink/tagVisits/GET_TAG_VISITS';
|
||||||
export const GET_TAG_VISITS_LARGE = 'shlink/tagVisits/GET_TAG_VISITS_LARGE';
|
export const GET_TAG_VISITS_LARGE = 'shlink/tagVisits/GET_TAG_VISITS_LARGE';
|
||||||
export const GET_TAG_VISITS_CANCEL = 'shlink/tagVisits/GET_TAG_VISITS_CANCEL';
|
export const GET_TAG_VISITS_CANCEL = 'shlink/tagVisits/GET_TAG_VISITS_CANCEL';
|
||||||
export const GET_TAG_VISITS_PROGRESS_CHANGED = 'shlink/tagVisits/GET_TAG_VISITS_PROGRESS_CHANGED';
|
export const GET_TAG_VISITS_PROGRESS_CHANGED = 'shlink/tagVisits/GET_TAG_VISITS_PROGRESS_CHANGED';
|
||||||
|
export const GET_TAG_VISITS_FALLBACK_TO_INTERVAL = 'shlink/tagVisits/GET_TAG_VISITS_FALLBACK_TO_INTERVAL';
|
||||||
/* eslint-enable padding-line-between-statements */
|
/* eslint-enable padding-line-between-statements */
|
||||||
|
|
||||||
export interface TagVisits extends VisitsInfo {
|
export interface TagVisits extends VisitsInfo {
|
||||||
|
@ -30,6 +31,7 @@ export interface TagVisitsAction extends Action<string> {
|
||||||
|
|
||||||
type TagsVisitsCombinedAction = TagVisitsAction
|
type TagsVisitsCombinedAction = TagVisitsAction
|
||||||
& VisitsLoadProgressChangedAction
|
& VisitsLoadProgressChangedAction
|
||||||
|
& VisitsFallbackIntervalAction
|
||||||
& CreateVisitsAction
|
& CreateVisitsAction
|
||||||
& ApiErrorAction;
|
& ApiErrorAction;
|
||||||
|
|
||||||
|
@ -46,10 +48,11 @@ const initialState: TagVisits = {
|
||||||
export default buildReducer<TagVisits, TagsVisitsCombinedAction>({
|
export default buildReducer<TagVisits, TagsVisitsCombinedAction>({
|
||||||
[GET_TAG_VISITS_START]: () => ({ ...initialState, loading: true }),
|
[GET_TAG_VISITS_START]: () => ({ ...initialState, loading: true }),
|
||||||
[GET_TAG_VISITS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }),
|
[GET_TAG_VISITS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }),
|
||||||
[GET_TAG_VISITS]: (_, { visits, tag, query }) => ({ ...initialState, visits, tag, query }),
|
[GET_TAG_VISITS]: (state, { visits, tag, query }) => ({ ...state, visits, tag, query, loading: false, error: false }),
|
||||||
[GET_TAG_VISITS_LARGE]: (state) => ({ ...state, loadingLarge: true }),
|
[GET_TAG_VISITS_LARGE]: (state) => ({ ...state, loadingLarge: true }),
|
||||||
[GET_TAG_VISITS_CANCEL]: (state) => ({ ...state, cancelLoad: true }),
|
[GET_TAG_VISITS_CANCEL]: (state) => ({ ...state, cancelLoad: true }),
|
||||||
[GET_TAG_VISITS_PROGRESS_CHANGED]: (state, { progress }) => ({ ...state, progress }),
|
[GET_TAG_VISITS_PROGRESS_CHANGED]: (state, { progress }) => ({ ...state, progress }),
|
||||||
|
[GET_TAG_VISITS_FALLBACK_TO_INTERVAL]: (state, { fallbackInterval }) => ({ ...state, fallbackInterval }),
|
||||||
[CREATE_VISITS]: (state, { createdVisits }) => {
|
[CREATE_VISITS]: (state, { createdVisits }) => {
|
||||||
const { tag, visits, query = {} } = state;
|
const { tag, visits, query = {} } = state;
|
||||||
const { startDate, endDate } = query;
|
const { startDate, endDate } = query;
|
||||||
|
@ -64,12 +67,14 @@ export default buildReducer<TagVisits, TagsVisitsCombinedAction>({
|
||||||
export const getTagVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
export const getTagVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
||||||
tag: string,
|
tag: string,
|
||||||
query: ShlinkVisitsParams = {},
|
query: ShlinkVisitsParams = {},
|
||||||
|
doFallbackRange = false,
|
||||||
) => async (dispatch: Dispatch, getState: GetState) => {
|
) => async (dispatch: Dispatch, getState: GetState) => {
|
||||||
const { getTagVisits } = buildShlinkApiClient(getState);
|
const { getTagVisits } = buildShlinkApiClient(getState);
|
||||||
const visitsLoader = async (page: number, itemsPerPage: number) => getTagVisits(
|
const visitsLoader = async (page: number, itemsPerPage: number) => getTagVisits(
|
||||||
tag,
|
tag,
|
||||||
{ ...query, page, itemsPerPage },
|
{ ...query, page, itemsPerPage },
|
||||||
);
|
);
|
||||||
|
const lastVisitLoader = lastVisitLoaderForLoader(doFallbackRange, async (params) => getTagVisits(tag, params));
|
||||||
const shouldCancel = () => getState().tagVisits.cancelLoad;
|
const shouldCancel = () => getState().tagVisits.cancelLoad;
|
||||||
const extraFinishActionData: Partial<TagVisitsAction> = { tag, query };
|
const extraFinishActionData: Partial<TagVisitsAction> = { tag, query };
|
||||||
const actionMap = {
|
const actionMap = {
|
||||||
|
@ -78,9 +83,10 @@ export const getTagVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
||||||
finish: GET_TAG_VISITS,
|
finish: GET_TAG_VISITS,
|
||||||
error: GET_TAG_VISITS_ERROR,
|
error: GET_TAG_VISITS_ERROR,
|
||||||
progress: GET_TAG_VISITS_PROGRESS_CHANGED,
|
progress: GET_TAG_VISITS_PROGRESS_CHANGED,
|
||||||
|
fallbackToInterval: GET_TAG_VISITS_FALLBACK_TO_INTERVAL,
|
||||||
};
|
};
|
||||||
|
|
||||||
return getVisitsWithLoader(visitsLoader, extraFinishActionData, actionMap, dispatch, shouldCancel);
|
return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, actionMap, dispatch, shouldCancel);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const cancelGetTagVisits = buildActionCreator(GET_TAG_VISITS_CANCEL);
|
export const cancelGetTagVisits = buildActionCreator(GET_TAG_VISITS_CANCEL);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Mock } from 'ts-mockery';
|
import { Mock } from 'ts-mockery';
|
||||||
import { addDays, subDays } from 'date-fns';
|
import { addDays, formatISO, subDays } from 'date-fns';
|
||||||
import reducer, {
|
import reducer, {
|
||||||
getOrphanVisits,
|
getOrphanVisits,
|
||||||
cancelGetOrphanVisits,
|
cancelGetOrphanVisits,
|
||||||
|
@ -9,6 +9,7 @@ import reducer, {
|
||||||
GET_ORPHAN_VISITS_LARGE,
|
GET_ORPHAN_VISITS_LARGE,
|
||||||
GET_ORPHAN_VISITS_CANCEL,
|
GET_ORPHAN_VISITS_CANCEL,
|
||||||
GET_ORPHAN_VISITS_PROGRESS_CHANGED,
|
GET_ORPHAN_VISITS_PROGRESS_CHANGED,
|
||||||
|
GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL,
|
||||||
} from '../../../src/visits/reducers/orphanVisits';
|
} from '../../../src/visits/reducers/orphanVisits';
|
||||||
import { CREATE_VISITS } from '../../../src/visits/reducers/visitCreation';
|
import { CREATE_VISITS } from '../../../src/visits/reducers/visitCreation';
|
||||||
import { rangeOf } from '../../../src/utils/utils';
|
import { rangeOf } from '../../../src/utils/utils';
|
||||||
|
@ -17,6 +18,7 @@ import { ShlinkVisits } from '../../../src/api/types';
|
||||||
import ShlinkApiClient from '../../../src/api/services/ShlinkApiClient';
|
import ShlinkApiClient from '../../../src/api/services/ShlinkApiClient';
|
||||||
import { ShlinkState } from '../../../src/container/types';
|
import { ShlinkState } from '../../../src/container/types';
|
||||||
import { formatIsoDate } from '../../../src/utils/helpers/date';
|
import { formatIsoDate } from '../../../src/utils/helpers/date';
|
||||||
|
import { DateInterval } from '../../../src/utils/dates/types';
|
||||||
|
|
||||||
describe('orphanVisitsReducer', () => {
|
describe('orphanVisitsReducer', () => {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
@ -116,6 +118,13 @@ describe('orphanVisitsReducer', () => {
|
||||||
|
|
||||||
expect(state).toEqual(expect.objectContaining({ progress: 85 }));
|
expect(state).toEqual(expect.objectContaining({ progress: 85 }));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('returns fallbackInterval on GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL', () => {
|
||||||
|
const fallbackInterval: DateInterval = 'last30Days';
|
||||||
|
const state = reducer(undefined, { type: GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL, fallbackInterval } as any);
|
||||||
|
|
||||||
|
expect(state).toEqual(expect.objectContaining({ fallbackInterval }));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getOrphanVisits', () => {
|
describe('getOrphanVisits', () => {
|
||||||
|
@ -163,6 +172,38 @@ describe('orphanVisitsReducer', () => {
|
||||||
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_ORPHAN_VISITS, visits, query: query ?? {} });
|
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_ORPHAN_VISITS, visits, query: query ?? {} });
|
||||||
expect(ShlinkApiClient.getOrphanVisits).toHaveBeenCalledTimes(1);
|
expect(ShlinkApiClient.getOrphanVisits).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
[
|
||||||
|
[ Mock.of<Visit>({ date: formatISO(subDays(new Date(), 5)) }) ],
|
||||||
|
{ type: GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL, fallbackInterval: 'last7Days' },
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[ Mock.of<Visit>({ date: formatISO(subDays(new Date(), 200)) }) ],
|
||||||
|
{ type: GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL, fallbackInterval: 'last365Days' },
|
||||||
|
],
|
||||||
|
[[], expect.objectContaining({ type: GET_ORPHAN_VISITS }) ],
|
||||||
|
])('dispatches fallback interval when the list of visits is empty', async (lastVisits, expectedSecondDispatch) => {
|
||||||
|
const buildVisitsResult = (data: Visit[] = []): ShlinkVisits => ({
|
||||||
|
data,
|
||||||
|
pagination: {
|
||||||
|
currentPage: 1,
|
||||||
|
pagesCount: 1,
|
||||||
|
totalItems: 1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const getShlinkOrphanVisits = jest.fn()
|
||||||
|
.mockResolvedValueOnce(buildVisitsResult())
|
||||||
|
.mockResolvedValueOnce(buildVisitsResult(lastVisits));
|
||||||
|
const ShlinkApiClient = Mock.of<ShlinkApiClient>({ getOrphanVisits: getShlinkOrphanVisits });
|
||||||
|
|
||||||
|
await getOrphanVisits(() => ShlinkApiClient)({}, undefined, true)(dispatchMock, getState);
|
||||||
|
|
||||||
|
expect(dispatchMock).toHaveBeenCalledTimes(2);
|
||||||
|
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_ORPHAN_VISITS_START });
|
||||||
|
expect(dispatchMock).toHaveBeenNthCalledWith(2, expectedSecondDispatch);
|
||||||
|
expect(getShlinkOrphanVisits).toHaveBeenCalledTimes(2);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('cancelGetOrphanVisits', () => {
|
describe('cancelGetOrphanVisits', () => {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Mock } from 'ts-mockery';
|
import { Mock } from 'ts-mockery';
|
||||||
import { addDays, subDays } from 'date-fns';
|
import { addDays, formatISO, subDays } from 'date-fns';
|
||||||
import reducer, {
|
import reducer, {
|
||||||
getShortUrlVisits,
|
getShortUrlVisits,
|
||||||
cancelGetShortUrlVisits,
|
cancelGetShortUrlVisits,
|
||||||
|
@ -9,6 +9,7 @@ import reducer, {
|
||||||
GET_SHORT_URL_VISITS_LARGE,
|
GET_SHORT_URL_VISITS_LARGE,
|
||||||
GET_SHORT_URL_VISITS_CANCEL,
|
GET_SHORT_URL_VISITS_CANCEL,
|
||||||
GET_SHORT_URL_VISITS_PROGRESS_CHANGED,
|
GET_SHORT_URL_VISITS_PROGRESS_CHANGED,
|
||||||
|
GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL,
|
||||||
ShortUrlVisits,
|
ShortUrlVisits,
|
||||||
} from '../../../src/visits/reducers/shortUrlVisits';
|
} from '../../../src/visits/reducers/shortUrlVisits';
|
||||||
import { CREATE_VISITS } from '../../../src/visits/reducers/visitCreation';
|
import { CREATE_VISITS } from '../../../src/visits/reducers/visitCreation';
|
||||||
|
@ -18,6 +19,7 @@ import { ShlinkVisits } from '../../../src/api/types';
|
||||||
import ShlinkApiClient from '../../../src/api/services/ShlinkApiClient';
|
import ShlinkApiClient from '../../../src/api/services/ShlinkApiClient';
|
||||||
import { ShlinkState } from '../../../src/container/types';
|
import { ShlinkState } from '../../../src/container/types';
|
||||||
import { formatIsoDate } from '../../../src/utils/helpers/date';
|
import { formatIsoDate } from '../../../src/utils/helpers/date';
|
||||||
|
import { DateInterval } from '../../../src/utils/dates/types';
|
||||||
|
|
||||||
describe('shortUrlVisitsReducer', () => {
|
describe('shortUrlVisitsReducer', () => {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
@ -137,6 +139,13 @@ describe('shortUrlVisitsReducer', () => {
|
||||||
|
|
||||||
expect(state).toEqual(expect.objectContaining({ progress: 85 }));
|
expect(state).toEqual(expect.objectContaining({ progress: 85 }));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('returns fallbackInterval on GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL', () => {
|
||||||
|
const fallbackInterval: DateInterval = 'last30Days';
|
||||||
|
const state = reducer(undefined, { type: GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL, fallbackInterval } as any);
|
||||||
|
|
||||||
|
expect(state).toEqual(expect.objectContaining({ fallbackInterval }));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getShortUrlVisits', () => {
|
describe('getShortUrlVisits', () => {
|
||||||
|
@ -209,6 +218,38 @@ describe('shortUrlVisitsReducer', () => {
|
||||||
visits: [ ...visitsMocks, ...visitsMocks, ...visitsMocks ],
|
visits: [ ...visitsMocks, ...visitsMocks, ...visitsMocks ],
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
[
|
||||||
|
[ Mock.of<Visit>({ date: formatISO(subDays(new Date(), 5)) }) ],
|
||||||
|
{ type: GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL, fallbackInterval: 'last7Days' },
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[ Mock.of<Visit>({ date: formatISO(subDays(new Date(), 200)) }) ],
|
||||||
|
{ type: GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL, fallbackInterval: 'last365Days' },
|
||||||
|
],
|
||||||
|
[[], expect.objectContaining({ type: GET_SHORT_URL_VISITS }) ],
|
||||||
|
])('dispatches fallback interval when the list of visits is empty', async (lastVisits, expectedSecondDispatch) => {
|
||||||
|
const buildVisitsResult = (data: Visit[] = []): ShlinkVisits => ({
|
||||||
|
data,
|
||||||
|
pagination: {
|
||||||
|
currentPage: 1,
|
||||||
|
pagesCount: 1,
|
||||||
|
totalItems: 1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const getShlinkShortUrlVisits = jest.fn()
|
||||||
|
.mockResolvedValueOnce(buildVisitsResult())
|
||||||
|
.mockResolvedValueOnce(buildVisitsResult(lastVisits));
|
||||||
|
const ShlinkApiClient = Mock.of<ShlinkApiClient>({ getShortUrlVisits: getShlinkShortUrlVisits });
|
||||||
|
|
||||||
|
await getShortUrlVisits(() => ShlinkApiClient)('abc123', {}, true)(dispatchMock, getState);
|
||||||
|
|
||||||
|
expect(dispatchMock).toHaveBeenCalledTimes(2);
|
||||||
|
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_VISITS_START });
|
||||||
|
expect(dispatchMock).toHaveBeenNthCalledWith(2, expectedSecondDispatch);
|
||||||
|
expect(getShlinkShortUrlVisits).toHaveBeenCalledTimes(2);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('cancelGetShortUrlVisits', () => {
|
describe('cancelGetShortUrlVisits', () => {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Mock } from 'ts-mockery';
|
import { Mock } from 'ts-mockery';
|
||||||
import { addDays, subDays } from 'date-fns';
|
import { addDays, formatISO, subDays } from 'date-fns';
|
||||||
import reducer, {
|
import reducer, {
|
||||||
getTagVisits,
|
getTagVisits,
|
||||||
cancelGetTagVisits,
|
cancelGetTagVisits,
|
||||||
|
@ -9,6 +9,7 @@ import reducer, {
|
||||||
GET_TAG_VISITS_LARGE,
|
GET_TAG_VISITS_LARGE,
|
||||||
GET_TAG_VISITS_CANCEL,
|
GET_TAG_VISITS_CANCEL,
|
||||||
GET_TAG_VISITS_PROGRESS_CHANGED,
|
GET_TAG_VISITS_PROGRESS_CHANGED,
|
||||||
|
GET_TAG_VISITS_FALLBACK_TO_INTERVAL,
|
||||||
TagVisits,
|
TagVisits,
|
||||||
} from '../../../src/visits/reducers/tagVisits';
|
} from '../../../src/visits/reducers/tagVisits';
|
||||||
import { CREATE_VISITS } from '../../../src/visits/reducers/visitCreation';
|
import { CREATE_VISITS } from '../../../src/visits/reducers/visitCreation';
|
||||||
|
@ -18,6 +19,7 @@ import { ShlinkVisits } from '../../../src/api/types';
|
||||||
import ShlinkApiClient from '../../../src/api/services/ShlinkApiClient';
|
import ShlinkApiClient from '../../../src/api/services/ShlinkApiClient';
|
||||||
import { ShlinkState } from '../../../src/container/types';
|
import { ShlinkState } from '../../../src/container/types';
|
||||||
import { formatIsoDate } from '../../../src/utils/helpers/date';
|
import { formatIsoDate } from '../../../src/utils/helpers/date';
|
||||||
|
import { DateInterval } from '../../../src/utils/dates/types';
|
||||||
|
|
||||||
describe('tagVisitsReducer', () => {
|
describe('tagVisitsReducer', () => {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
@ -137,6 +139,13 @@ describe('tagVisitsReducer', () => {
|
||||||
|
|
||||||
expect(state).toEqual(expect.objectContaining({ progress: 85 }));
|
expect(state).toEqual(expect.objectContaining({ progress: 85 }));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('returns fallbackInterval on GET_TAG_VISITS_FALLBACK_TO_INTERVAL', () => {
|
||||||
|
const fallbackInterval: DateInterval = 'last30Days';
|
||||||
|
const state = reducer(undefined, { type: GET_TAG_VISITS_FALLBACK_TO_INTERVAL, fallbackInterval } as any);
|
||||||
|
|
||||||
|
expect(state).toEqual(expect.objectContaining({ fallbackInterval }));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getTagVisits', () => {
|
describe('getTagVisits', () => {
|
||||||
|
@ -149,8 +158,9 @@ describe('tagVisitsReducer', () => {
|
||||||
const getState = () => Mock.of<ShlinkState>({
|
const getState = () => Mock.of<ShlinkState>({
|
||||||
tagVisits: { cancelLoad: false },
|
tagVisits: { cancelLoad: false },
|
||||||
});
|
});
|
||||||
|
const tag = 'foo';
|
||||||
|
|
||||||
beforeEach(jest.resetAllMocks);
|
beforeEach(jest.clearAllMocks);
|
||||||
|
|
||||||
it('dispatches start and error when promise is rejected', async () => {
|
it('dispatches start and error when promise is rejected', async () => {
|
||||||
const ShlinkApiClient = buildApiClientMock(Promise.reject({}));
|
const ShlinkApiClient = buildApiClientMock(Promise.reject({}));
|
||||||
|
@ -168,7 +178,6 @@ describe('tagVisitsReducer', () => {
|
||||||
[{}],
|
[{}],
|
||||||
])('dispatches start and success when promise is resolved', async (query) => {
|
])('dispatches start and success when promise is resolved', async (query) => {
|
||||||
const visits = visitsMocks;
|
const visits = visitsMocks;
|
||||||
const tag = 'foo';
|
|
||||||
const ShlinkApiClient = buildApiClientMock(Promise.resolve({
|
const ShlinkApiClient = buildApiClientMock(Promise.resolve({
|
||||||
data: visitsMocks,
|
data: visitsMocks,
|
||||||
pagination: {
|
pagination: {
|
||||||
|
@ -185,6 +194,38 @@ describe('tagVisitsReducer', () => {
|
||||||
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_TAG_VISITS, visits, tag, query: query ?? {} });
|
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_TAG_VISITS, visits, tag, query: query ?? {} });
|
||||||
expect(ShlinkApiClient.getTagVisits).toHaveBeenCalledTimes(1);
|
expect(ShlinkApiClient.getTagVisits).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
[
|
||||||
|
[ Mock.of<Visit>({ date: formatISO(subDays(new Date(), 20)) }) ],
|
||||||
|
{ type: GET_TAG_VISITS_FALLBACK_TO_INTERVAL, fallbackInterval: 'last30Days' },
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[ Mock.of<Visit>({ date: formatISO(subDays(new Date(), 100)) }) ],
|
||||||
|
{ type: GET_TAG_VISITS_FALLBACK_TO_INTERVAL, fallbackInterval: 'last180Days' },
|
||||||
|
],
|
||||||
|
[[], expect.objectContaining({ type: GET_TAG_VISITS }) ],
|
||||||
|
])('dispatches fallback interval when the list of visits is empty', async (lastVisits, expectedSecondDispatch) => {
|
||||||
|
const buildVisitsResult = (data: Visit[] = []): ShlinkVisits => ({
|
||||||
|
data,
|
||||||
|
pagination: {
|
||||||
|
currentPage: 1,
|
||||||
|
pagesCount: 1,
|
||||||
|
totalItems: 1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const getShlinkTagVisits = jest.fn()
|
||||||
|
.mockResolvedValueOnce(buildVisitsResult())
|
||||||
|
.mockResolvedValueOnce(buildVisitsResult(lastVisits));
|
||||||
|
const ShlinkApiClient = Mock.of<ShlinkApiClient>({ getTagVisits: getShlinkTagVisits });
|
||||||
|
|
||||||
|
await getTagVisits(() => ShlinkApiClient)(tag, {}, true)(dispatchMock, getState);
|
||||||
|
|
||||||
|
expect(dispatchMock).toHaveBeenCalledTimes(2);
|
||||||
|
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_TAG_VISITS_START });
|
||||||
|
expect(dispatchMock).toHaveBeenNthCalledWith(2, expectedSecondDispatch);
|
||||||
|
expect(getShlinkTagVisits).toHaveBeenCalledTimes(2);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('cancelGetTagVisits', () => {
|
describe('cancelGetTagVisits', () => {
|
||||||
|
|
Loading…
Reference in a new issue