From bf84e4a2edec166195d20703b99b43d38da77b1e Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 6 Nov 2022 11:53:23 +0100 Subject: [PATCH] Migrated editShortUrl payload action --- src/api/types/actions.ts | 1 + src/short-urls/reducers/shortUrlEdition.ts | 13 ++++++------- src/short-urls/reducers/shortUrlsList.ts | 2 +- src/utils/helpers/redux.ts | 2 ++ test/short-urls/reducers/shortUrlEdition.test.ts | 4 ++-- test/short-urls/reducers/shortUrlsList.test.ts | 2 +- 6 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/api/types/actions.ts b/src/api/types/actions.ts index b60b4c9c..2c8f6d38 100644 --- a/src/api/types/actions.ts +++ b/src/api/types/actions.ts @@ -1,6 +1,7 @@ import { Action } from 'redux'; import { ProblemDetailsError } from './errors'; +/** @deprecated */ export interface ApiErrorAction extends Action { errorData?: ProblemDetailsError; } diff --git a/src/short-urls/reducers/shortUrlEdition.ts b/src/short-urls/reducers/shortUrlEdition.ts index 72bf437d..64d374a5 100644 --- a/src/short-urls/reducers/shortUrlEdition.ts +++ b/src/short-urls/reducers/shortUrlEdition.ts @@ -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 { - shortUrl: ShortUrl; -} +export type ShortUrlEditedAction = PayloadAction; const initialState: ShortUrlEdition = { saving: false, @@ -31,7 +30,7 @@ const initialState: ShortUrlEdition = { export default buildReducer({ [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({ shortUrl, type: SHORT_URL_EDITED }); + dispatch({ payload, type: SHORT_URL_EDITED }); } catch (e: any) { dispatch({ type: EDIT_SHORT_URL_ERROR, errorData: parseApiError(e) }); diff --git a/src/short-urls/reducers/shortUrlsList.ts b/src/short-urls/reducers/shortUrlsList.ts index 7f826ad7..136bd164 100644 --- a/src/short-urls/reducers/shortUrlsList.ts +++ b/src/short-urls/reducers/shortUrlsList.ts @@ -89,7 +89,7 @@ export default buildReducer({ 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; diff --git a/src/utils/helpers/redux.ts b/src/utils/helpers/redux.ts index f8962883..8ae09e05 100644 --- a/src/utils/helpers/redux.ts +++ b/src/utils/helpers/redux.ts @@ -5,6 +5,7 @@ import { ShlinkState } from '../../container/types'; type ActionHandler = (currentState: State, action: AT) => State; type ActionHandlerMap = Record>; +/** @deprecated */ export const buildReducer = (map: ActionHandlerMap, initialState: State) => ( state: State | undefined, action: AT, @@ -16,6 +17,7 @@ export const buildReducer = (map: ActionHandlerMap(type: T) => (): Action => ({ type }); export const createAsyncThunk = ( diff --git a/test/short-urls/reducers/shortUrlEdition.test.ts b/test/short-urls/reducers/shortUrlEdition.test.ts index 313d7556..da67e589 100644 --- a/test/short-urls/reducers/shortUrlEdition.test.ts +++ b/test/short-urls/reducers/shortUrlEdition.test.ts @@ -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 () => { diff --git a/test/short-urls/reducers/shortUrlsList.test.ts b/test/short-urls/reducers/shortUrlsList.test.ts index 54349fb5..bd2dbb93 100644 --- a/test/short-urls/reducers/shortUrlsList.test.ts +++ b/test/short-urls/reducers/shortUrlsList.test.ts @@ -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); });