From 526d7195bc0a6c6b48808df213b53ee7c9958f71 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 6 Nov 2022 19:06:39 +0100 Subject: [PATCH] Updated getShortUrlDetail action to use payload action --- src/short-urls/reducers/shortUrlDetail.ts | 13 ++++++------- test/short-urls/reducers/shortUrlDetail.test.ts | 6 +++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/short-urls/reducers/shortUrlDetail.ts b/src/short-urls/reducers/shortUrlDetail.ts index 81ab192c..19c2d888 100644 --- a/src/short-urls/reducers/shortUrlDetail.ts +++ b/src/short-urls/reducers/shortUrlDetail.ts @@ -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 { - shortUrl: ShortUrl; -} +export type ShortUrlDetailAction = PayloadAction; const initialState: ShortUrlDetail = { loading: false, @@ -32,7 +31,7 @@ const initialState: ShortUrlDetail = { export default buildReducer({ [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({ shortUrl, type: GET_SHORT_URL_DETAIL }); + dispatch({ payload, type: GET_SHORT_URL_DETAIL }); } catch (e: any) { dispatch({ type: GET_SHORT_URL_DETAIL_ERROR, errorData: parseApiError(e) }); } diff --git a/test/short-urls/reducers/shortUrlDetail.test.ts b/test/short-urls/reducers/shortUrlDetail.test.ts index 2a6b9df7..5e2237c0 100644 --- a/test/short-urls/reducers/shortUrlDetail.test.ts +++ b/test/short-urls/reducers/shortUrlDetail.test.ts @@ -34,7 +34,7 @@ describe('shortUrlDetailReducer', () => { it('return short URL on GET_SHORT_URL_DETAIL', () => { const actionShortUrl = Mock.of({ 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(); }); });