diff --git a/src/short-urls/reducers/shortUrlEdition.ts b/src/short-urls/reducers/shortUrlEdition.ts index 28d1d992..6b9d5342 100644 --- a/src/short-urls/reducers/shortUrlEdition.ts +++ b/src/short-urls/reducers/shortUrlEdition.ts @@ -28,31 +28,27 @@ const initialState: ShortUrlEdition = { error: false, }; -export const shortUrlEditionReducerCreator = (buildShlinkApiClient: ShlinkApiClientBuilder) => { - const editShortUrl = createAsyncThunk( - SHORT_URL_EDITED, - ({ shortCode, domain, data }: EditShortUrl, { getState }): Promise => { - const { updateShortUrl } = buildShlinkApiClient(getState); - return updateShortUrl(shortCode, domain, data as any); // FIXME parse dates - }, - ); +export const editShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => createAsyncThunk( + SHORT_URL_EDITED, + ({ shortCode, domain, data }: EditShortUrl, { getState }): Promise => { + const { updateShortUrl } = buildShlinkApiClient(getState); + return updateShortUrl(shortCode, domain, data as any); // FIXME parse dates + }, +); - const { reducer } = createSlice({ - name: REDUCER_PREFIX, - initialState, - reducers: {}, - extraReducers: (builder) => { - builder.addCase(editShortUrl.pending, (state) => ({ ...state, saving: true, error: false, saved: false })); - builder.addCase( - editShortUrl.rejected, - (state, { error }) => ({ ...state, saving: false, error: true, saved: false, errorData: parseApiError(error) }), - ); - builder.addCase( - editShortUrl.fulfilled, - (_, { payload: shortUrl }) => ({ shortUrl, saving: false, error: false, saved: true }), - ); - }, - }); - - return { reducer, editShortUrl }; -}; +export const shortUrlEditionReducerCreator = (editShortUrlThunk: ReturnType) => createSlice({ + name: REDUCER_PREFIX, + initialState, + reducers: {}, + extraReducers: (builder) => { + builder.addCase(editShortUrlThunk.pending, (state) => ({ ...state, saving: true, error: false, saved: false })); + builder.addCase( + editShortUrlThunk.rejected, + (state, { error }) => ({ ...state, saving: false, error: true, saved: false, errorData: parseApiError(error) }), + ); + builder.addCase( + editShortUrlThunk.fulfilled, + (_, { payload: shortUrl }) => ({ shortUrl, saving: false, error: false, saved: true }), + ); + }, +}).reducer; diff --git a/src/short-urls/services/provideServices.ts b/src/short-urls/services/provideServices.ts index 13b9e2fe..c9911d0d 100644 --- a/src/short-urls/services/provideServices.ts +++ b/src/short-urls/services/provideServices.ts @@ -10,7 +10,7 @@ import { CreateShortUrlResult } from '../helpers/CreateShortUrlResult'; import { listShortUrls } from '../reducers/shortUrlsList'; import { shortUrlCreationReducerCreator, createShortUrl } from '../reducers/shortUrlCreation'; import { shortUrlDeletionReducerCreator } from '../reducers/shortUrlDeletion'; -import { shortUrlEditionReducerCreator } from '../reducers/shortUrlEdition'; +import { editShortUrl, shortUrlEditionReducerCreator } from '../reducers/shortUrlEdition'; import { shortUrlDetailReducerCreator } from '../reducers/shortUrlDetail'; import { ConnectDecorator } from '../../container/types'; import { ShortUrlsTable } from '../ShortUrlsTable'; @@ -60,8 +60,7 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator) => { bottle.serviceFactory('shortUrlCreationReducerCreator', shortUrlCreationReducerCreator, 'createShortUrl'); bottle.serviceFactory('shortUrlCreationReducer', prop('reducer'), 'shortUrlCreationReducerCreator'); - bottle.serviceFactory('shortUrlEditionReducerCreator', shortUrlEditionReducerCreator, 'buildShlinkApiClient'); - bottle.serviceFactory('shortUrlEditionReducer', prop('reducer'), 'shortUrlEditionReducerCreator'); + bottle.serviceFactory('shortUrlEditionReducer', shortUrlEditionReducerCreator, 'editShortUrl'); bottle.serviceFactory('shortUrlDeletionReducerCreator', shortUrlDeletionReducerCreator, 'buildShlinkApiClient'); bottle.serviceFactory('shortUrlDeletionReducer', prop('reducer'), 'shortUrlDeletionReducerCreator'); @@ -80,7 +79,7 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator) => { bottle.serviceFactory('getShortUrlDetail', prop('getShortUrlDetail'), 'shortUrlDetailReducerCreator'); - bottle.serviceFactory('editShortUrl', prop('editShortUrl'), 'shortUrlEditionReducerCreator'); + bottle.serviceFactory('editShortUrl', editShortUrl, 'buildShlinkApiClient'); }; export default provideServices; diff --git a/test/short-urls/reducers/shortUrlEdition.test.ts b/test/short-urls/reducers/shortUrlEdition.test.ts index 7b0ecc43..2e6d8396 100644 --- a/test/short-urls/reducers/shortUrlEdition.test.ts +++ b/test/short-urls/reducers/shortUrlEdition.test.ts @@ -1,5 +1,9 @@ import { Mock } from 'ts-mockery'; -import { ShortUrlEditedAction, shortUrlEditionReducerCreator } from '../../../src/short-urls/reducers/shortUrlEdition'; +import { + ShortUrlEditedAction, + shortUrlEditionReducerCreator, + editShortUrl as editShortUrlCreator, +} from '../../../src/short-urls/reducers/shortUrlEdition'; import { ShlinkState } from '../../../src/container/types'; import { ShortUrl } from '../../../src/short-urls/data'; import { SelectedServer } from '../../../src/servers/data'; @@ -10,7 +14,8 @@ describe('shortUrlEditionReducer', () => { const shortUrl = Mock.of({ longUrl, shortCode }); const updateShortUrl = jest.fn().mockResolvedValue(shortUrl); const buildShlinkApiClient = jest.fn().mockReturnValue({ updateShortUrl }); - const { reducer, editShortUrl } = shortUrlEditionReducerCreator(buildShlinkApiClient); + const editShortUrl = editShortUrlCreator(buildShlinkApiClient); + const reducer = shortUrlEditionReducerCreator(editShortUrl); afterEach(jest.clearAllMocks);