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 { 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<void>;
editShortUrl: (editShortUrl: EditShortUrlInfo) => Promise<void>;
}
export const EditShortUrl = (ShortUrlForm: FC<ShortUrlFormProps>) => ({
@ -89,7 +88,7 @@ export const EditShortUrl = (ShortUrlForm: FC<ShortUrlFormProps>) => ({
}
isNotSuccessful();
editShortUrl(shortUrl.shortCode, shortUrl.domain, shortUrlData)
editShortUrl({ ...shortUrl, data: shortUrlData })
.then(isSuccessful)
.catch(isNotSuccessful);
}}

View file

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

View file

@ -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);
}