Refactored editShortUrl action to require just one param

This commit is contained in:
Alejandro Celaya 2022-11-06 11:59:39 +01:00
parent bf84e4a2ed
commit 77cbb8ebc4
3 changed files with 12 additions and 9 deletions

View file

@ -14,8 +14,7 @@ import { ShlinkApiError } from '../api/ShlinkApiError';
import { useGoBack, useToggle } from '../utils/helpers/hooks'; import { useGoBack, useToggle } from '../utils/helpers/hooks';
import { ShortUrlFormProps } from './ShortUrlForm'; import { ShortUrlFormProps } from './ShortUrlForm';
import { ShortUrlDetail } from './reducers/shortUrlDetail'; import { ShortUrlDetail } from './reducers/shortUrlDetail';
import { EditShortUrlData } from './data'; import { EditShortUrl as EditShortUrlInfo, ShortUrlEdition } from './reducers/shortUrlEdition';
import { ShortUrlEdition } from './reducers/shortUrlEdition';
import { shortUrlDataFromShortUrl, urlDecodeShortCode } from './helpers'; import { shortUrlDataFromShortUrl, urlDecodeShortCode } from './helpers';
interface EditShortUrlConnectProps { interface EditShortUrlConnectProps {
@ -24,7 +23,7 @@ interface EditShortUrlConnectProps {
shortUrlDetail: ShortUrlDetail; shortUrlDetail: ShortUrlDetail;
shortUrlEdition: ShortUrlEdition; shortUrlEdition: ShortUrlEdition;
getShortUrlDetail: (shortCode: string, domain: OptionalString) => void; getShortUrlDetail: (shortCode: string, domain: OptionalString) => void;
editShortUrl: (shortUrl: string, domain: OptionalString, data: EditShortUrlData) => Promise<void>; editShortUrl: (editShortUrl: EditShortUrlInfo) => Promise<void>;
} }
export const EditShortUrl = (ShortUrlForm: FC<ShortUrlFormProps>) => ({ export const EditShortUrl = (ShortUrlForm: FC<ShortUrlFormProps>) => ({
@ -89,7 +88,7 @@ export const EditShortUrl = (ShortUrlForm: FC<ShortUrlFormProps>) => ({
} }
isNotSuccessful(); isNotSuccessful();
editShortUrl(shortUrl.shortCode, shortUrl.domain, shortUrlData) editShortUrl({ ...shortUrl, data: shortUrlData })
.then(isSuccessful) .then(isSuccessful)
.catch(isNotSuccessful); .catch(isNotSuccessful);
}} }}

View file

@ -20,6 +20,12 @@ export interface ShortUrlEdition {
errorData?: ProblemDetailsError; errorData?: ProblemDetailsError;
} }
export interface EditShortUrl {
shortCode: string;
domain?: OptionalString;
data: EditShortUrlData;
}
export type ShortUrlEditedAction = PayloadAction<ShortUrl>; export type ShortUrlEditedAction = PayloadAction<ShortUrl>;
const initialState: ShortUrlEdition = { const initialState: ShortUrlEdition = {
@ -34,9 +40,7 @@ export default buildReducer<ShortUrlEdition, ShortUrlEditedAction & ApiErrorActi
}, initialState); }, initialState);
export const editShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => ( export const editShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
shortCode: string, { shortCode, domain, data }: EditShortUrl,
domain: OptionalString,
data: EditShortUrlData,
) => async (dispatch: Dispatch, getState: GetState) => { ) => async (dispatch: Dispatch, getState: GetState) => {
dispatch({ type: EDIT_SHORT_URL_START }); dispatch({ type: EDIT_SHORT_URL_START });

View file

@ -48,7 +48,7 @@ describe('shortUrlEditionReducer', () => {
afterEach(jest.clearAllMocks); afterEach(jest.clearAllMocks);
it.each([[undefined], [null], ['example.com']])('dispatches short URL on success', async (domain) => { 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(buildShlinkApiClient).toHaveBeenCalledTimes(1);
expect(updateShortUrl).toHaveBeenCalledTimes(1); expect(updateShortUrl).toHaveBeenCalledTimes(1);
@ -64,7 +64,7 @@ describe('shortUrlEditionReducer', () => {
updateShortUrl.mockRejectedValue(error); updateShortUrl.mockRejectedValue(error);
try { try {
await editShortUrl(buildShlinkApiClient)(shortCode, undefined, { longUrl })(dispatch, createGetState()); await editShortUrl(buildShlinkApiClient)({ shortCode, data: { longUrl } })(dispatch, createGetState());
} catch (e) { } catch (e) {
expect(e).toBe(error); expect(e).toBe(error);
} }