From 4fa6ae493d15b3b818335869e791ae62292b3d1f Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 4 Nov 2022 16:50:03 +0100 Subject: [PATCH] Removed unnecesary type castings and improved type inference for actions in demainsListReducer --- src/domains/reducers/domainsList.ts | 32 +++++++++-------------- test/domains/reducers/domainsList.test.ts | 23 +++++++--------- 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/domains/reducers/domainsList.ts b/src/domains/reducers/domainsList.ts index 75717ddf..de1df166 100644 --- a/src/domains/reducers/domainsList.ts +++ b/src/domains/reducers/domainsList.ts @@ -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; -type FilterDomainsAction = PayloadAction; -type ValidateDomainAction = PayloadAction; - -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>({ + const filterDomains = createAction(FILTER_DOMAINS); + + const { reducer } = createSlice>({ 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, }; }; diff --git a/test/domains/reducers/domainsList.test.ts b/test/domains/reducers/domainsList.test.ts index f6640dd8..dac4a87d 100644 --- a/test/domains/reducers/domainsList.test.ts +++ b/test/domains/reducers/domainsList.test.ts @@ -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 = {}) => Mock.of( - { 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({ domains }), action(filterDomains.toString(), { payload: 'oO' as any })), + reducer(Mock.of({ domains }), { type: filterDomains.toString(), payload: 'oO' }), ).toEqual({ domains, filteredDomains }); }); @@ -80,7 +74,7 @@ describe('domainsListReducer', () => { expect(reducer( Mock.of({ 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({ 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')),