diff --git a/src/domains/reducers/domainRedirects.ts b/src/domains/reducers/domainRedirects.ts index aa97871b..7aea1b15 100644 --- a/src/domains/reducers/domainRedirects.ts +++ b/src/domains/reducers/domainRedirects.ts @@ -1,7 +1,6 @@ -import { createAsyncThunk } from '@reduxjs/toolkit'; +import { createAsyncThunk } from '../../utils/helpers/redux'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; import { ShlinkDomainRedirects } from '../../api/types'; -import { ShlinkState } from '../../container/types'; const EDIT_DOMAIN_REDIRECTS = 'shlink/domainRedirects/EDIT_DOMAIN_REDIRECTS'; @@ -12,11 +11,11 @@ export interface EditDomainRedirects { export const editDomainRedirects = ( buildShlinkApiClient: ShlinkApiClientBuilder, -) => createAsyncThunk( +) => createAsyncThunk( EDIT_DOMAIN_REDIRECTS, - async ({ domain, redirects: domainRedirects }, { getState }) => { + async ({ domain, redirects: providedRedirects }: EditDomainRedirects, { getState }): Promise => { const { editDomainRedirects: shlinkEditDomainRedirects } = buildShlinkApiClient(getState); - const redirects = await shlinkEditDomainRedirects({ domain, ...domainRedirects }); + const redirects = await shlinkEditDomainRedirects({ domain, ...providedRedirects }); return { domain, redirects }; }, diff --git a/src/domains/reducers/domainsList.ts b/src/domains/reducers/domainsList.ts index bf7f8758..80f7bf06 100644 --- a/src/domains/reducers/domainsList.ts +++ b/src/domains/reducers/domainsList.ts @@ -1,7 +1,7 @@ -import { createSlice, createAsyncThunk, createAction, SliceCaseReducers, AsyncThunk } from '@reduxjs/toolkit'; +import { createSlice, createAction, SliceCaseReducers, AsyncThunk } from '@reduxjs/toolkit'; +import { createAsyncThunk } from '../../utils/helpers/redux'; 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'; @@ -49,22 +49,19 @@ export const domainsListReducerCreator = ( buildShlinkApiClient: ShlinkApiClientBuilder, editDomainRedirects: AsyncThunk, ) => { - const listDomains = createAsyncThunk( - LIST_DOMAINS, - async (_, { getState }) => { - const { listDomains: shlinkListDomains } = buildShlinkApiClient(getState); - const { data, defaultRedirects } = await shlinkListDomains(); + const listDomains = createAsyncThunk(LIST_DOMAINS, async (_, { getState }): Promise => { + const { listDomains: shlinkListDomains } = buildShlinkApiClient(getState); + const { data, defaultRedirects } = await shlinkListDomains(); - return { - domains: data.map((domain): Domain => ({ ...domain, status: 'validating' })), - defaultRedirects, - }; - }, - ); + return { + domains: data.map((domain): Domain => ({ ...domain, status: 'validating' })), + defaultRedirects, + }; + }); - const checkDomainHealth = createAsyncThunk( + const checkDomainHealth = createAsyncThunk( VALIDATE_DOMAIN, - async (domain: string, { getState }) => { + async (domain: string, { getState }): Promise => { const { selectedServer } = getState(); if (!hasServerData(selectedServer)) { diff --git a/src/mercure/reducers/mercureInfo.ts b/src/mercure/reducers/mercureInfo.ts index 378c6fa9..6104b232 100644 --- a/src/mercure/reducers/mercureInfo.ts +++ b/src/mercure/reducers/mercureInfo.ts @@ -1,6 +1,6 @@ -import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'; +import { createSlice } from '@reduxjs/toolkit'; +import { createAsyncThunk } from '../../utils/helpers/redux'; import { ShlinkMercureInfo } from '../../api/types'; -import { ShlinkState } from '../../container/types'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; const GET_MERCURE_INFO = 'shlink/mercure/GET_MERCURE_INFO'; @@ -17,17 +17,14 @@ const initialState: MercureInfo = { }; export const mercureInfoReducerCreator = (buildShlinkApiClient: ShlinkApiClientBuilder) => { - const loadMercureInfo = createAsyncThunk( - GET_MERCURE_INFO, - async (_, { getState }) => { - const { settings } = getState(); - if (!settings.realTimeUpdates.enabled) { - throw new Error('Real time updates not enabled'); - } + const loadMercureInfo = createAsyncThunk(GET_MERCURE_INFO, async (_, { getState }): Promise => { + const { settings } = getState(); + if (!settings.realTimeUpdates.enabled) { + throw new Error('Real time updates not enabled'); + } - return buildShlinkApiClient(getState).mercureInfo(); - }, - ); + return buildShlinkApiClient(getState).mercureInfo(); + }); const { reducer } = createSlice({ name: 'mercureInfoReducer', diff --git a/src/utils/helpers/redux.ts b/src/utils/helpers/redux.ts index d2607707..f8962883 100644 --- a/src/utils/helpers/redux.ts +++ b/src/utils/helpers/redux.ts @@ -1,4 +1,6 @@ +import { createAsyncThunk as baseCreateAsyncThunk, AsyncThunkPayloadCreator } from '@reduxjs/toolkit'; import { Action } from 'redux'; +import { ShlinkState } from '../../container/types'; type ActionHandler = (currentState: State, action: AT) => State; type ActionHandlerMap = Record>; @@ -15,3 +17,8 @@ export const buildReducer = (map: ActionHandlerMap(type: T) => (): Action => ({ type }); + +export const createAsyncThunk = ( + typePrefix: string, + payloadCreator: AsyncThunkPayloadCreator, +) => baseCreateAsyncThunk(typePrefix, payloadCreator);