Skip to content

Commit

Permalink
perf(state): use redux-toolkit and redux hooks instead of connect
Browse files Browse the repository at this point in the history
- improve redux selectors
- move current chapter state into user state
- uninstall package [email protected]
  • Loading branch information
SimonGolms committed Jan 1, 2021
1 parent ed6daf2 commit 369e47d
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 107 deletions.
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"fitty": "^2.3.3",
"i18next": "19.8.3",
"i18next-browser-languagedetector": "6.0.1",
"immer": "7.0.14",
"ionicons": "5.2.3",
"lunr": "^2.3.9",
"react": "17.0.1",
Expand Down
5 changes: 1 addition & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ import AboutPage from './pages/About';
import { SearchPage } from './pages/Search';
import { THEMES } from './utils/theme';
import { selectCurrentTheme } from './data/user/user.selector';
import { RootState } from './store';

const App: React.FC = () => {
const currentTheme = useSelector((state) =>
selectCurrentTheme(state as RootState),
);
const currentTheme = useSelector(selectCurrentTheme);

return (
<Suspense fallback="<App Suspense Loading>">
Expand Down
13 changes: 4 additions & 9 deletions src/components/Chapters/footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ import { arrowBack, arrowForward, star, starOutline } from 'ionicons/icons';
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory } from 'react-router';
import {
addFavorite,
removeFavorite,
} from '../../data/chapter/chapter.actions';
import { selectIsChapterFavorite } from '../../data/chapter/chapter.select';
import { RootState } from '../../store';
import { selectIsFavorite } from '../../data/user/user.selector';
import { addFavorite, removeFavorite } from '../../data/user/user.slice';

type ContainerProps = {
previousChapter: string;
Expand All @@ -27,9 +23,8 @@ export const ChapterFooter: React.FC<ContainerProps> = (props) => {

const history = useHistory();
const currentChapter = history.location.pathname;
const isFavorite = useSelector((state) =>
selectIsChapterFavorite(state as RootState, currentChapter),
);

const isFavorite = useSelector(selectIsFavorite(currentChapter));
const dispatch = useDispatch();

const toggleFavorite = () => {
Expand Down
5 changes: 1 addition & 4 deletions src/components/SearchPopover/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
import { ellipsisHorizontal, ellipsisVertical } from 'ionicons/icons';
import { useDispatch, useSelector } from 'react-redux';
import { setCurrentSearchView } from '../../data/user/user.slice';
import { RootState } from '../../store';
import { selectCurrentSearchView } from '../../data/user/user.selector';

export const SearchPopover: React.FC = () => {
Expand All @@ -23,9 +22,7 @@ export const SearchPopover: React.FC = () => {
});

const dispatch = useDispatch();
const currentSearchView = useSelector((state: RootState) =>
selectCurrentSearchView(state),
);
const currentSearchView = useSelector(selectCurrentSearchView);

return (
<>
Expand Down
15 changes: 0 additions & 15 deletions src/data/chapter/chapter.actions.tsx

This file was deleted.

7 changes: 0 additions & 7 deletions src/data/chapter/chapter.constants.tsx

This file was deleted.

37 changes: 0 additions & 37 deletions src/data/chapter/chapter.reducer.tsx

This file was deleted.

19 changes: 0 additions & 19 deletions src/data/chapter/chapter.select.tsx

This file was deleted.

11 changes: 11 additions & 0 deletions src/data/user/user.selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ export const selectHasSeenTutorial = createSelector(
selfUserState,
(state) => state.hasSeenTutorial,
);

export const selectFavorites = createSelector(
selfUserState,
(state) => state.favorites,
);

export const selectIsFavorite = (chapterId: string) => {
return createSelector(selectFavorites, (favorites) =>
favorites.includes(chapterId),
);
};
12 changes: 12 additions & 0 deletions src/data/user/user.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ interface CurrentThemePayload {
}

type UserState = {
favorites: Array<string>;
hasSeenTutorial: boolean;
} & CurrentSearchView &
CurrentTheme;

const initialState: UserState = {
favorites: [],
currentSearchView: 'card',
currentTheme: 'system',
hasSeenTutorial: false,
Expand All @@ -31,6 +33,14 @@ const userSlice = createSlice({
name: 'user',
initialState,
reducers: {
addFavorite(state, action: PayloadAction<string>) {
state.favorites = [...state.favorites, action.payload];
},
removeFavorite(state, action: PayloadAction<string>) {
state.favorites = state.favorites.filter(
(favorite) => !favorite.includes(action.payload),
);
},
resetUserState() {
return initialState;
},
Expand All @@ -56,6 +66,8 @@ const userSlice = createSlice({
});

export const {
addFavorite,
removeFavorite,
resetUserState,
setCurrentSearchView,
setCurrentTheme,
Expand Down
5 changes: 1 addition & 4 deletions src/pages/Settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@ import { I18N_LANGUAGES_SUPPORTED } from '../../i18n';
import { colorPaletteOutline, languageOutline } from 'ionicons/icons';
import { THEMES } from '../../utils/theme';
import { selectCurrentTheme } from '../../data/user/user.selector';
import { RootState } from '../../store';
import { resetUserState, setCurrentTheme } from '../../data/user/user.slice';

export const SettingsPage: React.FC = () => {
const { t, i18n } = useTranslation();

const dispatch = useDispatch();
const currentTheme = useSelector((state: RootState) =>
selectCurrentTheme(state),
);
const currentTheme = useSelector(selectCurrentTheme);

return (
<IonPage>
Expand Down
2 changes: 0 additions & 2 deletions src/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import { combineReducers, Reducer } from 'redux';
import { connectRouter } from 'connected-react-router';
import chapterReducer from './data/chapter/chapter.reducer';
import history from './utils/history';
import userSlice from './data/user/user.slice';

Expand All @@ -13,7 +12,6 @@ import userSlice from './data/user/user.slice';
*/
const createReducer = (injectedReducers = {}) => {
const rootReducer = combineReducers({
chapter: chapterReducer,
user: userSlice,
router: connectRouter(history) as Reducer,
...injectedReducers,
Expand Down

0 comments on commit 369e47d

Please sign in to comment.