Updated getShortUrlDetail action to use payload action

This commit is contained in:
Alejandro Celaya 2022-11-06 19:06:39 +01:00
parent cf4143e4e2
commit 526d7195bc
2 changed files with 9 additions and 10 deletions

View file

@ -1,4 +1,5 @@
import { Action, Dispatch } from 'redux';
import { PayloadAction } from '@reduxjs/toolkit';
import { Dispatch } from 'redux';
import { ShortUrl } from '../data';
import { buildReducer } from '../../utils/helpers/redux';
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
@ -20,9 +21,7 @@ export interface ShortUrlDetail {
errorData?: ProblemDetailsError;
}
export interface ShortUrlDetailAction extends Action<string> {
shortUrl: ShortUrl;
}
export type ShortUrlDetailAction = PayloadAction<ShortUrl>;
const initialState: ShortUrlDetail = {
loading: false,
@ -32,7 +31,7 @@ const initialState: ShortUrlDetail = {
export default buildReducer<ShortUrlDetail, ShortUrlDetailAction & ApiErrorAction>({
[GET_SHORT_URL_DETAIL_START]: () => ({ loading: true, error: false }),
[GET_SHORT_URL_DETAIL_ERROR]: (_, { errorData }) => ({ loading: false, error: true, errorData }),
[GET_SHORT_URL_DETAIL]: (_, { shortUrl }) => ({ shortUrl, ...initialState }),
[GET_SHORT_URL_DETAIL]: (_, { payload: shortUrl }) => ({ shortUrl, ...initialState }),
}, initialState);
export const getShortUrlDetail = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
@ -43,11 +42,11 @@ export const getShortUrlDetail = (buildShlinkApiClient: ShlinkApiClientBuilder)
try {
const { shortUrlsList } = getState();
const shortUrl = shortUrlsList?.shortUrls?.data.find(
const payload = shortUrlsList?.shortUrls?.data.find(
(url) => shortUrlMatches(url, shortCode, domain),
) ?? await buildShlinkApiClient(getState).getShortUrl(shortCode, domain);
dispatch<ShortUrlDetailAction>({ shortUrl, type: GET_SHORT_URL_DETAIL });
dispatch<ShortUrlDetailAction>({ payload, type: GET_SHORT_URL_DETAIL });
} catch (e: any) {
dispatch<ApiErrorAction>({ type: GET_SHORT_URL_DETAIL_ERROR, errorData: parseApiError(e) });
}

View file

@ -34,7 +34,7 @@ describe('shortUrlDetailReducer', () => {
it('return short URL on GET_SHORT_URL_DETAIL', () => {
const actionShortUrl = Mock.of<ShortUrl>({ longUrl: 'foo', shortCode: 'bar' });
const state = reducer({ loading: true, error: false }, { type: GET_SHORT_URL_DETAIL, shortUrl: actionShortUrl });
const state = reducer({ loading: true, error: false }, { type: GET_SHORT_URL_DETAIL, payload: actionShortUrl });
const { loading, error, shortUrl } = state;
expect(loading).toEqual(false);
@ -84,7 +84,7 @@ describe('shortUrlDetailReducer', () => {
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_DETAIL_START });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_DETAIL, shortUrl: resolvedShortUrl });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_DETAIL, payload: resolvedShortUrl });
expect(ShlinkApiClient.getShortUrl).toHaveBeenCalledTimes(1);
});
@ -103,7 +103,7 @@ describe('shortUrlDetailReducer', () => {
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_DETAIL_START });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_DETAIL, shortUrl: foundShortUrl });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_DETAIL, payload: foundShortUrl });
expect(ShlinkApiClient.getShortUrl).not.toHaveBeenCalled();
});
});