Skip to content

Commit

Permalink
готовит redux
Browse files Browse the repository at this point in the history
  • Loading branch information
denispan committed Mar 24, 2024
1 parent 69e2fb8 commit df18cbf
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/hooks/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {TypedUseSelectorHook, useDispatch, useSelector} from 'react-redux';
import {AppDispatch, RootState} from '../types/store.ts';

const useAppDispatch = useDispatch<AppDispatch>;
const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

export {useAppDispatch, useAppSelector};
6 changes: 5 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import ReactDOM from 'react-dom/client';
import App from './app.tsx';
import {OFFERS_COUNT} from './const.ts';
import {getFavoriteOffers, getOffersShortInfo} from './mocks/offers.ts';
import {Provider} from 'react-redux';
import {store} from './store';

const offers = getOffersShortInfo();
const offersFavorites = getFavoriteOffers();
Expand All @@ -13,6 +15,8 @@ const root = ReactDOM.createRoot(

root.render(
<React.StrictMode>
<App offers={offers} offersFavorites={offersFavorites} offersCount={OFFERS_COUNT} />
<Provider store={store}>
<App offers={offers} offersFavorites={offersFavorites} offersCount={OFFERS_COUNT} />
</Provider>
</React.StrictMode>
);
15 changes: 15 additions & 0 deletions src/store/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {createAction} from '@reduxjs/toolkit';
import {CITIES} from '../const.ts';
import {getOffersShortInfo} from '../mocks/offers.ts';

export const setCityAction = createAction(
'main/city',
(value: typeof CITIES[number]['slug']) => ({
payload: value
}));

export const getOffersShortInfoAction = createAction(
'main/offers-short',
() => ({
payload: getOffersShortInfo(),
}));
6 changes: 6 additions & 0 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {configureStore} from '@reduxjs/toolkit';
import {reducer} from './reducer.ts';

export const store = configureStore({
reducer: reducer
});
25 changes: 25 additions & 0 deletions src/store/reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {CITIES, DEFAULT_CITY_SLUG} from '../const.ts';
import {getOffersShortInfo} from '../mocks/offers.ts';
import {createReducer} from '@reduxjs/toolkit';
import {getOffersShortInfoAction, setCityAction} from './action.ts';
import {OfferShortInfo} from '../types/offer.ts';

interface State {
city: typeof CITIES[number]['slug'];
offers: OfferShortInfo[];
}

const initialState: State = {
city: DEFAULT_CITY_SLUG,
offers: getOffersShortInfo(),
};

export const reducer = createReducer(initialState, (builder) => {
builder
.addCase(setCityAction, (state, action) => {
state.city = action.payload;
})
.addCase(getOffersShortInfoAction, (state, action) => {
state.offers = action.payload;
});
});
6 changes: 6 additions & 0 deletions src/types/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {store} from '../store';

type RootState = ReturnType<typeof store.getState>;
type AppDispatch = typeof store.dispatch;

export type {RootState, AppDispatch};

0 comments on commit df18cbf

Please sign in to comment.