From 77cbb8ebc4ed35a06c219fdc8f43e5b54c65dcec Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 6 Nov 2022 11:59:39 +0100 Subject: [PATCH] Refactored editShortUrl action to require just one param --- src/short-urls/EditShortUrl.tsx | 7 +++---- src/short-urls/reducers/shortUrlEdition.ts | 10 +++++++--- test/short-urls/reducers/shortUrlEdition.test.ts | 4 ++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/short-urls/EditShortUrl.tsx b/src/short-urls/EditShortUrl.tsx index 3c243a25..8445bfc7 100644 --- a/src/short-urls/EditShortUrl.tsx +++ b/src/short-urls/EditShortUrl.tsx @@ -14,8 +14,7 @@ import { ShlinkApiError } from '../api/ShlinkApiError'; import { useGoBack, useToggle } from '../utils/helpers/hooks'; import { ShortUrlFormProps } from './ShortUrlForm'; import { ShortUrlDetail } from './reducers/shortUrlDetail'; -import { EditShortUrlData } from './data'; -import { ShortUrlEdition } from './reducers/shortUrlEdition'; +import { EditShortUrl as EditShortUrlInfo, ShortUrlEdition } from './reducers/shortUrlEdition'; import { shortUrlDataFromShortUrl, urlDecodeShortCode } from './helpers'; interface EditShortUrlConnectProps { @@ -24,7 +23,7 @@ interface EditShortUrlConnectProps { shortUrlDetail: ShortUrlDetail; shortUrlEdition: ShortUrlEdition; getShortUrlDetail: (shortCode: string, domain: OptionalString) => void; - editShortUrl: (shortUrl: string, domain: OptionalString, data: EditShortUrlData) => Promise; + editShortUrl: (editShortUrl: EditShortUrlInfo) => Promise; } export const EditShortUrl = (ShortUrlForm: FC) => ({ @@ -89,7 +88,7 @@ export const EditShortUrl = (ShortUrlForm: FC) => ({ } isNotSuccessful(); - editShortUrl(shortUrl.shortCode, shortUrl.domain, shortUrlData) + editShortUrl({ ...shortUrl, data: shortUrlData }) .then(isSuccessful) .catch(isNotSuccessful); }} diff --git a/src/short-urls/reducers/shortUrlEdition.ts b/src/short-urls/reducers/shortUrlEdition.ts index 64d374a5..e446c33e 100644 --- a/src/short-urls/reducers/shortUrlEdition.ts +++ b/src/short-urls/reducers/shortUrlEdition.ts @@ -20,6 +20,12 @@ export interface ShortUrlEdition { errorData?: ProblemDetailsError; } +export interface EditShortUrl { + shortCode: string; + domain?: OptionalString; + data: EditShortUrlData; +} + export type ShortUrlEditedAction = PayloadAction; const initialState: ShortUrlEdition = { @@ -34,9 +40,7 @@ export default buildReducer ( - shortCode: string, - domain: OptionalString, - data: EditShortUrlData, + { shortCode, domain, data }: EditShortUrl, ) => async (dispatch: Dispatch, getState: GetState) => { dispatch({ type: EDIT_SHORT_URL_START }); diff --git a/test/short-urls/reducers/shortUrlEdition.test.ts b/test/short-urls/reducers/shortUrlEdition.test.ts index da67e589..792b33ab 100644 --- a/test/short-urls/reducers/shortUrlEdition.test.ts +++ b/test/short-urls/reducers/shortUrlEdition.test.ts @@ -48,7 +48,7 @@ describe('shortUrlEditionReducer', () => { afterEach(jest.clearAllMocks); it.each([[undefined], [null], ['example.com']])('dispatches short URL on success', async (domain) => { - await editShortUrl(buildShlinkApiClient)(shortCode, domain, { longUrl })(dispatch, createGetState()); + await editShortUrl(buildShlinkApiClient)({ shortCode, domain, data: { longUrl } })(dispatch, createGetState()); expect(buildShlinkApiClient).toHaveBeenCalledTimes(1); expect(updateShortUrl).toHaveBeenCalledTimes(1); @@ -64,7 +64,7 @@ describe('shortUrlEditionReducer', () => { updateShortUrl.mockRejectedValue(error); try { - await editShortUrl(buildShlinkApiClient)(shortCode, undefined, { longUrl })(dispatch, createGetState()); + await editShortUrl(buildShlinkApiClient)({ shortCode, data: { longUrl } })(dispatch, createGetState()); } catch (e) { expect(e).toBe(error); }