From 275aee4de26f3a84a0e57280b5fb911398ba67ca Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 24 Dec 2021 13:39:51 +0100 Subject: [PATCH] Removed shortUrlsListParams reducer, as the state is now handled internally in the component --- src/container/types.ts | 2 -- src/reducers/index.ts | 2 -- src/servers/reducers/selectedServer.ts | 2 -- src/short-urls/ShortUrlsList.tsx | 10 ++------ .../reducers/shortUrlsListParams.ts | 21 ----------------- src/short-urls/services/provideServices.ts | 6 ++--- test/servers/reducers/selectedServer.test.ts | 10 ++++---- test/short-urls/ShortUrlsList.test.tsx | 6 ++--- .../reducers/shortUrlsListParams.test.ts | 23 ------------------- 9 files changed, 10 insertions(+), 72 deletions(-) delete mode 100644 test/short-urls/reducers/shortUrlsListParams.test.ts diff --git a/src/container/types.ts b/src/container/types.ts index f4d20282..e3289da8 100644 --- a/src/container/types.ts +++ b/src/container/types.ts @@ -4,7 +4,6 @@ import { Settings } from '../settings/reducers/settings'; import { ShortUrlCreation } from '../short-urls/reducers/shortUrlCreation'; import { ShortUrlDeletion } from '../short-urls/reducers/shortUrlDeletion'; import { ShortUrlEdition } from '../short-urls/reducers/shortUrlEdition'; -import { ShortUrlsListParams } from '../short-urls/reducers/shortUrlsListParams'; import { ShortUrlsList } from '../short-urls/reducers/shortUrlsList'; import { TagDeletion } from '../tags/reducers/tagDelete'; import { TagEdition } from '../tags/reducers/tagEdit'; @@ -20,7 +19,6 @@ export interface ShlinkState { servers: ServersMap; selectedServer: SelectedServer; shortUrlsList: ShortUrlsList; - shortUrlsListParams: ShortUrlsListParams; shortUrlCreationResult: ShortUrlCreation; shortUrlDeletion: ShortUrlDeletion; shortUrlEdition: ShortUrlEdition; diff --git a/src/reducers/index.ts b/src/reducers/index.ts index 2257cdda..3c85f168 100644 --- a/src/reducers/index.ts +++ b/src/reducers/index.ts @@ -2,7 +2,6 @@ import { combineReducers } from 'redux'; import serversReducer from '../servers/reducers/servers'; import selectedServerReducer from '../servers/reducers/selectedServer'; import shortUrlsListReducer from '../short-urls/reducers/shortUrlsList'; -import shortUrlsListParamsReducer from '../short-urls/reducers/shortUrlsListParams'; import shortUrlCreationReducer from '../short-urls/reducers/shortUrlCreation'; import shortUrlDeletionReducer from '../short-urls/reducers/shortUrlDeletion'; import shortUrlEditionReducer from '../short-urls/reducers/shortUrlEdition'; @@ -24,7 +23,6 @@ export default combineReducers({ servers: serversReducer, selectedServer: selectedServerReducer, shortUrlsList: shortUrlsListReducer, - shortUrlsListParams: shortUrlsListParamsReducer, shortUrlCreationResult: shortUrlCreationReducer, shortUrlDeletion: shortUrlDeletionReducer, shortUrlEdition: shortUrlEditionReducer, diff --git a/src/servers/reducers/selectedServer.ts b/src/servers/reducers/selectedServer.ts index 38c7c133..b531547c 100644 --- a/src/servers/reducers/selectedServer.ts +++ b/src/servers/reducers/selectedServer.ts @@ -1,6 +1,5 @@ import { identity, memoizeWith, pipe } from 'ramda'; import { Action, Dispatch } from 'redux'; -import { resetShortUrlParams } from '../../short-urls/reducers/shortUrlsListParams'; import { versionToPrintable, versionToSemVer as toSemVer } from '../../utils/helpers/version'; import { SelectedServer } from '../data'; import { GetState } from '../../container/types'; @@ -53,7 +52,6 @@ export const selectServer = ( getState: GetState, ) => { dispatch(resetSelectedServer()); - dispatch(resetShortUrlParams()); const { servers } = getState(); const selectedServer = servers[serverId]; diff --git a/src/short-urls/ShortUrlsList.tsx b/src/short-urls/ShortUrlsList.tsx index fe0623e7..dc81d6d4 100644 --- a/src/short-urls/ShortUrlsList.tsx +++ b/src/short-urls/ShortUrlsList.tsx @@ -11,7 +11,7 @@ import { TableOrderIcon } from '../utils/table/TableOrderIcon'; import { ShlinkShortUrlsListParams } from '../api/types'; import { DEFAULT_SHORT_URLS_ORDERING, Settings } from '../settings/reducers/settings'; import { ShortUrlsList as ShortUrlsListState } from './reducers/shortUrlsList'; -import { OrderableFields, ShortUrlsListParams, ShortUrlsOrder, SORTABLE_FIELDS } from './reducers/shortUrlsListParams'; +import { OrderableFields, ShortUrlsOrder, SORTABLE_FIELDS } from './reducers/shortUrlsListParams'; import { ShortUrlsTableProps } from './ShortUrlsTable'; import Paginator from './Paginator'; import { ShortUrlListRouteParams, useShortUrlsQuery } from './helpers/hooks'; @@ -20,15 +20,11 @@ interface ShortUrlsListProps extends RouteComponentProps void; - shortUrlsListParams: ShortUrlsListParams; - resetShortUrlParams: () => void; settings: Settings; } const ShortUrlsList = (ShortUrlsTable: FC, SearchBar: FC) => boundToMercureHub(({ listShortUrls, - resetShortUrlParams, - shortUrlsListParams, match, location, history, @@ -37,8 +33,7 @@ const ShortUrlsList = (ShortUrlsTable: FC, SearchBar: FC) = settings, }: ShortUrlsListProps) => { const serverId = getServerId(selectedServer); - const { orderBy } = shortUrlsListParams; - const initialOrderBy = orderBy ?? settings.shortUrlList?.defaultOrdering ?? DEFAULT_SHORT_URLS_ORDERING; + const initialOrderBy = settings.shortUrlList?.defaultOrdering ?? DEFAULT_SHORT_URLS_ORDERING; const [ order, setOrder ] = useState(initialOrderBy); const [{ tags, search, startDate, endDate }, toFirstPage ] = useShortUrlsQuery({ history, match, location }); const selectedTags = useMemo(() => tags?.split(',') ?? [], [ tags ]); @@ -53,7 +48,6 @@ const ShortUrlsList = (ShortUrlsTable: FC, SearchBar: FC) = (tags) => toFirstPage({ tags }), ); - useEffect(() => resetShortUrlParams, []); useEffect(() => { listShortUrls({ page: match.params.page, diff --git a/src/short-urls/reducers/shortUrlsListParams.ts b/src/short-urls/reducers/shortUrlsListParams.ts index f2c2b0bc..299f9d1a 100644 --- a/src/short-urls/reducers/shortUrlsListParams.ts +++ b/src/short-urls/reducers/shortUrlsListParams.ts @@ -1,8 +1,4 @@ -import { buildActionCreator, buildReducer } from '../../utils/helpers/redux'; import { Order } from '../../utils/helpers/ordering'; -import { LIST_SHORT_URLS, ListShortUrlsAction } from './shortUrlsList'; - -export const RESET_SHORT_URL_PARAMS = 'shlink/shortUrlsListParams/RESET_SHORT_URL_PARAMS'; export const SORTABLE_FIELDS = { dateCreated: 'Created at', @@ -15,20 +11,3 @@ export const SORTABLE_FIELDS = { export type OrderableFields = keyof typeof SORTABLE_FIELDS; export type ShortUrlsOrder = Order; - -export interface ShortUrlsListParams { - page?: string; - itemsPerPage?: number; - orderBy?: ShortUrlsOrder; -} - -const initialState: ShortUrlsListParams = { - page: '1', -}; - -export default buildReducer({ - [LIST_SHORT_URLS]: (state, { params }) => ({ ...state, ...params }), - [RESET_SHORT_URL_PARAMS]: () => initialState, -}, initialState); - -export const resetShortUrlParams = buildActionCreator(RESET_SHORT_URL_PARAMS); diff --git a/src/short-urls/services/provideServices.ts b/src/short-urls/services/provideServices.ts index 2394ddd5..45b745f2 100644 --- a/src/short-urls/services/provideServices.ts +++ b/src/short-urls/services/provideServices.ts @@ -9,7 +9,6 @@ import CreateShortUrlResult from '../helpers/CreateShortUrlResult'; import { listShortUrls } from '../reducers/shortUrlsList'; import { createShortUrl, resetCreateShortUrl } from '../reducers/shortUrlCreation'; import { deleteShortUrl, resetDeleteShortUrl } from '../reducers/shortUrlDeletion'; -import { resetShortUrlParams } from '../reducers/shortUrlsListParams'; import { editShortUrl } from '../reducers/shortUrlEdition'; import { ConnectDecorator } from '../../container/types'; import { ShortUrlsTable } from '../ShortUrlsTable'; @@ -22,8 +21,8 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator, withRouter: // Components bottle.serviceFactory('ShortUrlsList', ShortUrlsList, 'ShortUrlsTable', 'SearchBar'); bottle.decorator('ShortUrlsList', connect( - [ 'selectedServer', 'shortUrlsListParams', 'mercureInfo', 'shortUrlsList', 'settings' ], - [ 'listShortUrls', 'resetShortUrlParams', 'createNewVisits', 'loadMercureInfo' ], + [ 'selectedServer', 'mercureInfo', 'shortUrlsList', 'settings' ], + [ 'listShortUrls', 'createNewVisits', 'loadMercureInfo' ], )); bottle.serviceFactory('ShortUrlsTable', ShortUrlsTable, 'ShortUrlsRow'); @@ -56,7 +55,6 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator, withRouter: // Actions bottle.serviceFactory('listShortUrls', listShortUrls, 'buildShlinkApiClient'); - bottle.serviceFactory('resetShortUrlParams', () => resetShortUrlParams); bottle.serviceFactory('createShortUrl', createShortUrl, 'buildShlinkApiClient'); bottle.serviceFactory('resetCreateShortUrl', () => resetCreateShortUrl); diff --git a/test/servers/reducers/selectedServer.test.ts b/test/servers/reducers/selectedServer.test.ts index 030b17f4..acd896ef 100644 --- a/test/servers/reducers/selectedServer.test.ts +++ b/test/servers/reducers/selectedServer.test.ts @@ -8,7 +8,6 @@ import reducer, { MAX_FALLBACK_VERSION, MIN_FALLBACK_VERSION, } from '../../../src/servers/reducers/selectedServer'; -import { RESET_SHORT_URL_PARAMS } from '../../../src/short-urls/reducers/shortUrlsListParams'; import { ShlinkState } from '../../../src/container/types'; import { NonReachableServer, NotFoundServer, RegularServer } from '../../../src/servers/data'; @@ -62,10 +61,9 @@ describe('selectedServerReducer', () => { await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState); - expect(dispatch).toHaveBeenCalledTimes(4); + expect(dispatch).toHaveBeenCalledTimes(3); expect(dispatch).toHaveBeenNthCalledWith(1, { type: RESET_SELECTED_SERVER }); - expect(dispatch).toHaveBeenNthCalledWith(2, { type: RESET_SHORT_URL_PARAMS }); - expect(dispatch).toHaveBeenNthCalledWith(3, { type: SELECT_SERVER, selectedServer: expectedSelectedServer }); + expect(dispatch).toHaveBeenNthCalledWith(2, { type: SELECT_SERVER, selectedServer: expectedSelectedServer }); expect(loadMercureInfo).toHaveBeenCalledTimes(1); }); @@ -89,7 +87,7 @@ describe('selectedServerReducer', () => { await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState); expect(apiClientMock.health).toHaveBeenCalled(); - expect(dispatch).toHaveBeenNthCalledWith(3, { type: SELECT_SERVER, selectedServer: expectedSelectedServer }); + expect(dispatch).toHaveBeenNthCalledWith(2, { type: SELECT_SERVER, selectedServer: expectedSelectedServer }); expect(loadMercureInfo).not.toHaveBeenCalled(); }); @@ -102,7 +100,7 @@ describe('selectedServerReducer', () => { expect(getState).toHaveBeenCalled(); expect(apiClientMock.health).not.toHaveBeenCalled(); - expect(dispatch).toHaveBeenNthCalledWith(3, { type: SELECT_SERVER, selectedServer: expectedSelectedServer }); + expect(dispatch).toHaveBeenNthCalledWith(2, { type: SELECT_SERVER, selectedServer: expectedSelectedServer }); expect(loadMercureInfo).not.toHaveBeenCalled(); }); }); diff --git a/test/short-urls/ShortUrlsList.test.tsx b/test/short-urls/ShortUrlsList.test.tsx index 71a1dd5f..8ceadcbc 100644 --- a/test/short-urls/ShortUrlsList.test.tsx +++ b/test/short-urls/ShortUrlsList.test.tsx @@ -33,18 +33,16 @@ describe('', () => { }, }); const ShortUrlsList = shortUrlsListCreator(ShortUrlsTable, SearchBar); - const createWrapper = (orderBy: ShortUrlsOrder = {}) => shallow( + const createWrapper = (defaultOrdering: ShortUrlsOrder = {}) => shallow( ({ mercureInfo: { loading: true } })} listShortUrls={listShortUrlsMock} - resetShortUrlParams={jest.fn()} - shortUrlsListParams={{ page: '1', orderBy }} match={Mock.of>({ params: {} })} location={Mock.of({ search: '?tags=test%20tag&search=example.com' })} shortUrlsList={shortUrlsList} history={Mock.of({ push })} selectedServer={Mock.of({ id: '1' })} - settings={Mock.all()} + settings={Mock.of({ shortUrlList: { defaultOrdering } })} />, ).dive(); // Dive is needed as this component is wrapped in a HOC diff --git a/test/short-urls/reducers/shortUrlsListParams.test.ts b/test/short-urls/reducers/shortUrlsListParams.test.ts deleted file mode 100644 index 6acace25..00000000 --- a/test/short-urls/reducers/shortUrlsListParams.test.ts +++ /dev/null @@ -1,23 +0,0 @@ -import reducer, { - RESET_SHORT_URL_PARAMS, - resetShortUrlParams, -} from '../../../src/short-urls/reducers/shortUrlsListParams'; -import { LIST_SHORT_URLS } from '../../../src/short-urls/reducers/shortUrlsList'; - -describe('shortUrlsListParamsReducer', () => { - describe('reducer', () => { - it('returns params when action is LIST_SHORT_URLS', () => - expect(reducer(undefined, { type: LIST_SHORT_URLS, params: { searchTerm: 'foo', page: '2' } } as any)).toEqual({ - page: '2', - searchTerm: 'foo', - })); - - it('returns default value when action is RESET_SHORT_URL_PARAMS', () => - expect(reducer(undefined, { type: RESET_SHORT_URL_PARAMS } as any)).toEqual({ page: '1' })); - }); - - describe('resetShortUrlParams', () => { - it('returns proper action', () => - expect(resetShortUrlParams()).toEqual({ type: RESET_SHORT_URL_PARAMS })); - }); -});