From 9718c63cfdf326fd562db17770a0e934e45a3c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadeusz=20Miesi=C4=85c?= <tadeusz.miesiac@gmail.com> Date: Wed, 11 Oct 2023 20:37:04 +0800 Subject: [PATCH] refactor(redux: drawer,drugs,search): minor naming fixes, added selectors --- src/redux/drawer/drawer.reducers.test.ts | 10 +++++----- src/redux/drawer/drawer.reducers.ts | 4 ++-- src/redux/drawer/drawer.selectors.ts | 3 ++- src/redux/drawer/drawer.slice.ts | 2 +- src/redux/drawer/drawer.types.ts | 2 +- src/redux/drugs/drugs.selectors.ts | 5 +++++ src/redux/search/search.slice.ts | 4 ---- src/redux/search/search.types.ts | 6 ------ src/redux/store.ts | 1 + 9 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/redux/drawer/drawer.reducers.test.ts b/src/redux/drawer/drawer.reducers.test.ts index 7701a180..c9aa33db 100644 --- a/src/redux/drawer/drawer.reducers.test.ts +++ b/src/redux/drawer/drawer.reducers.test.ts @@ -5,7 +5,7 @@ import drawerReducer, { openDrawer, closeDrawer } from './drawer.slice'; import type { DrawerState } from './drawer.types'; const INITIAL_STATE: DrawerState = { - open: false, + isOpen: false, drawerName: 'none', }; @@ -38,19 +38,19 @@ describe('drawer reducer', () => { it('should update the store when you click a project info button on the nav bar', async () => { const { type } = await store.dispatch(openDrawer('project-info')); - const { open, drawerName } = store.getState().drawer; + const { isOpen, drawerName } = store.getState().drawer; expect(type).toBe('drawer/openDrawer'); - expect(open).toBe(true); + expect(isOpen).toBe(true); expect(drawerName).toEqual('project-info'); }); it('should update the store when you click the close button on the drawer', async () => { const { type } = await store.dispatch(closeDrawer()); - const { open, drawerName } = store.getState().drawer; + const { isOpen, drawerName } = store.getState().drawer; expect(type).toBe('drawer/closeDrawer'); - expect(open).toBe(false); + expect(isOpen).toBe(false); expect(drawerName).toEqual('none'); }); diff --git a/src/redux/drawer/drawer.reducers.ts b/src/redux/drawer/drawer.reducers.ts index 47b8ef6d..ade3a5c1 100644 --- a/src/redux/drawer/drawer.reducers.ts +++ b/src/redux/drawer/drawer.reducers.ts @@ -3,10 +3,10 @@ import { DrawerState } from '@/redux/drawer/drawer.types'; import { PathName } from '@/types/pathName'; export const openDrawerReducer = (state: DrawerState, action: PayloadAction<PathName>): void => { - state.open = true; + state.isOpen = true; state.drawerName = action.payload; }; export const closeDrawerReducer = (state: DrawerState): void => { - state.open = false; + state.isOpen = false; }; diff --git a/src/redux/drawer/drawer.selectors.ts b/src/redux/drawer/drawer.selectors.ts index a49b98ff..0ae254bd 100644 --- a/src/redux/drawer/drawer.selectors.ts +++ b/src/redux/drawer/drawer.selectors.ts @@ -1,4 +1,5 @@ import { createSelector } from '@reduxjs/toolkit'; import { rootSelector } from '@/redux/root/root.selectors'; -export const drawerDataSelector = createSelector(rootSelector, state => state.drawer); +export const drawerSelector = createSelector(rootSelector, state => state.drawer); +export const isDrawerOpenSelector = createSelector(drawerSelector, state => state.isOpen); diff --git a/src/redux/drawer/drawer.slice.ts b/src/redux/drawer/drawer.slice.ts index 4c71fda1..aa1f2e55 100644 --- a/src/redux/drawer/drawer.slice.ts +++ b/src/redux/drawer/drawer.slice.ts @@ -3,7 +3,7 @@ import { DrawerState } from '@/redux/drawer/drawer.types'; import { openDrawerReducer, closeDrawerReducer } from './drawer.reducers'; const initialState: DrawerState = { - open: false, + isOpen: false, drawerName: 'none', }; diff --git a/src/redux/drawer/drawer.types.ts b/src/redux/drawer/drawer.types.ts index a0ad70b8..8b4bf47a 100644 --- a/src/redux/drawer/drawer.types.ts +++ b/src/redux/drawer/drawer.types.ts @@ -1,4 +1,4 @@ export type DrawerState = { - open: boolean; + isOpen: boolean; drawerName: 'none' | 'search' | 'project-info' | 'plugins' | 'export' | 'legend'; }; diff --git a/src/redux/drugs/drugs.selectors.ts b/src/redux/drugs/drugs.selectors.ts index b67a8cb0..cca30ff2 100644 --- a/src/redux/drugs/drugs.selectors.ts +++ b/src/redux/drugs/drugs.selectors.ts @@ -1,6 +1,11 @@ import { rootSelector } from '@/redux/root/root.selectors'; import { createSelector } from '@reduxjs/toolkit'; +const SIZE_OF_EMPTY_ARRAY = 0; + export const drugsSelector = createSelector(rootSelector, state => state.drugs); export const loadingDrugsStatusSelector = createSelector(drugsSelector, state => state.loading); +export const numberOfDrugsSelector = createSelector(drugsSelector, state => + state.data ? state.data.length : SIZE_OF_EMPTY_ARRAY, +); diff --git a/src/redux/search/search.slice.ts b/src/redux/search/search.slice.ts index 73930e33..360bfa68 100644 --- a/src/redux/search/search.slice.ts +++ b/src/redux/search/search.slice.ts @@ -4,10 +4,6 @@ import { createSlice } from '@reduxjs/toolkit'; const initialState: SearchState = { searchValue: '', - searchResult: { - content: '', - drugs: '', - }, loading: 'idle', }; diff --git a/src/redux/search/search.types.ts b/src/redux/search/search.types.ts index 1faf1b72..45380f01 100644 --- a/src/redux/search/search.types.ts +++ b/src/redux/search/search.types.ts @@ -1,12 +1,6 @@ import { Loading } from '@/types/loadingState'; -export interface SearchResult { - content: string; - drugs: string; -} - export interface SearchState { searchValue: string; - searchResult: SearchResult; loading: Loading; } diff --git a/src/redux/store.ts b/src/redux/store.ts index a095912b..e981a8a0 100644 --- a/src/redux/store.ts +++ b/src/redux/store.ts @@ -20,6 +20,7 @@ export const store = configureStore({ devTools: true, }); +export type StoreType = typeof store; // Infer the `RootState` and `AppDispatch` types from the store itself export type RootState = ReturnType<typeof store.getState>; // Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState} -- GitLab