Permalink
Browse files

Rewrite tabs handling to be driven by Redux

Added reducer utility functions
Added tab handling logic
Added tabs reducer to root reducer
Removed dead test reducer

Rewrote TabBarContainer to be a connected version of TabBar
  • Loading branch information...
markerikson committed Nov 17, 2016
1 parent 4f871f1 commit e2312e28c36e9487def415562cd4d7bbe3807424
@@ -0,0 +1,15 @@
export function createReducer(initialState, fnMap) {
return (state = initialState, {type, payload}) => {
const handler = fnMap[type];
return handler ? handler(state, payload) : state;
};
}
export function reduceReducers(...reducers) {
return (previous, current) =>
reducers.reduce(
(p, r) => r(p, current),
previous
);
}
@@ -1,35 +1,16 @@
import React, {Component} from "react";
import {connect} from "react-redux";
import TabBar from "./TabBar";
export default class TabBarContainer extends Component {
constructor(props) {
super(props);
import {selectCurrentTab} from "./tabSelectors";
import {selectTab} from "./tabActions";
const {tabs = [{name : null}]} = props;
const mapState = (state) => {
const currentTab = selectCurrentTab(state);
const firstTab = tabs[0];
this.state = {
currentTab : firstTab.name
} ;
}
return {currentTab};
}
onTabClick = (name) => {
this.setState({currentTab : name});
}
const actions = {onTabClick : selectTab};
render() {
const {tabs, ...otherProps} = this.props;
const {currentTab} = this.state;
return (
<TabBar
{...otherProps}
currentTab={currentTab}
onTabClick={this.onTabClick}
tabs={tabs}
/>
)
}
}
export default connect(mapState, actions)(TabBar);
@@ -0,0 +1,8 @@
import {TAB_SELECTED} from "./tabConstants";
export function selectTab(tabName) {
return {
type : TAB_SELECTED,
payload : {tabName},
};
}
@@ -0,0 +1 @@
export const TAB_SELECTED = "TAB_SELECTED";
@@ -0,0 +1,17 @@
import {createReducer} from "common/utils/reducerUtils";
import {TAB_SELECTED} from "./tabConstants";
const initialState = {
currentTab : "unitInfo",
};
export function selectTab(state, payload) {
return {
currentTab : payload.tabName,
};
}
export default createReducer(initialState, {
[TAB_SELECTED] : selectTab,
});
@@ -0,0 +1,8 @@
import {createSelector} from "reselect";
export const selectTabs = state => state.tabs;
export const selectCurrentTab = createSelector(
selectTabs,
tabs => tabs.currentTab
);
@@ -1,9 +1,9 @@
import {combineReducers} from "redux";
import testReducer from "./testReducer";
import tabReducer from "features/tabs/tabReducer";
const rootReducer = combineReducers({
test : testReducer,
tabs : tabReducer,
});
export default rootReducer;

This file was deleted.

Oops, something went wrong.

0 comments on commit e2312e2

Please sign in to comment.