Split short URL edition reducer and async thunk

This commit is contained in:
Alejandro Celaya 2022-11-09 18:40:51 +01:00
parent 979c16eb9c
commit ae49090bad
3 changed files with 33 additions and 33 deletions

View file

@ -28,31 +28,27 @@ const initialState: ShortUrlEdition = {
error: false, error: false,
}; };
export const shortUrlEditionReducerCreator = (buildShlinkApiClient: ShlinkApiClientBuilder) => { export const editShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => createAsyncThunk(
const editShortUrl = createAsyncThunk( SHORT_URL_EDITED,
SHORT_URL_EDITED, ({ shortCode, domain, data }: EditShortUrl, { getState }): Promise<ShortUrl> => {
({ shortCode, domain, data }: EditShortUrl, { getState }): Promise<ShortUrl> => { const { updateShortUrl } = buildShlinkApiClient(getState);
const { updateShortUrl } = buildShlinkApiClient(getState); return updateShortUrl(shortCode, domain, data as any); // FIXME parse dates
return updateShortUrl(shortCode, domain, data as any); // FIXME parse dates },
}, );
);
const { reducer } = createSlice({ export const shortUrlEditionReducerCreator = (editShortUrlThunk: ReturnType<typeof editShortUrl>) => createSlice({
name: REDUCER_PREFIX, name: REDUCER_PREFIX,
initialState, initialState,
reducers: {}, reducers: {},
extraReducers: (builder) => { extraReducers: (builder) => {
builder.addCase(editShortUrl.pending, (state) => ({ ...state, saving: true, error: false, saved: false })); builder.addCase(editShortUrlThunk.pending, (state) => ({ ...state, saving: true, error: false, saved: false }));
builder.addCase( builder.addCase(
editShortUrl.rejected, editShortUrlThunk.rejected,
(state, { error }) => ({ ...state, saving: false, error: true, saved: false, errorData: parseApiError(error) }), (state, { error }) => ({ ...state, saving: false, error: true, saved: false, errorData: parseApiError(error) }),
); );
builder.addCase( builder.addCase(
editShortUrl.fulfilled, editShortUrlThunk.fulfilled,
(_, { payload: shortUrl }) => ({ shortUrl, saving: false, error: false, saved: true }), (_, { payload: shortUrl }) => ({ shortUrl, saving: false, error: false, saved: true }),
); );
}, },
}); }).reducer;
return { reducer, editShortUrl };
};

View file

@ -10,7 +10,7 @@ import { CreateShortUrlResult } from '../helpers/CreateShortUrlResult';
import { listShortUrls } from '../reducers/shortUrlsList'; import { listShortUrls } from '../reducers/shortUrlsList';
import { shortUrlCreationReducerCreator, createShortUrl } from '../reducers/shortUrlCreation'; import { shortUrlCreationReducerCreator, createShortUrl } from '../reducers/shortUrlCreation';
import { shortUrlDeletionReducerCreator } from '../reducers/shortUrlDeletion'; import { shortUrlDeletionReducerCreator } from '../reducers/shortUrlDeletion';
import { shortUrlEditionReducerCreator } from '../reducers/shortUrlEdition'; import { editShortUrl, shortUrlEditionReducerCreator } from '../reducers/shortUrlEdition';
import { shortUrlDetailReducerCreator } from '../reducers/shortUrlDetail'; import { shortUrlDetailReducerCreator } from '../reducers/shortUrlDetail';
import { ConnectDecorator } from '../../container/types'; import { ConnectDecorator } from '../../container/types';
import { ShortUrlsTable } from '../ShortUrlsTable'; import { ShortUrlsTable } from '../ShortUrlsTable';
@ -60,8 +60,7 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator) => {
bottle.serviceFactory('shortUrlCreationReducerCreator', shortUrlCreationReducerCreator, 'createShortUrl'); bottle.serviceFactory('shortUrlCreationReducerCreator', shortUrlCreationReducerCreator, 'createShortUrl');
bottle.serviceFactory('shortUrlCreationReducer', prop('reducer'), 'shortUrlCreationReducerCreator'); bottle.serviceFactory('shortUrlCreationReducer', prop('reducer'), 'shortUrlCreationReducerCreator');
bottle.serviceFactory('shortUrlEditionReducerCreator', shortUrlEditionReducerCreator, 'buildShlinkApiClient'); bottle.serviceFactory('shortUrlEditionReducer', shortUrlEditionReducerCreator, 'editShortUrl');
bottle.serviceFactory('shortUrlEditionReducer', prop('reducer'), 'shortUrlEditionReducerCreator');
bottle.serviceFactory('shortUrlDeletionReducerCreator', shortUrlDeletionReducerCreator, 'buildShlinkApiClient'); bottle.serviceFactory('shortUrlDeletionReducerCreator', shortUrlDeletionReducerCreator, 'buildShlinkApiClient');
bottle.serviceFactory('shortUrlDeletionReducer', prop('reducer'), 'shortUrlDeletionReducerCreator'); bottle.serviceFactory('shortUrlDeletionReducer', prop('reducer'), 'shortUrlDeletionReducerCreator');
@ -80,7 +79,7 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator) => {
bottle.serviceFactory('getShortUrlDetail', prop('getShortUrlDetail'), 'shortUrlDetailReducerCreator'); bottle.serviceFactory('getShortUrlDetail', prop('getShortUrlDetail'), 'shortUrlDetailReducerCreator');
bottle.serviceFactory('editShortUrl', prop('editShortUrl'), 'shortUrlEditionReducerCreator'); bottle.serviceFactory('editShortUrl', editShortUrl, 'buildShlinkApiClient');
}; };
export default provideServices; export default provideServices;

View file

@ -1,5 +1,9 @@
import { Mock } from 'ts-mockery'; 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 { ShlinkState } from '../../../src/container/types';
import { ShortUrl } from '../../../src/short-urls/data'; import { ShortUrl } from '../../../src/short-urls/data';
import { SelectedServer } from '../../../src/servers/data'; import { SelectedServer } from '../../../src/servers/data';
@ -10,7 +14,8 @@ describe('shortUrlEditionReducer', () => {
const shortUrl = Mock.of<ShortUrl>({ longUrl, shortCode }); const shortUrl = Mock.of<ShortUrl>({ longUrl, shortCode });
const updateShortUrl = jest.fn().mockResolvedValue(shortUrl); const updateShortUrl = jest.fn().mockResolvedValue(shortUrl);
const buildShlinkApiClient = jest.fn().mockReturnValue({ updateShortUrl }); const buildShlinkApiClient = jest.fn().mockReturnValue({ updateShortUrl });
const { reducer, editShortUrl } = shortUrlEditionReducerCreator(buildShlinkApiClient); const editShortUrl = editShortUrlCreator(buildShlinkApiClient);
const reducer = shortUrlEditionReducerCreator(editShortUrl);
afterEach(jest.clearAllMocks); afterEach(jest.clearAllMocks);