diff --git a/src/visits/reducers/common.ts b/src/visits/reducers/common.ts index 1a08d7a7..4c30e58a 100644 --- a/src/visits/reducers/common.ts +++ b/src/visits/reducers/common.ts @@ -15,24 +15,16 @@ const calcProgress = (total: number, current: number): number => (current * 100) type VisitsLoader = (page: number, itemsPerPage: number) => Promise; type LastVisitLoader = () => Promise; -interface ActionMap { - start: string; - large: string; - finish: string; - error: string; - progress: string; - fallbackToInterval: string; -} export const getVisitsWithLoader = async & { visits: Visit[] }>( visitsLoader: VisitsLoader, lastVisitLoader: LastVisitLoader, extraFinishActionData: Partial, - actionMap: ActionMap, + actionsPrefix: string, dispatch: Dispatch, shouldCancel: () => boolean, ) => { - dispatch({ type: actionMap.start }); + dispatch({ type: `${actionsPrefix}/pending` }); const loadVisitsInParallel = async (pages: number[]): Promise => Promise.all(pages.map(async (page) => visitsLoader(page, ITEMS_PER_PAGE).then(prop('data')))).then(flatten); @@ -44,7 +36,10 @@ export const getVisitsWithLoader = async & { visits: V const data = await loadVisitsInParallel(pagesBlocks[index]); - dispatch({ type: actionMap.progress, progress: calcProgress(pagesBlocks.length, index + PARALLEL_STARTING_PAGE) }); + dispatch({ + type: `${actionsPrefix}/progressChanged`, + progress: calcProgress(pagesBlocks.length, index + PARALLEL_STARTING_PAGE), + }); if (index < pagesBlocks.length - 1) { return data.concat(await loadPagesBlocks(pagesBlocks, index + 1)); @@ -66,7 +61,7 @@ export const getVisitsWithLoader = async & { visits: V const pagesBlocks = splitEvery(PARALLEL_REQUESTS_COUNT, pagesRange); if (pagination.pagesCount - 1 > PARALLEL_REQUESTS_COUNT) { - dispatch({ type: actionMap.large }); + dispatch({ type: `${actionsPrefix}/large` }); } return data.concat(await loadPagesBlocks(pagesBlocks)); @@ -77,11 +72,11 @@ export const getVisitsWithLoader = async & { visits: V dispatch( !visits.length && lastVisit - ? { type: actionMap.fallbackToInterval, fallbackInterval: dateToMatchingInterval(lastVisit.date) } - : { ...extraFinishActionData, visits, type: actionMap.finish }, + ? { type: `${actionsPrefix}/fallbackToInterval`, fallbackInterval: dateToMatchingInterval(lastVisit.date) } + : { ...extraFinishActionData, visits, type: `${actionsPrefix}/fulfilled` }, ); } catch (e: any) { - dispatch({ type: actionMap.error, errorData: parseApiError(e) }); + dispatch({ type: `${actionsPrefix}/rejected`, errorData: parseApiError(e) }); } }; diff --git a/src/visits/reducers/domainVisits.ts b/src/visits/reducers/domainVisits.ts index eb2c80fe..7f964cba 100644 --- a/src/visits/reducers/domainVisits.ts +++ b/src/visits/reducers/domainVisits.ts @@ -1,6 +1,7 @@ +import { createAction } from '@reduxjs/toolkit'; import { Action, Dispatch } from 'redux'; import { Visit, VisitsFallbackIntervalAction, VisitsInfo, VisitsLoadProgressChangedAction } from '../types'; -import { buildActionCreator, buildReducer } from '../../utils/helpers/redux'; +import { buildReducer } from '../../utils/helpers/redux'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; import { GetState } from '../../container/types'; import { ShlinkVisitsParams } from '../../api/types'; @@ -10,13 +11,14 @@ import { getVisitsWithLoader, lastVisitLoaderForLoader } from './common'; import { createNewVisits, CreateVisitsAction } from './visitCreation'; import { domainMatches } from '../../short-urls/helpers'; -export const GET_DOMAIN_VISITS_START = 'shlink/domainVisits/GET_DOMAIN_VISITS_START'; -export const GET_DOMAIN_VISITS_ERROR = 'shlink/domainVisits/GET_DOMAIN_VISITS_ERROR'; -export const GET_DOMAIN_VISITS = 'shlink/domainVisits/GET_DOMAIN_VISITS'; -export const GET_DOMAIN_VISITS_LARGE = 'shlink/domainVisits/GET_DOMAIN_VISITS_LARGE'; -export const GET_DOMAIN_VISITS_CANCEL = 'shlink/domainVisits/GET_DOMAIN_VISITS_CANCEL'; -export const GET_DOMAIN_VISITS_PROGRESS_CHANGED = 'shlink/domainVisits/GET_DOMAIN_VISITS_PROGRESS_CHANGED'; -export const GET_DOMAIN_VISITS_FALLBACK_TO_INTERVAL = 'shlink/domainVisits/GET_DOMAIN_VISITS_FALLBACK_TO_INTERVAL'; +const REDUCER_PREFIX = 'shlink/domainVisits'; +export const GET_DOMAIN_VISITS_START = `${REDUCER_PREFIX}/getDomainVisits/pending`; +export const GET_DOMAIN_VISITS_ERROR = `${REDUCER_PREFIX}/getDomainVisits/rejected`; +export const GET_DOMAIN_VISITS = `${REDUCER_PREFIX}/getDomainVisits/fulfilled`; +export const GET_DOMAIN_VISITS_LARGE = `${REDUCER_PREFIX}/getDomainVisits/large`; +export const GET_DOMAIN_VISITS_CANCEL = `${REDUCER_PREFIX}/getDomainVisits/cancel`; +export const GET_DOMAIN_VISITS_PROGRESS_CHANGED = `${REDUCER_PREFIX}/getDomainVisits/progressChanged`; +export const GET_DOMAIN_VISITS_FALLBACK_TO_INTERVAL = `${REDUCER_PREFIX}/getDomainVisits/fallbackToInterval`; export const DEFAULT_DOMAIN = 'DEFAULT'; @@ -47,15 +49,17 @@ const initialState: DomainVisits = { }; export default buildReducer({ - [GET_DOMAIN_VISITS_START]: () => ({ ...initialState, loading: true }), - [GET_DOMAIN_VISITS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }), - [GET_DOMAIN_VISITS]: (state, { visits, domain, query }) => ( + [`${REDUCER_PREFIX}/getDomainVisits/pending`]: () => ({ ...initialState, loading: true }), + [`${REDUCER_PREFIX}/getDomainVisits/rejected`]: (_, { errorData }) => ({ ...initialState, error: true, errorData }), + [`${REDUCER_PREFIX}/getDomainVisits/fulfilled`]: (state, { visits, domain, query }) => ( { ...state, visits, domain, query, loading: false, loadingLarge: false, error: false } ), - [GET_DOMAIN_VISITS_LARGE]: (state) => ({ ...state, loadingLarge: true }), - [GET_DOMAIN_VISITS_CANCEL]: (state) => ({ ...state, cancelLoad: true }), - [GET_DOMAIN_VISITS_PROGRESS_CHANGED]: (state, { progress }) => ({ ...state, progress }), - [GET_DOMAIN_VISITS_FALLBACK_TO_INTERVAL]: (state, { fallbackInterval }) => ({ ...state, fallbackInterval }), + [`${REDUCER_PREFIX}/getDomainVisits/large`]: (state) => ({ ...state, loadingLarge: true }), + [`${REDUCER_PREFIX}/getDomainVisits/cancel`]: (state) => ({ ...state, cancelLoad: true }), + [`${REDUCER_PREFIX}/getDomainVisits/progressChanged`]: (state, { progress }) => ({ ...state, progress }), + [`${REDUCER_PREFIX}/getDomainVisits/fallbackToInterval`]: (state, { fallbackInterval }) => ( + { ...state, fallbackInterval } + ), [createNewVisits.toString()]: (state, { payload }) => { const { domain, visits, query = {} } = state; const { startDate, endDate } = query; @@ -81,16 +85,9 @@ export const getDomainVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => const lastVisitLoader = lastVisitLoaderForLoader(doIntervalFallback, async (params) => getVisits(domain, params)); const shouldCancel = () => getState().domainVisits.cancelLoad; const extraFinishActionData: Partial = { domain, query }; - const actionMap = { - start: GET_DOMAIN_VISITS_START, - large: GET_DOMAIN_VISITS_LARGE, - finish: GET_DOMAIN_VISITS, - error: GET_DOMAIN_VISITS_ERROR, - progress: GET_DOMAIN_VISITS_PROGRESS_CHANGED, - fallbackToInterval: GET_DOMAIN_VISITS_FALLBACK_TO_INTERVAL, - }; + const prefix = `${REDUCER_PREFIX}/getDomainVisits`; - return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, actionMap, dispatch, shouldCancel); + return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, prefix, dispatch, shouldCancel); }; -export const cancelGetDomainVisits = buildActionCreator(GET_DOMAIN_VISITS_CANCEL); +export const cancelGetDomainVisits = createAction(`${REDUCER_PREFIX}/getDomainVisits/cancel`); diff --git a/src/visits/reducers/nonOrphanVisits.ts b/src/visits/reducers/nonOrphanVisits.ts index 94c037fc..0b4a307c 100644 --- a/src/visits/reducers/nonOrphanVisits.ts +++ b/src/visits/reducers/nonOrphanVisits.ts @@ -1,3 +1,4 @@ +import { createAction } from '@reduxjs/toolkit'; import { Action, Dispatch } from 'redux'; import { Visit, @@ -5,7 +6,7 @@ import { VisitsInfo, VisitsLoadProgressChangedAction, } from '../types'; -import { buildActionCreator, buildReducer } from '../../utils/helpers/redux'; +import { buildReducer } from '../../utils/helpers/redux'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; import { GetState } from '../../container/types'; import { ShlinkVisitsParams } from '../../api/types'; @@ -14,13 +15,14 @@ import { isBetween } from '../../utils/helpers/date'; import { getVisitsWithLoader, lastVisitLoaderForLoader } from './common'; import { createNewVisits, CreateVisitsAction } from './visitCreation'; -export const GET_NON_ORPHAN_VISITS_START = 'shlink/orphanVisits/GET_NON_ORPHAN_VISITS_START'; -export const GET_NON_ORPHAN_VISITS_ERROR = 'shlink/orphanVisits/GET_NON_ORPHAN_VISITS_ERROR'; -export const GET_NON_ORPHAN_VISITS = 'shlink/orphanVisits/GET_NON_ORPHAN_VISITS'; -export const GET_NON_ORPHAN_VISITS_LARGE = 'shlink/orphanVisits/GET_NON_ORPHAN_VISITS_LARGE'; -export const GET_NON_ORPHAN_VISITS_CANCEL = 'shlink/orphanVisits/GET_NON_ORPHAN_VISITS_CANCEL'; -export const GET_NON_ORPHAN_VISITS_PROGRESS_CHANGED = 'shlink/orphanVisits/GET_NON_ORPHAN_VISITS_PROGRESS_CHANGED'; -export const GET_NON_ORPHAN_VISITS_FALLBACK_TO_INTERVAL = 'shlink/orphanVisits/GET_NON_ORPHAN_VISITS_FALLBACK_TO_INTERVAL'; +const REDUCER_PREFIX = 'shlink/orphanVisits'; +export const GET_NON_ORPHAN_VISITS_START = `${REDUCER_PREFIX}/getNonOrphanVisits/pending`; +export const GET_NON_ORPHAN_VISITS_ERROR = `${REDUCER_PREFIX}/getNonOrphanVisits/rejected`; +export const GET_NON_ORPHAN_VISITS = `${REDUCER_PREFIX}/getNonOrphanVisits/fulfilled`; +export const GET_NON_ORPHAN_VISITS_LARGE = `${REDUCER_PREFIX}/getNonOrphanVisits/large`; +export const GET_NON_ORPHAN_VISITS_CANCEL = `${REDUCER_PREFIX}/getNonOrphanVisits/cancel`; +export const GET_NON_ORPHAN_VISITS_PROGRESS_CHANGED = `${REDUCER_PREFIX}/getNonOrphanVisits/progressChanged`; +export const GET_NON_ORPHAN_VISITS_FALLBACK_TO_INTERVAL = `${REDUCER_PREFIX}/getNonOrphanVisits/fallbackToInterval`; export interface NonOrphanVisitsAction extends Action { visits: Visit[]; @@ -43,15 +45,19 @@ const initialState: VisitsInfo = { }; export default buildReducer({ - [GET_NON_ORPHAN_VISITS_START]: () => ({ ...initialState, loading: true }), - [GET_NON_ORPHAN_VISITS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }), - [GET_NON_ORPHAN_VISITS]: (state, { visits, query }) => ( + [`${REDUCER_PREFIX}/getNonOrphanVisits/pending`]: () => ({ ...initialState, loading: true }), + [`${REDUCER_PREFIX}/getNonOrphanVisits/rejected`]: (_, { errorData }) => ( + { ...initialState, error: true, errorData } + ), + [`${REDUCER_PREFIX}/getNonOrphanVisits/fulfilled`]: (state, { visits, query }) => ( { ...state, visits, query, loading: false, loadingLarge: false, error: false } ), - [GET_NON_ORPHAN_VISITS_LARGE]: (state) => ({ ...state, loadingLarge: true }), - [GET_NON_ORPHAN_VISITS_CANCEL]: (state) => ({ ...state, cancelLoad: true }), - [GET_NON_ORPHAN_VISITS_PROGRESS_CHANGED]: (state, { progress }) => ({ ...state, progress }), - [GET_NON_ORPHAN_VISITS_FALLBACK_TO_INTERVAL]: (state, { fallbackInterval }) => ({ ...state, fallbackInterval }), + [`${REDUCER_PREFIX}/getNonOrphanVisits/large`]: (state) => ({ ...state, loadingLarge: true }), + [`${REDUCER_PREFIX}/getNonOrphanVisits/cancel`]: (state) => ({ ...state, cancelLoad: true }), + [`${REDUCER_PREFIX}/getNonOrphanVisits/progressChanged`]: (state, { progress }) => ({ ...state, progress }), + [`${REDUCER_PREFIX}/getNonOrphanVisits/fallbackToInterval`]: (state, { fallbackInterval }) => ( + { ...state, fallbackInterval } + ), [createNewVisits.toString()]: (state, { payload }) => { const { visits, query = {} } = state; const { startDate, endDate } = query; @@ -73,16 +79,9 @@ export const getNonOrphanVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) const lastVisitLoader = lastVisitLoaderForLoader(doIntervalFallback, shlinkGetNonOrphanVisits); const shouldCancel = () => getState().orphanVisits.cancelLoad; const extraFinishActionData: Partial = { query }; - const actionMap = { - start: GET_NON_ORPHAN_VISITS_START, - large: GET_NON_ORPHAN_VISITS_LARGE, - finish: GET_NON_ORPHAN_VISITS, - error: GET_NON_ORPHAN_VISITS_ERROR, - progress: GET_NON_ORPHAN_VISITS_PROGRESS_CHANGED, - fallbackToInterval: GET_NON_ORPHAN_VISITS_FALLBACK_TO_INTERVAL, - }; + const prefix = `${REDUCER_PREFIX}/getNonOrphanVisits`; - return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, actionMap, dispatch, shouldCancel); + return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, prefix, dispatch, shouldCancel); }; -export const cancelGetNonOrphanVisits = buildActionCreator(GET_NON_ORPHAN_VISITS_CANCEL); +export const cancelGetNonOrphanVisits = createAction(`${REDUCER_PREFIX}/getNonOrphanVisits/cancel`); diff --git a/src/visits/reducers/orphanVisits.ts b/src/visits/reducers/orphanVisits.ts index ecbe832d..188018f5 100644 --- a/src/visits/reducers/orphanVisits.ts +++ b/src/visits/reducers/orphanVisits.ts @@ -1,3 +1,4 @@ +import { createAction } from '@reduxjs/toolkit'; import { Action, Dispatch } from 'redux'; import { OrphanVisit, @@ -7,7 +8,7 @@ import { VisitsInfo, VisitsLoadProgressChangedAction, } from '../types'; -import { buildActionCreator, buildReducer } from '../../utils/helpers/redux'; +import { buildReducer } from '../../utils/helpers/redux'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; import { GetState } from '../../container/types'; import { ShlinkVisitsParams } from '../../api/types'; @@ -17,13 +18,14 @@ import { isBetween } from '../../utils/helpers/date'; import { getVisitsWithLoader, lastVisitLoaderForLoader } from './common'; import { createNewVisits, CreateVisitsAction } from './visitCreation'; -export const GET_ORPHAN_VISITS_START = 'shlink/orphanVisits/GET_ORPHAN_VISITS_START'; -export const GET_ORPHAN_VISITS_ERROR = 'shlink/orphanVisits/GET_ORPHAN_VISITS_ERROR'; -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_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_FALLBACK_TO_INTERVAL = 'shlink/orphanVisits/GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL'; +const REDUCER_PREFIX = 'shlink/orphanVisits'; +export const GET_ORPHAN_VISITS_START = `${REDUCER_PREFIX}/getOrphanVisits/pending`; +export const GET_ORPHAN_VISITS_ERROR = `${REDUCER_PREFIX}/getOrphanVisits/rejected`; +export const GET_ORPHAN_VISITS = `${REDUCER_PREFIX}/getOrphanVisits/fulfilled`; +export const GET_ORPHAN_VISITS_LARGE = `${REDUCER_PREFIX}/getOrphanVisits/large`; +export const GET_ORPHAN_VISITS_CANCEL = `${REDUCER_PREFIX}/getOrphanVisits/cancel`; +export const GET_ORPHAN_VISITS_PROGRESS_CHANGED = `${REDUCER_PREFIX}/getOrphanVisits/progressChanged`; +export const GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL = `${REDUCER_PREFIX}/getOrphanVisits/fallbackToInterval`; export interface OrphanVisitsAction extends Action { visits: Visit[]; @@ -46,15 +48,17 @@ const initialState: VisitsInfo = { }; export default buildReducer({ - [GET_ORPHAN_VISITS_START]: () => ({ ...initialState, loading: true }), - [GET_ORPHAN_VISITS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }), - [GET_ORPHAN_VISITS]: (state, { visits, query }) => ( + [`${REDUCER_PREFIX}/getOrphanVisits/pending`]: () => ({ ...initialState, loading: true }), + [`${REDUCER_PREFIX}/getOrphanVisits/rejected`]: (_, { errorData }) => ({ ...initialState, error: true, errorData }), + [`${REDUCER_PREFIX}/getOrphanVisits/fulfilled`]: (state, { visits, query }) => ( { ...state, visits, query, loading: false, loadingLarge: false, error: false } ), - [GET_ORPHAN_VISITS_LARGE]: (state) => ({ ...state, loadingLarge: true }), - [GET_ORPHAN_VISITS_CANCEL]: (state) => ({ ...state, cancelLoad: true }), - [GET_ORPHAN_VISITS_PROGRESS_CHANGED]: (state, { progress }) => ({ ...state, progress }), - [GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL]: (state, { fallbackInterval }) => ({ ...state, fallbackInterval }), + [`${REDUCER_PREFIX}/getOrphanVisits/large`]: (state) => ({ ...state, loadingLarge: true }), + [`${REDUCER_PREFIX}/getOrphanVisits/cancel`]: (state) => ({ ...state, cancelLoad: true }), + [`${REDUCER_PREFIX}/getOrphanVisits/progressChanged`]: (state, { progress }) => ({ ...state, progress }), + [`${REDUCER_PREFIX}/getOrphanVisits/fallbackToInterval`]: (state, { fallbackInterval }) => ( + { ...state, fallbackInterval } + ), [createNewVisits.toString()]: (state, { payload }) => { const { visits, query = {} } = state; const { startDate, endDate } = query; @@ -84,16 +88,9 @@ export const getOrphanVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => const lastVisitLoader = lastVisitLoaderForLoader(doIntervalFallback, getVisits); const shouldCancel = () => getState().orphanVisits.cancelLoad; const extraFinishActionData: Partial = { query }; - const actionMap = { - start: GET_ORPHAN_VISITS_START, - large: GET_ORPHAN_VISITS_LARGE, - finish: GET_ORPHAN_VISITS, - error: GET_ORPHAN_VISITS_ERROR, - progress: GET_ORPHAN_VISITS_PROGRESS_CHANGED, - fallbackToInterval: GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL, - }; + const prefix = `${REDUCER_PREFIX}/getOrphanVisits`; - return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, actionMap, dispatch, shouldCancel); + return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, prefix, dispatch, shouldCancel); }; -export const cancelGetOrphanVisits = buildActionCreator(GET_ORPHAN_VISITS_CANCEL); +export const cancelGetOrphanVisits = createAction(`${REDUCER_PREFIX}/getOrphanVisits/cancel`); diff --git a/src/visits/reducers/shortUrlVisits.ts b/src/visits/reducers/shortUrlVisits.ts index c768d8b5..2e314f96 100644 --- a/src/visits/reducers/shortUrlVisits.ts +++ b/src/visits/reducers/shortUrlVisits.ts @@ -1,8 +1,9 @@ +import { createAction } from '@reduxjs/toolkit'; import { Action, Dispatch } from 'redux'; import { shortUrlMatches } from '../../short-urls/helpers'; import { Visit, VisitsFallbackIntervalAction, VisitsInfo, VisitsLoadProgressChangedAction } from '../types'; import { ShortUrlIdentifier } from '../../short-urls/data'; -import { buildActionCreator, buildReducer } from '../../utils/helpers/redux'; +import { buildReducer } from '../../utils/helpers/redux'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; import { GetState } from '../../container/types'; import { ShlinkVisitsParams } from '../../api/types'; @@ -11,13 +12,14 @@ import { isBetween } from '../../utils/helpers/date'; import { getVisitsWithLoader, lastVisitLoaderForLoader } from './common'; import { createNewVisits, CreateVisitsAction } from './visitCreation'; -export const GET_SHORT_URL_VISITS_START = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_START'; -export const GET_SHORT_URL_VISITS_ERROR = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_ERROR'; -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_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_FALLBACK_TO_INTERVAL = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL'; +const REDUCER_PREFIX = 'shlink/shortUrlVisits'; +export const GET_SHORT_URL_VISITS_START = `${REDUCER_PREFIX}/getShortUrlVisits/pending`; +export const GET_SHORT_URL_VISITS_ERROR = `${REDUCER_PREFIX}/getShortUrlVisits/rejected`; +export const GET_SHORT_URL_VISITS = `${REDUCER_PREFIX}/getShortUrlVisits/fulfilled`; +export const GET_SHORT_URL_VISITS_LARGE = `${REDUCER_PREFIX}/getShortUrlVisits/large`; +export const GET_SHORT_URL_VISITS_CANCEL = `${REDUCER_PREFIX}/getShortUrlVisits/cancel`; +export const GET_SHORT_URL_VISITS_PROGRESS_CHANGED = `${REDUCER_PREFIX}/getShortUrlVisits/progressChanged`; +export const GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL = `${REDUCER_PREFIX}/getShortUrlVisits/fallbackToInterval`; export interface ShortUrlVisits extends VisitsInfo, ShortUrlIdentifier {} @@ -44,9 +46,9 @@ const initialState: ShortUrlVisits = { }; export default buildReducer({ - [GET_SHORT_URL_VISITS_START]: () => ({ ...initialState, loading: true }), - [GET_SHORT_URL_VISITS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }), - [GET_SHORT_URL_VISITS]: (state, { visits, query, shortCode, domain }) => ({ + [`${REDUCER_PREFIX}/getShortUrlVisits/pending`]: () => ({ ...initialState, loading: true }), + [`${REDUCER_PREFIX}/getShortUrlVisits/rejected`]: (_, { errorData }) => ({ ...initialState, error: true, errorData }), + [`${REDUCER_PREFIX}/getShortUrlVisits/fulfilled`]: (state, { visits, query, shortCode, domain }) => ({ ...state, visits, shortCode, @@ -56,10 +58,12 @@ export default buildReducer({ loadingLarge: false, error: false, }), - [GET_SHORT_URL_VISITS_LARGE]: (state) => ({ ...state, loadingLarge: true }), - [GET_SHORT_URL_VISITS_CANCEL]: (state) => ({ ...state, cancelLoad: true }), - [GET_SHORT_URL_VISITS_PROGRESS_CHANGED]: (state, { progress }) => ({ ...state, progress }), - [GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL]: (state, { fallbackInterval }) => ({ ...state, fallbackInterval }), + [`${REDUCER_PREFIX}/getShortUrlVisits/large`]: (state) => ({ ...state, loadingLarge: true }), + [`${REDUCER_PREFIX}/getShortUrlVisits/cancel`]: (state) => ({ ...state, cancelLoad: true }), + [`${REDUCER_PREFIX}/getShortUrlVisits/progressChanged`]: (state, { progress }) => ({ ...state, progress }), + [`${REDUCER_PREFIX}/getShortUrlVisits/fallbackToInterval`]: (state, { fallbackInterval }) => ( + { ...state, fallbackInterval } + ), [createNewVisits.toString()]: (state, { payload }) => { const { shortCode, domain, visits, query = {} } = state; const { startDate, endDate } = query; @@ -90,16 +94,9 @@ export const getShortUrlVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) ); const shouldCancel = () => getState().shortUrlVisits.cancelLoad; const extraFinishActionData: Partial = { shortCode, query, domain: query.domain }; - const actionMap = { - start: GET_SHORT_URL_VISITS_START, - large: GET_SHORT_URL_VISITS_LARGE, - finish: GET_SHORT_URL_VISITS, - error: GET_SHORT_URL_VISITS_ERROR, - progress: GET_SHORT_URL_VISITS_PROGRESS_CHANGED, - fallbackToInterval: GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL, - }; + const prefix = `${REDUCER_PREFIX}/getShortUrlVisits`; - return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, actionMap, dispatch, shouldCancel); + return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, prefix, dispatch, shouldCancel); }; -export const cancelGetShortUrlVisits = buildActionCreator(GET_SHORT_URL_VISITS_CANCEL); +export const cancelGetShortUrlVisits = createAction(`${REDUCER_PREFIX}/getShortUrlVisits/cancel`); diff --git a/src/visits/reducers/tagVisits.ts b/src/visits/reducers/tagVisits.ts index 65b0bfc8..adb1dff2 100644 --- a/src/visits/reducers/tagVisits.ts +++ b/src/visits/reducers/tagVisits.ts @@ -1,6 +1,7 @@ +import { createAction } from '@reduxjs/toolkit'; import { Action, Dispatch } from 'redux'; import { Visit, VisitsFallbackIntervalAction, VisitsInfo, VisitsLoadProgressChangedAction } from '../types'; -import { buildActionCreator, buildReducer } from '../../utils/helpers/redux'; +import { buildReducer } from '../../utils/helpers/redux'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; import { GetState } from '../../container/types'; import { ShlinkVisitsParams } from '../../api/types'; @@ -9,13 +10,14 @@ import { isBetween } from '../../utils/helpers/date'; import { getVisitsWithLoader, lastVisitLoaderForLoader } from './common'; import { createNewVisits, CreateVisitsAction } from './visitCreation'; -export const GET_TAG_VISITS_START = 'shlink/tagVisits/GET_TAG_VISITS_START'; -export const GET_TAG_VISITS_ERROR = 'shlink/tagVisits/GET_TAG_VISITS_ERROR'; -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_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_FALLBACK_TO_INTERVAL = 'shlink/tagVisits/GET_TAG_VISITS_FALLBACK_TO_INTERVAL'; +const REDUCER_PREFIX = 'shlink/tagVisits'; +export const GET_TAG_VISITS_START = `${REDUCER_PREFIX}/getTagVisits/pending`; +export const GET_TAG_VISITS_ERROR = `${REDUCER_PREFIX}/getTagVisits/rejected`; +export const GET_TAG_VISITS = `${REDUCER_PREFIX}/getTagVisits/fulfilled`; +export const GET_TAG_VISITS_LARGE = `${REDUCER_PREFIX}/getTagVisits/large`; +export const GET_TAG_VISITS_CANCEL = `${REDUCER_PREFIX}/getTagVisits/cancel`; +export const GET_TAG_VISITS_PROGRESS_CHANGED = `${REDUCER_PREFIX}/getTagVisits/progressChanged`; +export const GET_TAG_VISITS_FALLBACK_TO_INTERVAL = `${REDUCER_PREFIX}/getTagVisits/fallbackToInterval`; export interface TagVisits extends VisitsInfo { tag: string; @@ -44,15 +46,17 @@ const initialState: TagVisits = { }; export default buildReducer({ - [GET_TAG_VISITS_START]: () => ({ ...initialState, loading: true }), - [GET_TAG_VISITS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }), - [GET_TAG_VISITS]: (state, { visits, tag, query }) => ( + [`${REDUCER_PREFIX}/getTagVisits/pending`]: () => ({ ...initialState, loading: true }), + [`${REDUCER_PREFIX}/getTagVisits/rejected`]: (_, { errorData }) => ({ ...initialState, error: true, errorData }), + [`${REDUCER_PREFIX}/getTagVisits/fulfilled`]: (state, { visits, tag, query }) => ( { ...state, visits, tag, query, loading: false, loadingLarge: false, error: false } ), - [GET_TAG_VISITS_LARGE]: (state) => ({ ...state, loadingLarge: true }), - [GET_TAG_VISITS_CANCEL]: (state) => ({ ...state, cancelLoad: true }), - [GET_TAG_VISITS_PROGRESS_CHANGED]: (state, { progress }) => ({ ...state, progress }), - [GET_TAG_VISITS_FALLBACK_TO_INTERVAL]: (state, { fallbackInterval }) => ({ ...state, fallbackInterval }), + [`${REDUCER_PREFIX}/getTagVisits/large`]: (state) => ({ ...state, loadingLarge: true }), + [`${REDUCER_PREFIX}/getTagVisits/cancel`]: (state) => ({ ...state, cancelLoad: true }), + [`${REDUCER_PREFIX}/getTagVisits/progressChanged`]: (state, { progress }) => ({ ...state, progress }), + [`${REDUCER_PREFIX}/getTagVisits/fallbackToInterval`]: (state, { fallbackInterval }) => ( + { ...state, fallbackInterval } + ), [createNewVisits.toString()]: (state, { payload }) => { const { tag, visits, query = {} } = state; const { startDate, endDate } = query; @@ -77,16 +81,9 @@ export const getTagVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => ( const lastVisitLoader = lastVisitLoaderForLoader(doIntervalFallback, async (params) => getVisits(tag, params)); const shouldCancel = () => getState().tagVisits.cancelLoad; const extraFinishActionData: Partial = { tag, query }; - const actionMap = { - start: GET_TAG_VISITS_START, - large: GET_TAG_VISITS_LARGE, - finish: GET_TAG_VISITS, - error: GET_TAG_VISITS_ERROR, - progress: GET_TAG_VISITS_PROGRESS_CHANGED, - fallbackToInterval: GET_TAG_VISITS_FALLBACK_TO_INTERVAL, - }; + const prefix = `${REDUCER_PREFIX}/getTagVisits`; - return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, actionMap, dispatch, shouldCancel); + return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, prefix, dispatch, shouldCancel); }; -export const cancelGetTagVisits = buildActionCreator(GET_TAG_VISITS_CANCEL); +export const cancelGetTagVisits = createAction(`${REDUCER_PREFIX}/getTagVisits/cancel`);