Migrated editShortUrl payload action

This commit is contained in:
Alejandro Celaya 2022-11-06 11:53:23 +01:00
parent a316366ae9
commit bf84e4a2ed
6 changed files with 13 additions and 11 deletions

View file

@ -1,6 +1,7 @@
import { Action } from 'redux';
import { ProblemDetailsError } from './errors';
/** @deprecated */
export interface ApiErrorAction extends Action<string> {
errorData?: ProblemDetailsError;
}

View file

@ -1,4 +1,5 @@
import { Action, Dispatch } from 'redux';
import { PayloadAction } from '@reduxjs/toolkit';
import { Dispatch } from 'redux';
import { buildReducer } from '../../utils/helpers/redux';
import { GetState } from '../../container/types';
import { OptionalString } from '../../utils/utils';
@ -19,9 +20,7 @@ export interface ShortUrlEdition {
errorData?: ProblemDetailsError;
}
export interface ShortUrlEditedAction extends Action<string> {
shortUrl: ShortUrl;
}
export type ShortUrlEditedAction = PayloadAction<ShortUrl>;
const initialState: ShortUrlEdition = {
saving: false,
@ -31,7 +30,7 @@ const initialState: ShortUrlEdition = {
export default buildReducer<ShortUrlEdition, ShortUrlEditedAction & ApiErrorAction>({
[EDIT_SHORT_URL_START]: (state) => ({ ...state, saving: true, error: false }),
[EDIT_SHORT_URL_ERROR]: (state, { errorData }) => ({ ...state, saving: false, error: true, errorData }),
[SHORT_URL_EDITED]: (_, { shortUrl }) => ({ shortUrl, saving: false, error: false }),
[SHORT_URL_EDITED]: (_, { payload: shortUrl }) => ({ shortUrl, saving: false, error: false }),
}, initialState);
export const editShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
@ -44,9 +43,9 @@ export const editShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
const { updateShortUrl } = buildShlinkApiClient(getState);
try {
const shortUrl = await updateShortUrl(shortCode, domain, data as any); // FIXME parse dates;
const payload = await updateShortUrl(shortCode, domain, data as any); // FIXME parse dates;
dispatch<ShortUrlEditedAction>({ shortUrl, type: SHORT_URL_EDITED });
dispatch<ShortUrlEditedAction>({ payload, type: SHORT_URL_EDITED });
} catch (e: any) {
dispatch<ApiErrorAction>({ type: EDIT_SHORT_URL_ERROR, errorData: parseApiError(e) });

View file

@ -89,7 +89,7 @@ export default buildReducer<ShortUrlsList, ListShortUrlsCombinedAction>({
state,
)),
),
[SHORT_URL_EDITED]: (state, { shortUrl: editedShortUrl }) => (!state.shortUrls ? state : assocPath(
[SHORT_URL_EDITED]: (state, { payload: editedShortUrl }) => (!state.shortUrls ? state : assocPath(
['shortUrls', 'data'],
state.shortUrls.data.map((shortUrl) => {
const { shortCode, domain } = editedShortUrl;

View file

@ -5,6 +5,7 @@ import { ShlinkState } from '../../container/types';
type ActionHandler<State, AT> = (currentState: State, action: AT) => State;
type ActionHandlerMap<State, AT> = Record<string, ActionHandler<State, AT>>;
/** @deprecated */
export const buildReducer = <State, AT extends Action>(map: ActionHandlerMap<State, AT>, initialState: State) => (
state: State | undefined,
action: AT,
@ -16,6 +17,7 @@ export const buildReducer = <State, AT extends Action>(map: ActionHandlerMap<Sta
return actionHandler ? actionHandler(currentState, action) : currentState;
};
/** @deprecated */
export const buildActionCreator = <T extends string>(type: T) => (): Action<T> => ({ type });
export const createAsyncThunk = <Returned, ThunkArg>(

View file

@ -31,7 +31,7 @@ describe('shortUrlEditionReducer', () => {
});
it('returns provided tags and shortCode on SHORT_URL_EDITED', () => {
expect(reducer(undefined, { type: SHORT_URL_EDITED, shortUrl })).toEqual({
expect(reducer(undefined, { type: SHORT_URL_EDITED, payload: shortUrl })).toEqual({
shortUrl,
saving: false,
error: false,
@ -55,7 +55,7 @@ describe('shortUrlEditionReducer', () => {
expect(updateShortUrl).toHaveBeenCalledWith(shortCode, domain, { longUrl });
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_SHORT_URL_START });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SHORT_URL_EDITED, shortUrl });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SHORT_URL_EDITED, payload: shortUrl });
});
it('dispatches error on failure', async () => {

View file

@ -181,7 +181,7 @@ describe('shortUrlsListReducer', () => {
error: false,
};
const result = reducer(state, { type: SHORT_URL_EDITED, shortUrl: editedShortUrl } as any);
const result = reducer(state, { type: SHORT_URL_EDITED, payload: editedShortUrl } as any);
expect(result.shortUrls?.data).toEqual(expectedList);
});