Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit ff42d50

Browse files
committed
Organize redux store
1 parent 6a0e984 commit ff42d50

File tree

12 files changed

+62
-70
lines changed

12 files changed

+62
-70
lines changed

src/components/App.connect.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
1-
import { ActionType } from 'typesafe-actions';
21
import { Dispatch } from 'redux';
32
import { connect } from 'react-redux';
43

5-
import * as actions from "../actions";
6-
7-
import { RootState } from "../reducers";
4+
import { ActionsType, RootStateType } from "../shared/store";
85

96
import App from "./App";
107

11-
type Action = ActionType<typeof actions>;
12-
138
interface OwnProps {
149
}
1510

16-
const mapStateToProps = (state: RootState) => ({
11+
const mapStateToProps = (state: RootStateType) => ({
1712
loading: !state.map.ready,
1813
});
1914

20-
const mapDispatchToProps = (dispatch: Dispatch<Action>, props: OwnProps) => ({
15+
const mapDispatchToProps = (dispatch: Dispatch<ActionsType>, props: OwnProps) => ({
2116
});
2217

23-
export default connect(mapStateToProps, mapDispatchToProps)(App);
18+
export default connect(mapStateToProps, mapDispatchToProps)(App);

src/components/Map.connect.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
1-
import { ActionType } from 'typesafe-actions';
21
import { Dispatch, bindActionCreators } from 'redux';
32
import { connect } from 'react-redux';
43

5-
import * as actions from "../actions";
6-
7-
import { RootState } from "../reducers";
8-
9-
type Action = ActionType<typeof actions>;
4+
import { ActionsType, RootStateType, actions } from "../shared/store";
105

116
import Map from "./Map";
127

138
interface OwnProps {
149
}
1510

16-
const mapStateToProps = (state: RootState) => ({});
11+
const mapStateToProps = (state: RootStateType) => ({});
1712

18-
const mapDispatchToProps = (dispatch: Dispatch<Action>, props: OwnProps) => bindActionCreators({
13+
const mapDispatchToProps = (dispatch: Dispatch<ActionsType>, props: OwnProps) => bindActionCreators({
1914
getWeather: (lat: number, lng: number) => actions.weatherGetAction(lat, lng),
2015
mapReady: () => actions.mapReadyAction(),
2116
}, dispatch);
2217

23-
export default connect(mapStateToProps, mapDispatchToProps)(Map);
18+
export default connect(mapStateToProps, mapDispatchToProps)(Map);

src/components/Weather.connect.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
import { ActionType } from 'typesafe-actions';
21
import { Dispatch } from 'redux';
32
import { connect } from 'react-redux';
43

5-
import * as actions from "../actions";
4+
import { ActionsType, RootStateType } from "../shared/store";
65

7-
import { RootState } from "../reducers";
8-
9-
type Action = ActionType<typeof actions>;
10-
11-
import { Weather, WeatherProps } from "./Weather";
6+
import { Weather } from "./Weather";
127

138
interface OwnProps {
149
}
1510

16-
const mapStateToProps = (state: RootState) => ({
11+
const mapStateToProps = (state: RootStateType) => ({
1712
weather: state.weather.weather,
1813
});
1914

20-
const mapDispatchToProps = (dispatch: Dispatch<Action>, props: OwnProps) => ({});
15+
const mapDispatchToProps = (dispatch: Dispatch<ActionsType>, props: OwnProps) => ({});
2116

2217
export default connect(mapStateToProps, mapDispatchToProps)(Weather);

src/index.tsx

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,13 @@
11
import * as React from "react";
22
import * as ReactDOM from "react-dom";
3-
import { createStore, applyMiddleware, compose } from "redux";
4-
import { createEpicMiddleware } from "redux-observable";
53
import { Provider } from "react-redux";
6-
import { ActionType } from "typesafe-actions";
74

85
import App from "./components/App.connect";
96

107
/**
118
* Redux store setup
129
*/
13-
import * as actions from "./actions";
14-
import reducers, { RootState } from "./reducers";
15-
import epics from "./epics";
16-
17-
type Action = ActionType<typeof actions>;
18-
19-
declare global {
20-
interface Window {
21-
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__: Function;
22-
}
23-
}
24-
25-
const composeEnhancers = (
26-
window && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
27-
) || compose;
28-
29-
const epicMiddleware = createEpicMiddleware<Action, Action, RootState>();
30-
31-
function configureStore(initialState?: RootState) {
32-
// configure middlewares
33-
const middlewares = [
34-
epicMiddleware,
35-
];
36-
// compose enhancers
37-
const enhancer = composeEnhancers(
38-
applyMiddleware(...middlewares)
39-
);
40-
// create store
41-
return createStore(
42-
reducers,
43-
initialState,
44-
enhancer
45-
);
46-
}
47-
48-
const store = configureStore();
49-
50-
epicMiddleware.run(epics);
10+
import { store } from "./shared/store";
5111

5212
ReactDOM.render(
5313
<Provider store={store}>
File renamed without changes.
File renamed without changes.

src/epics/WeatherEpic.ts renamed to src/shared/store/epics/WeatherEpic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type Action = ActionType<typeof actions>;
99

1010
import { RootState } from "../reducers";
1111

12-
import { getWeather } from "../shared/services/Api";
12+
import { getWeather } from "../../services/Api";
1313

1414
const weatherGetEpic: Epic<Action, Action, RootState> = (action$, store) =>
1515
action$.pipe(
File renamed without changes.

src/shared/store/index.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { applyMiddleware, compose, createStore } from 'redux';
2+
import { createEpicMiddleware } from 'redux-observable';
3+
import { ActionType } from 'typesafe-actions';
4+
5+
import * as actions from './actions';
6+
import epics from './epics';
7+
import reducers, { RootState } from './reducers';
8+
9+
export type RootStateType = RootState;
10+
export type ActionsType = ActionType<typeof actions>;
11+
12+
declare global {
13+
interface Window {
14+
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__: Function;
15+
}
16+
}
17+
18+
const epicMiddleware = createEpicMiddleware<
19+
ActionsType,
20+
ActionsType,
21+
RootState
22+
>();
23+
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
24+
25+
// Create store
26+
function configureStore(initialState?: RootStateType) {
27+
// configure middlewares
28+
const middlewares = [
29+
epicMiddleware,
30+
];
31+
// compose enhancers
32+
const enhancer = composeEnhancers(
33+
applyMiddleware(...middlewares)
34+
);
35+
// create store
36+
return createStore(
37+
reducers,
38+
initialState,
39+
enhancer
40+
);
41+
}
42+
43+
const store = configureStore();
44+
45+
epicMiddleware.run(epics);
46+
47+
export { store, actions };
File renamed without changes.

0 commit comments

Comments
 (0)