Migrated create visit action to use payload

This commit is contained in:
Alejandro Celaya 2022-11-05 13:01:00 +01:00
parent cd90d3e581
commit d588d8d9ef
17 changed files with 54 additions and 51 deletions

View file

@ -56,13 +56,13 @@ export default buildReducer<ShortUrlsList, ListShortUrlsCombinedAction>({
state,
)),
),
[CREATE_VISITS]: (state, { createdVisits }) => assocPath(
[CREATE_VISITS]: (state, { payload }) => assocPath(
['shortUrls', 'data'],
state.shortUrls?.data?.map(
(currentShortUrl) => {
// Find the last of the new visit for this short URL, and pick the amount of visits from it
const lastVisit = last(
createdVisits.filter(
payload.createdVisits.filter(
({ shortUrl }) => shortUrl && shortUrlMatches(currentShortUrl, shortUrl.shortCode, shortUrl.domain),
),
);

View file

@ -99,9 +99,9 @@ export default buildReducer<TagsList, TagsCombinedAction>({
...state,
filteredTags: state.tags.filter((tag) => tag.toLowerCase().match(searchTerm.toLowerCase())),
}),
[CREATE_VISITS]: (state, { createdVisits }) => ({
[CREATE_VISITS]: (state, { payload }) => ({
...state,
stats: increaseVisitsForTags(calculateVisitsPerTag(createdVisits), state.stats),
stats: increaseVisitsForTags(calculateVisitsPerTag(payload.createdVisits), state.stats),
}),
[CREATE_SHORT_URL]: ({ tags: stateTags, ...rest }, { result }) => ({
...rest,

View file

@ -56,10 +56,10 @@ export default buildReducer<DomainVisits, DomainVisitsCombinedAction>({
[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 }),
[CREATE_VISITS]: (state, { createdVisits }) => {
[CREATE_VISITS]: (state, { payload }) => {
const { domain, visits, query = {} } = state;
const { startDate, endDate } = query;
const newVisits = createdVisits
const newVisits = payload.createdVisits
.filter(({ shortUrl, visit }) =>
shortUrl && domainMatches(shortUrl, domain) && isBetween(visit.date, startDate, endDate))
.map(({ visit }) => visit);

View file

@ -52,10 +52,10 @@ export default buildReducer<VisitsInfo, NonOrphanVisitsCombinedAction>({
[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 }),
[CREATE_VISITS]: (state, { createdVisits }) => {
[CREATE_VISITS]: (state, { payload }) => {
const { visits, query = {} } = state;
const { startDate, endDate } = query;
const newVisits = createdVisits
const newVisits = payload.createdVisits
.filter(({ visit }) => isBetween(visit.date, startDate, endDate))
.map(({ visit }) => visit);

View file

@ -55,10 +55,10 @@ export default buildReducer<VisitsInfo, OrphanVisitsCombinedAction>({
[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 }),
[CREATE_VISITS]: (state, { createdVisits }) => {
[CREATE_VISITS]: (state, { payload }) => {
const { visits, query = {} } = state;
const { startDate, endDate } = query;
const newVisits = createdVisits
const newVisits = payload.createdVisits
.filter(({ visit, shortUrl }) => !shortUrl && isBetween(visit.date, startDate, endDate))
.map(({ visit }) => visit);

View file

@ -60,10 +60,10 @@ export default buildReducer<ShortUrlVisits, ShortUrlVisitsCombinedAction>({
[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 }),
[CREATE_VISITS]: (state, { createdVisits }) => {
[CREATE_VISITS]: (state, { payload }) => {
const { shortCode, domain, visits, query = {} } = state;
const { startDate, endDate } = query;
const newVisits = createdVisits
const newVisits = payload.createdVisits
.filter(
({ shortUrl, visit }) =>
shortUrl && shortUrlMatches(shortUrl, shortCode, domain) && isBetween(visit.date, startDate, endDate),

View file

@ -53,10 +53,10 @@ export default buildReducer<TagVisits, TagsVisitsCombinedAction>({
[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 }),
[CREATE_VISITS]: (state, { createdVisits }) => {
[CREATE_VISITS]: (state, { payload }) => {
const { tag, visits, query = {} } = state;
const { startDate, endDate } = query;
const newVisits = createdVisits
const newVisits = payload.createdVisits
.filter(({ shortUrl, visit }) => shortUrl?.tags.includes(tag) && isBetween(visit.date, startDate, endDate))
.map(({ visit }) => visit);

View file

@ -1,13 +1,13 @@
import { Action } from 'redux';
import { PayloadAction } from '@reduxjs/toolkit';
import { CreateVisit } from '../types';
export const CREATE_VISITS = 'shlink/visitCreation/CREATE_VISITS';
export interface CreateVisitsAction extends Action<typeof CREATE_VISITS> {
export type CreateVisitsAction = PayloadAction<{
createdVisits: CreateVisit[];
}
}>;
export const createNewVisits = (createdVisits: CreateVisit[]): CreateVisitsAction => ({
type: CREATE_VISITS,
createdVisits,
payload: { createdVisits },
});

View file

@ -30,8 +30,8 @@ export default buildReducer<VisitsOverview, GetVisitsOverviewAction & CreateVisi
[GET_OVERVIEW_START]: () => ({ ...initialState, loading: true }),
[GET_OVERVIEW_ERROR]: () => ({ ...initialState, error: true }),
[GET_OVERVIEW]: (_, { visitsCount, orphanVisitsCount }) => ({ ...initialState, visitsCount, orphanVisitsCount }),
[CREATE_VISITS]: ({ visitsCount, orphanVisitsCount = 0, ...rest }, { createdVisits }) => {
const { regularVisits, orphanVisits } = groupNewVisitsByType(createdVisits);
[CREATE_VISITS]: ({ visitsCount, orphanVisitsCount = 0, ...rest }, { payload }) => {
const { regularVisits, orphanVisits } = groupNewVisitsByType(payload.createdVisits);
return {
...rest,

View file

@ -85,7 +85,7 @@ describe('shortUrlsListReducer', () => {
error: false,
};
expect(reducer(state, { type: CREATE_VISITS, createdVisits } as any)).toEqual({
expect(reducer(state, { type: CREATE_VISITS, payload: { createdVisits } } as any)).toEqual({
shortUrls: {
data: [
{ shortCode, domain: 'example.com', visitsCount: 5 },

View file

@ -135,7 +135,7 @@ describe('domainVisitsReducer', () => {
const { visits } = reducer(prevState, {
type: CREATE_VISITS,
createdVisits: [{ shortUrl, visit: { date: formatIsoDate(now) ?? undefined } }],
payload: { createdVisits: [{ shortUrl, visit: { date: formatIsoDate(now) ?? undefined } }] },
} as any);
expect(visits).toHaveLength(expectedVisits);

View file

@ -105,10 +105,10 @@ describe('nonOrphanVisitsReducer', () => {
const prevState = buildState({ ...state, visits: visitsMocks });
const visit = Mock.of<Visit>({ date: formatIsoDate(now) ?? undefined });
const { visits } = reducer(
prevState,
{ type: CREATE_VISITS, createdVisits: [{ visit }, { visit }] } as any,
);
const { visits } = reducer(prevState, {
type: CREATE_VISITS,
payload: { createdVisits: [{ visit }, { visit }] },
} as any);
expect(visits).toHaveLength(expectedVisits);
});

View file

@ -105,10 +105,10 @@ describe('orphanVisitsReducer', () => {
const prevState = buildState({ ...state, visits: visitsMocks });
const visit = Mock.of<Visit>({ date: formatIsoDate(now) ?? undefined });
const { visits } = reducer(
prevState,
{ type: CREATE_VISITS, createdVisits: [{ visit }, { visit }] } as any,
);
const { visits } = reducer(prevState, {
type: CREATE_VISITS,
payload: { createdVisits: [{ visit }, { visit }] },
} as any);
expect(visits).toHaveLength(expectedVisits);
});

View file

@ -126,10 +126,10 @@ describe('shortUrlVisitsReducer', () => {
visits: visitsMocks,
});
const { visits } = reducer(
prevState,
{ type: CREATE_VISITS, createdVisits: [{ shortUrl, visit: { date: formatIsoDate(now) ?? undefined } }] } as any,
);
const { visits } = reducer(prevState, {
type: CREATE_VISITS,
payload: { createdVisits: [{ shortUrl, visit: { date: formatIsoDate(now) ?? undefined } }] },
} as any);
expect(visits).toHaveLength(expectedVisits);
});

View file

@ -128,7 +128,7 @@ describe('tagVisitsReducer', () => {
const { visits } = reducer(prevState, {
type: CREATE_VISITS,
createdVisits: [{ shortUrl, visit: { date: formatIsoDate(now) ?? undefined } }],
payload: { createdVisits: [{ shortUrl, visit: { date: formatIsoDate(now) ?? undefined } }] },
} as any);
expect(visits).toHaveLength(expectedVisits);

View file

@ -9,9 +9,10 @@ describe('visitCreationReducer', () => {
const visit = Mock.all<Visit>();
it('just returns the action with proper type', () => {
expect(createNewVisits([{ shortUrl, visit }])).toEqual(
{ type: CREATE_VISITS, createdVisits: [{ shortUrl, visit }] },
);
expect(createNewVisits([{ shortUrl, visit }])).toEqual({
type: CREATE_VISITS,
payload: { createdVisits: [{ shortUrl, visit }] },
});
});
});
});

View file

@ -52,19 +52,21 @@ describe('visitsOverviewReducer', () => {
state({ visitsCount: 100, orphanVisitsCount: providedOrphanVisitsCount }),
{
type: CREATE_VISITS,
createdVisits: [
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
],
payload: {
createdVisits: [
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
],
},
} as unknown as GetVisitsOverviewAction & CreateVisitsAction,
);