Skip to content

Commit

Permalink
Merge pull request #31 from Chamindu36/ts-dev-migration
Browse files Browse the repository at this point in the history
[TS Migration] Adding RootReducer properly and fix syntax errors
  • Loading branch information
Chamindu36 authored Aug 27, 2023
2 parents 66c6bf3 + 874ce3e commit 8b73023
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 8 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
},
"devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@types/redux-logger": "^3.0.9",
"babel-eslint": "^10.1.0",
"gh-pages": "^5.0.0"
}
Expand Down
4 changes: 3 additions & 1 deletion src/store/cart/cart.selector.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { createSelector } from 'reselect';
import { CartState } from './cart.reducer';

const selectCartReducer = (state): CartState => state.cart;
import { RootState } from '../store'

const selectCartReducer = (state: RootState): CartState => state.cart;

export const selectIsCartOpen = createSelector(
[selectCartReducer],
Expand Down
3 changes: 2 additions & 1 deletion src/store/category/category.selector.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createSelector } from "reselect";

import { RootState } from '../store'
import { CategoriesState } from "./category.reducer";
import { CategoryMap } from "./category.types";

const selectCategoryReducer = (state): CategoriesState => state.categories;
const selectCategoryReducer = (state: RootState): CategoriesState => state.categories;

export const selectCategories = createSelector(
[selectCategoryReducer],
Expand Down
File renamed without changes.
25 changes: 20 additions & 5 deletions src/store/store.js → src/store/store.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import { compose, createStore, applyMiddleware } from 'redux';
import { compose, createStore, applyMiddleware, Middleware } from 'redux';

import { persistStore, persistReducer } from 'redux-persist';
import { persistStore, persistReducer, PersistConfig } from 'redux-persist';
import storage from 'redux-persist/lib/storage';

import logger from 'redux-logger';

// import thunk from 'redux-thunk'; // No need to use thunks when using sagas
import createSagaMiddleware from 'redux-saga'

import { rootReducer } from './root-reducer';
import { rootSaga } from './root-saga';

const persistConfig = {
export type RootState = ReturnType<typeof rootReducer>;

declare global {
interface Window {
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof compose;
}
};

type ExtendedPersistConfig = PersistConfig<RootState> & {
whitelist: Array<keyof RootState>;
};


const persistConfig : ExtendedPersistConfig= {
key: 'root',
storage,
whitelist: ['cart']
Expand All @@ -20,9 +34,10 @@ const sagaMiddleWare = createSagaMiddleware();

const persistedReducer = persistReducer(persistConfig, rootReducer);

const middleWares = [process.env.NODE_ENV !== 'production' && logger,
const middleWares = [
process.env.NODE_ENV !== 'production' && logger,
sagaMiddleWare
].filter(Boolean);
].filter((middleware): middleware is Middleware =>Boolean(middleware));

// Add dev tool extension in chrome to see states, acions and changes
const composeEnhancer =
Expand Down
4 changes: 3 additions & 1 deletion src/store/user/user.selector.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createSelector } from 'reselect';

import { RootState } from '../store'

import { UserState } from './user.reducer';

export const selectUserReducer = (state): UserState => state.user;
export const selectUserReducer = (state: RootState): UserState => state.user;

export const selectCurrentUser = createSelector(
selectUserReducer,
Expand Down

0 comments on commit 8b73023

Please sign in to comment.