Removed unnecesary type castings and improved type inference for actions in demainsListReducer

This commit is contained in:
Alejandro Celaya 2022-11-04 16:50:03 +01:00
parent 79645099ba
commit 4fa6ae493d
2 changed files with 23 additions and 32 deletions

View file

@ -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 { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
import { ShlinkState } from '../../container/types';
import { Domain, DomainStatus } from '../data';
import { hasServerData } from '../../servers/data';
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 { parseApiError } from '../../api/utils';
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 interface DomainsList {
@ -31,15 +32,6 @@ interface ValidateDomain {
status: DomainStatus;
}
type ListDomainsAction = PayloadAction<ListDomains>;
type FilterDomainsAction = PayloadAction<string>;
type ValidateDomainAction = PayloadAction<ValidateDomain>;
export type DomainsCombinedAction = ListDomainsAction
& FilterDomainsAction
& EditDomainRedirectsAction
& ValidateDomainAction;
const initialState: DomainsList = {
domains: [],
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',
initialState,
reducers: {
filterDomains: (state, { payload }) => ({
...state,
filteredDomains: state.domains.filter(({ domain }) => domain.toLowerCase().match(payload.toLowerCase())),
}),
},
reducers: {},
extraReducers: (builder) => {
builder.addCase(listDomains.pending, () => ({ ...initialState, loading: true }));
builder.addCase(listDomains.rejected, (_, { error }) => (
@ -116,6 +105,11 @@ export const domainsListReducerCreator = (buildShlinkApiClient: ShlinkApiClientB
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"
...state,
domains: state.domains.map(replaceRedirectsOnDomain(domain, redirects)),
@ -128,6 +122,6 @@ export const domainsListReducerCreator = (buildShlinkApiClient: ShlinkApiClientB
reducer,
listDomains,
checkDomainHealth,
...actions,
filterDomains,
};
};

View file

@ -1,7 +1,6 @@
import { Mock } from 'ts-mockery';
import { AxiosError } from 'axios';
import {
DomainsCombinedAction,
DomainsList,
replaceRedirectsOnDomain,
replaceStatusOnDomain,
@ -31,7 +30,6 @@ describe('domainsListReducer', () => {
data: { type: 'NOT_FOUND', status: 404 },
},
});
// @ts-expect-error filterDomains is actually part of the result
const { reducer, listDomains: listDomainsAction, checkDomainHealth, filterDomains } = domainsListReducerCreator(
buildShlinkApiClient,
);
@ -39,31 +37,27 @@ describe('domainsListReducer', () => {
beforeEach(jest.clearAllMocks);
describe('reducer', () => {
const action = (type: string, args: Partial<DomainsCombinedAction> = {}) => Mock.of<DomainsCombinedAction>(
{ type, ...args },
);
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 },
);
});
it('returns error on LIST_DOMAINS_ERROR', () => {
expect(reducer(undefined, action(listDomainsAction.rejected.toString(), { error } as any))).toEqual(
{ domains: [], filteredDomains: [], loading: false, error: true, errorData: parseApiError(error as any) },
expect(reducer(undefined, { type: listDomainsAction.rejected.toString(), error })).toEqual(
{ domains: [], filteredDomains: [], loading: false, error: true, errorData: parseApiError(error) },
);
});
it('returns domains on LIST_DOMAINS', () => {
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 });
});
it('filters domains on FILTER_DOMAINS', () => {
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 });
});
@ -80,7 +74,7 @@ describe('domainsListReducer', () => {
expect(reducer(
Mock.of<DomainsList>({ domains, filteredDomains }),
action(EDIT_DOMAIN_REDIRECTS, { domain, redirects }),
{ type: EDIT_DOMAIN_REDIRECTS, domain, redirects },
)).toEqual({
domains: domains.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) => {
expect(reducer(
Mock.of<DomainsList>({ domains, filteredDomains }),
action(checkDomainHealth.fulfilled.toString(), { payload: { domain, status: 'valid' } } as any),
{
type: checkDomainHealth.fulfilled.toString(),
payload: { domain, status: 'valid' },
},
)).toEqual({
domains: domains.map(replaceStatusOnDomain(domain, 'valid')),
filteredDomains: filteredDomains.map(replaceStatusOnDomain(domain, 'valid')),