mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-12 10:33:49 +00:00
Replaced redux action to create one visit by action that allows multiple visits at once
This commit is contained in:
@@ -7,7 +7,7 @@ import { ShlinkApiClientBuilder } from '../../utils/services/ShlinkApiClientBuil
|
||||
import { GetState } from '../../container/types';
|
||||
import { OptionalString } from '../../utils/utils';
|
||||
import { getVisitsWithLoader } from './common';
|
||||
import { CREATE_VISIT, CreateVisitAction } from './visitCreation';
|
||||
import { CREATE_VISITS, CreateVisitsAction } from './visitCreation';
|
||||
|
||||
/* eslint-disable padding-line-between-statements */
|
||||
export const GET_SHORT_URL_VISITS_START = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_START';
|
||||
@@ -24,7 +24,7 @@ interface ShortUrlVisitsAction extends Action<string>, ShortUrlIdentifier {
|
||||
visits: Visit[];
|
||||
}
|
||||
|
||||
type ShortUrlVisitsCombinedAction = ShortUrlVisitsAction & VisitsLoadProgressChangedAction & CreateVisitAction;
|
||||
type ShortUrlVisitsCombinedAction = ShortUrlVisitsAction & VisitsLoadProgressChangedAction & CreateVisitsAction;
|
||||
|
||||
const initialState: ShortUrlVisits = {
|
||||
visits: [],
|
||||
@@ -49,14 +49,14 @@ export default buildReducer<ShortUrlVisits, ShortUrlVisitsCombinedAction>({
|
||||
[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 }),
|
||||
[CREATE_VISIT]: (state, { shortUrl, visit }) => { // eslint-disable-line object-shorthand
|
||||
[CREATE_VISITS]: (state, { createdVisits }) => { // eslint-disable-line object-shorthand
|
||||
const { shortCode, domain, visits } = state;
|
||||
|
||||
if (!shortUrlMatches(shortUrl, shortCode, domain)) {
|
||||
return state;
|
||||
}
|
||||
const newVisits = createdVisits
|
||||
.filter(({ shortUrl }) => shortUrlMatches(shortUrl, shortCode, domain))
|
||||
.map(({ visit }) => visit);
|
||||
|
||||
return { ...state, visits: [ ...visits, visit ] };
|
||||
return { ...state, visits: [ ...visits, ...newVisits ] };
|
||||
},
|
||||
}, initialState);
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
|
||||
import { ShlinkApiClientBuilder } from '../../utils/services/ShlinkApiClientBuilder';
|
||||
import { GetState } from '../../container/types';
|
||||
import { getVisitsWithLoader } from './common';
|
||||
import { CREATE_VISIT, CreateVisitAction } from './visitCreation';
|
||||
import { CREATE_VISITS, CreateVisitsAction } from './visitCreation';
|
||||
|
||||
/* eslint-disable padding-line-between-statements */
|
||||
export const GET_TAG_VISITS_START = 'shlink/tagVisits/GET_TAG_VISITS_START';
|
||||
@@ -34,21 +34,20 @@ const initialState: TagVisits = {
|
||||
progress: 0,
|
||||
};
|
||||
|
||||
export default buildReducer<TagVisits, TagVisitsAction & VisitsLoadProgressChangedAction & CreateVisitAction>({
|
||||
export default buildReducer<TagVisits, TagVisitsAction & VisitsLoadProgressChangedAction & CreateVisitsAction>({
|
||||
[GET_TAG_VISITS_START]: () => ({ ...initialState, loading: true }),
|
||||
[GET_TAG_VISITS_ERROR]: () => ({ ...initialState, error: true }),
|
||||
[GET_TAG_VISITS]: (_, { visits, tag }) => ({ ...initialState, visits, tag }),
|
||||
[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 }),
|
||||
[CREATE_VISIT]: (state, { shortUrl, visit }) => { // eslint-disable-line object-shorthand
|
||||
[CREATE_VISITS]: (state, { createdVisits }) => { // eslint-disable-line object-shorthand
|
||||
const { tag, visits } = state;
|
||||
const newVisits = createdVisits
|
||||
.filter(({ shortUrl }) => shortUrl.tags.includes(tag))
|
||||
.map(({ visit }) => visit);
|
||||
|
||||
if (!shortUrl.tags.includes(tag)) {
|
||||
return state;
|
||||
}
|
||||
|
||||
return { ...state, visits: [ ...visits, visit ] };
|
||||
return { ...state, visits: [ ...visits, ...newVisits ] };
|
||||
},
|
||||
}, initialState);
|
||||
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { Action } from 'redux';
|
||||
import { CreateVisit } from '../types';
|
||||
|
||||
export const CREATE_VISIT = 'shlink/visitCreation/CREATE_VISIT';
|
||||
export const CREATE_VISITS = 'shlink/visitCreation/CREATE_VISITS';
|
||||
|
||||
export type CreateVisitAction = Action<typeof CREATE_VISIT> & CreateVisit;
|
||||
export interface CreateVisitsAction extends Action<typeof CREATE_VISITS> {
|
||||
createdVisits: CreateVisit[];
|
||||
}
|
||||
|
||||
export const createNewVisit = ({ shortUrl, visit }: CreateVisit): CreateVisitAction => ({
|
||||
type: CREATE_VISIT,
|
||||
shortUrl,
|
||||
visit,
|
||||
export const createNewVisits = (createdVisits: CreateVisit[]): CreateVisitsAction => ({
|
||||
type: CREATE_VISITS,
|
||||
createdVisits,
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@ import ShortUrlVisits from '../ShortUrlVisits';
|
||||
import { cancelGetShortUrlVisits, getShortUrlVisits } from '../reducers/shortUrlVisits';
|
||||
import { getShortUrlDetail } from '../reducers/shortUrlDetail';
|
||||
import MapModal from '../helpers/MapModal';
|
||||
import { createNewVisit } from '../reducers/visitCreation';
|
||||
import { createNewVisits } from '../reducers/visitCreation';
|
||||
import { cancelGetTagVisits, getTagVisits } from '../reducers/tagVisits';
|
||||
import TagVisits from '../TagVisits';
|
||||
import { ConnectDecorator } from '../../container/types';
|
||||
@@ -15,12 +15,12 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator) => {
|
||||
bottle.serviceFactory('ShortUrlVisits', () => ShortUrlVisits);
|
||||
bottle.decorator('ShortUrlVisits', connect(
|
||||
[ 'shortUrlVisits', 'shortUrlDetail', 'mercureInfo' ],
|
||||
[ 'getShortUrlVisits', 'getShortUrlDetail', 'cancelGetShortUrlVisits', 'createNewVisit', 'loadMercureInfo' ],
|
||||
[ 'getShortUrlVisits', 'getShortUrlDetail', 'cancelGetShortUrlVisits', 'createNewVisits', 'loadMercureInfo' ],
|
||||
));
|
||||
bottle.serviceFactory('TagVisits', TagVisits, 'ColorGenerator');
|
||||
bottle.decorator('TagVisits', connect(
|
||||
[ 'tagVisits', 'mercureInfo' ],
|
||||
[ 'getTagVisits', 'cancelGetTagVisits', 'createNewVisit', 'loadMercureInfo' ],
|
||||
[ 'getTagVisits', 'cancelGetTagVisits', 'createNewVisits', 'loadMercureInfo' ],
|
||||
));
|
||||
|
||||
// Services
|
||||
@@ -34,7 +34,7 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator) => {
|
||||
bottle.serviceFactory('getTagVisits', getTagVisits, 'buildShlinkApiClient');
|
||||
bottle.serviceFactory('cancelGetTagVisits', () => cancelGetTagVisits);
|
||||
|
||||
bottle.serviceFactory('createNewVisit', () => createNewVisit);
|
||||
bottle.serviceFactory('createNewVisits', () => createNewVisits);
|
||||
};
|
||||
|
||||
export default provideServices;
|
||||
|
||||
Reference in New Issue
Block a user