Skip to content

Commit d93f37e

Browse files
authored
Merge pull request codeBelt#10 from codeBelt/js/base-reducer
Add BaseReducer
2 parents 2ed24b2 + 20d6888 commit d93f37e

File tree

4 files changed

+37
-27
lines changed

4 files changed

+37
-27
lines changed

src/stores/rootReducer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default (history) => {
99
error: ErrorReducer.reducer,
1010
requesting: RequestingReducer.reducer,
1111
router: connectRouter(history),
12-
shows: ShowsReducer.reducer,
12+
shows: new ShowsReducer().reducer,
1313
};
1414

1515
return combineReducers(reducerMap);

src/stores/shows/ShowsReducer.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
import ShowsAction from './ShowsAction';
2+
import BaseReducer from '../../utilities/BaseReducer';
23

3-
export default class ShowsReducer {
4-
static initialState = {
4+
export default class ShowsReducer extends BaseReducer {
5+
initialState = {
56
currentShowId: '74',
67
show: null,
78
episodes: [],
89
actors: [],
910
};
1011

11-
static reducer(state = ShowsReducer.initialState, action) {
12-
if (action.error) {
13-
return state;
14-
}
12+
[ShowsAction.REQUEST_SHOW_FINISHED](state, action) {
13+
return {
14+
...state,
15+
show: action.payload,
16+
};
17+
}
18+
19+
[ShowsAction.REQUEST_EPISODES_FINISHED](state, action) {
20+
return {
21+
...state,
22+
episodes: action.payload,
23+
};
24+
}
1525

16-
switch (action.type) {
17-
case ShowsAction.REQUEST_SHOW_FINISHED:
18-
return {
19-
...state,
20-
show: action.payload,
21-
};
22-
case ShowsAction.REQUEST_EPISODES_FINISHED:
23-
return {
24-
...state,
25-
episodes: action.payload,
26-
};
27-
case ShowsAction.REQUEST_CAST_FINISHED:
28-
return {
29-
...state,
30-
actors: action.payload,
31-
};
32-
default:
33-
return state;
34-
}
26+
[ShowsAction.REQUEST_CAST_FINISHED](state, action) {
27+
return {
28+
...state,
29+
actors: action.payload,
30+
};
3531
}
3632
}

src/utilities/BaseReducer.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default class BaseReducer {
2+
initialState = {};
3+
4+
reducer = (state = this.initialState, action) => {
5+
const handler = this[action.type];
6+
7+
if (!handler || action.error) {
8+
return state;
9+
}
10+
11+
return handler(state, action);
12+
};
13+
}

src/views/App.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ConnectedRouter } from 'connected-react-router';
33
import { Route, Switch } from 'react-router-dom';
44
import RouteEnum from '../constants/RouteEnum';
55
import MainNav from './components/main-nav/MainNav';
6+
import LoadingIndicator from './components/loading-indicator/LoadingIndicator';
67

78
const HomePage = lazy(() => import('./home-page/HomePage'));
89
const NotFoundPage = lazy(() => import('./not-found-page/NotFoundPage'));
@@ -12,7 +13,7 @@ export default class App extends React.Component {
1213
render() {
1314
return (
1415
<ConnectedRouter history={this.props.history}>
15-
<Suspense fallback={<div>Loading...</div>}>
16+
<Suspense fallback={<LoadingIndicator isActive={true} />}>
1617
<MainNav />
1718
<Switch>
1819
<Route exact={true} path={RouteEnum.Home} component={HomePage} />

0 commit comments

Comments
 (0)