mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-09 17:57:26 +03:00
Removed unnecesary type castings and improved type inference for actions in demainsListReducer
This commit is contained in:
parent
79645099ba
commit
4fa6ae493d
2 changed files with 23 additions and 32 deletions
|
@ -1,15 +1,16 @@
|
||||||
import { createSlice, PayloadAction, createAsyncThunk, SliceCaseReducers } from '@reduxjs/toolkit';
|
import { createSlice, createAsyncThunk, createAction, SliceCaseReducers } from '@reduxjs/toolkit';
|
||||||
import { ShlinkDomainRedirects } from '../../api/types';
|
import { ShlinkDomainRedirects } from '../../api/types';
|
||||||
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
||||||
import { ShlinkState } from '../../container/types';
|
import { ShlinkState } from '../../container/types';
|
||||||
import { Domain, DomainStatus } from '../data';
|
import { Domain, DomainStatus } from '../data';
|
||||||
import { hasServerData } from '../../servers/data';
|
import { hasServerData } from '../../servers/data';
|
||||||
import { replaceAuthorityFromUri } from '../../utils/helpers/uri';
|
import { replaceAuthorityFromUri } from '../../utils/helpers/uri';
|
||||||
import { EDIT_DOMAIN_REDIRECTS, EditDomainRedirectsAction } from './domainRedirects';
|
import { EDIT_DOMAIN_REDIRECTS } from './domainRedirects';
|
||||||
import { ProblemDetailsError } from '../../api/types/errors';
|
import { ProblemDetailsError } from '../../api/types/errors';
|
||||||
import { parseApiError } from '../../api/utils';
|
import { parseApiError } from '../../api/utils';
|
||||||
|
|
||||||
export const LIST_DOMAINS = 'shlink/domainsList/LIST_DOMAINS';
|
export const LIST_DOMAINS = 'shlink/domainsList/LIST_DOMAINS';
|
||||||
|
export const FILTER_DOMAINS = 'shlink/domainsList/FILTER_DOMAINS';
|
||||||
export const VALIDATE_DOMAIN = 'shlink/domainsList/VALIDATE_DOMAIN';
|
export const VALIDATE_DOMAIN = 'shlink/domainsList/VALIDATE_DOMAIN';
|
||||||
|
|
||||||
export interface DomainsList {
|
export interface DomainsList {
|
||||||
|
@ -31,15 +32,6 @@ interface ValidateDomain {
|
||||||
status: DomainStatus;
|
status: DomainStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
type ListDomainsAction = PayloadAction<ListDomains>;
|
|
||||||
type FilterDomainsAction = PayloadAction<string>;
|
|
||||||
type ValidateDomainAction = PayloadAction<ValidateDomain>;
|
|
||||||
|
|
||||||
export type DomainsCombinedAction = ListDomainsAction
|
|
||||||
& FilterDomainsAction
|
|
||||||
& EditDomainRedirectsAction
|
|
||||||
& ValidateDomainAction;
|
|
||||||
|
|
||||||
const initialState: DomainsList = {
|
const initialState: DomainsList = {
|
||||||
domains: [],
|
domains: [],
|
||||||
filteredDomains: [],
|
filteredDomains: [],
|
||||||
|
@ -92,15 +84,12 @@ export const domainsListReducerCreator = (buildShlinkApiClient: ShlinkApiClientB
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const { actions, reducer } = createSlice<DomainsList, SliceCaseReducers<DomainsList>>({
|
const filterDomains = createAction<string>(FILTER_DOMAINS);
|
||||||
|
|
||||||
|
const { reducer } = createSlice<DomainsList, SliceCaseReducers<DomainsList>>({
|
||||||
name: 'domainsList',
|
name: 'domainsList',
|
||||||
initialState,
|
initialState,
|
||||||
reducers: {
|
reducers: {},
|
||||||
filterDomains: (state, { payload }) => ({
|
|
||||||
...state,
|
|
||||||
filteredDomains: state.domains.filter(({ domain }) => domain.toLowerCase().match(payload.toLowerCase())),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
extraReducers: (builder) => {
|
extraReducers: (builder) => {
|
||||||
builder.addCase(listDomains.pending, () => ({ ...initialState, loading: true }));
|
builder.addCase(listDomains.pending, () => ({ ...initialState, loading: true }));
|
||||||
builder.addCase(listDomains.rejected, (_, { error }) => (
|
builder.addCase(listDomains.rejected, (_, { error }) => (
|
||||||
|
@ -116,6 +105,11 @@ export const domainsListReducerCreator = (buildShlinkApiClient: ShlinkApiClientB
|
||||||
filteredDomains: filteredDomains.map(replaceStatusOnDomain(payload.domain, payload.status)),
|
filteredDomains: filteredDomains.map(replaceStatusOnDomain(payload.domain, payload.status)),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
builder.addCase(filterDomains, (state, { payload }) => ({
|
||||||
|
...state,
|
||||||
|
filteredDomains: state.domains.filter(({ domain }) => domain.toLowerCase().match(payload.toLowerCase())),
|
||||||
|
}));
|
||||||
|
|
||||||
builder.addCase(EDIT_DOMAIN_REDIRECTS, (state, { domain, redirects }: any) => ({ // TODO Fix this "any"
|
builder.addCase(EDIT_DOMAIN_REDIRECTS, (state, { domain, redirects }: any) => ({ // TODO Fix this "any"
|
||||||
...state,
|
...state,
|
||||||
domains: state.domains.map(replaceRedirectsOnDomain(domain, redirects)),
|
domains: state.domains.map(replaceRedirectsOnDomain(domain, redirects)),
|
||||||
|
@ -128,6 +122,6 @@ export const domainsListReducerCreator = (buildShlinkApiClient: ShlinkApiClientB
|
||||||
reducer,
|
reducer,
|
||||||
listDomains,
|
listDomains,
|
||||||
checkDomainHealth,
|
checkDomainHealth,
|
||||||
...actions,
|
filterDomains,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import { Mock } from 'ts-mockery';
|
import { Mock } from 'ts-mockery';
|
||||||
import { AxiosError } from 'axios';
|
import { AxiosError } from 'axios';
|
||||||
import {
|
import {
|
||||||
DomainsCombinedAction,
|
|
||||||
DomainsList,
|
DomainsList,
|
||||||
replaceRedirectsOnDomain,
|
replaceRedirectsOnDomain,
|
||||||
replaceStatusOnDomain,
|
replaceStatusOnDomain,
|
||||||
|
@ -31,7 +30,6 @@ describe('domainsListReducer', () => {
|
||||||
data: { type: 'NOT_FOUND', status: 404 },
|
data: { type: 'NOT_FOUND', status: 404 },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
// @ts-expect-error filterDomains is actually part of the result
|
|
||||||
const { reducer, listDomains: listDomainsAction, checkDomainHealth, filterDomains } = domainsListReducerCreator(
|
const { reducer, listDomains: listDomainsAction, checkDomainHealth, filterDomains } = domainsListReducerCreator(
|
||||||
buildShlinkApiClient,
|
buildShlinkApiClient,
|
||||||
);
|
);
|
||||||
|
@ -39,31 +37,27 @@ describe('domainsListReducer', () => {
|
||||||
beforeEach(jest.clearAllMocks);
|
beforeEach(jest.clearAllMocks);
|
||||||
|
|
||||||
describe('reducer', () => {
|
describe('reducer', () => {
|
||||||
const action = (type: string, args: Partial<DomainsCombinedAction> = {}) => Mock.of<DomainsCombinedAction>(
|
|
||||||
{ type, ...args },
|
|
||||||
);
|
|
||||||
|
|
||||||
it('returns loading on LIST_DOMAINS_START', () => {
|
it('returns loading on LIST_DOMAINS_START', () => {
|
||||||
expect(reducer(undefined, action(listDomainsAction.pending.toString()))).toEqual(
|
expect(reducer(undefined, { type: listDomainsAction.pending.toString() })).toEqual(
|
||||||
{ domains: [], filteredDomains: [], loading: true, error: false },
|
{ domains: [], filteredDomains: [], loading: true, error: false },
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns error on LIST_DOMAINS_ERROR', () => {
|
it('returns error on LIST_DOMAINS_ERROR', () => {
|
||||||
expect(reducer(undefined, action(listDomainsAction.rejected.toString(), { error } as any))).toEqual(
|
expect(reducer(undefined, { type: listDomainsAction.rejected.toString(), error })).toEqual(
|
||||||
{ domains: [], filteredDomains: [], loading: false, error: true, errorData: parseApiError(error as any) },
|
{ domains: [], filteredDomains: [], loading: false, error: true, errorData: parseApiError(error) },
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns domains on LIST_DOMAINS', () => {
|
it('returns domains on LIST_DOMAINS', () => {
|
||||||
expect(
|
expect(
|
||||||
reducer(undefined, action(listDomainsAction.fulfilled.toString(), { payload: { domains } } as any)),
|
reducer(undefined, { type: listDomainsAction.fulfilled.toString(), payload: { domains } }),
|
||||||
).toEqual({ domains, filteredDomains: domains, loading: false, error: false });
|
).toEqual({ domains, filteredDomains: domains, loading: false, error: false });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('filters domains on FILTER_DOMAINS', () => {
|
it('filters domains on FILTER_DOMAINS', () => {
|
||||||
expect(
|
expect(
|
||||||
reducer(Mock.of<DomainsList>({ domains }), action(filterDomains.toString(), { payload: 'oO' as any })),
|
reducer(Mock.of<DomainsList>({ domains }), { type: filterDomains.toString(), payload: 'oO' }),
|
||||||
).toEqual({ domains, filteredDomains });
|
).toEqual({ domains, filteredDomains });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -80,7 +74,7 @@ describe('domainsListReducer', () => {
|
||||||
|
|
||||||
expect(reducer(
|
expect(reducer(
|
||||||
Mock.of<DomainsList>({ domains, filteredDomains }),
|
Mock.of<DomainsList>({ domains, filteredDomains }),
|
||||||
action(EDIT_DOMAIN_REDIRECTS, { domain, redirects }),
|
{ type: EDIT_DOMAIN_REDIRECTS, domain, redirects },
|
||||||
)).toEqual({
|
)).toEqual({
|
||||||
domains: domains.map(replaceRedirectsOnDomain(domain, redirects)),
|
domains: domains.map(replaceRedirectsOnDomain(domain, redirects)),
|
||||||
filteredDomains: filteredDomains.map(replaceRedirectsOnDomain(domain, redirects)),
|
filteredDomains: filteredDomains.map(replaceRedirectsOnDomain(domain, redirects)),
|
||||||
|
@ -94,7 +88,10 @@ describe('domainsListReducer', () => {
|
||||||
])('replaces status on proper domain on VALIDATE_DOMAIN', (domain) => {
|
])('replaces status on proper domain on VALIDATE_DOMAIN', (domain) => {
|
||||||
expect(reducer(
|
expect(reducer(
|
||||||
Mock.of<DomainsList>({ domains, filteredDomains }),
|
Mock.of<DomainsList>({ domains, filteredDomains }),
|
||||||
action(checkDomainHealth.fulfilled.toString(), { payload: { domain, status: 'valid' } } as any),
|
{
|
||||||
|
type: checkDomainHealth.fulfilled.toString(),
|
||||||
|
payload: { domain, status: 'valid' },
|
||||||
|
},
|
||||||
)).toEqual({
|
)).toEqual({
|
||||||
domains: domains.map(replaceStatusOnDomain(domain, 'valid')),
|
domains: domains.map(replaceStatusOnDomain(domain, 'valid')),
|
||||||
filteredDomains: filteredDomains.map(replaceStatusOnDomain(domain, 'valid')),
|
filteredDomains: filteredDomains.map(replaceStatusOnDomain(domain, 'valid')),
|
||||||
|
|
Loading…
Reference in a new issue