From 5e03d1d097654157094a88bb5056874fb7652171 Mon Sep 17 00:00:00 2001 From: Aruna Herath Date: Thu, 18 Aug 2016 00:45:46 +0530 Subject: [PATCH] Store selected down panel name in redux store. This makes the downl panel component stateless. --- src/modules/ui/actions/index.js | 1 + src/modules/ui/actions/ui.js | 7 ++++++ src/modules/ui/components/down_panel/index.js | 25 +++++++------------ src/modules/ui/configs/reducers/ui.js | 7 ++++++ src/modules/ui/containers/down_panel.js | 10 ++++++-- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/modules/ui/actions/index.js b/src/modules/ui/actions/index.js index 91e9ee5..db163ea 100755 --- a/src/modules/ui/actions/index.js +++ b/src/modules/ui/actions/index.js @@ -3,6 +3,7 @@ import ui from './ui'; export const types = { SET_STORY_FILTER: 'UI_SET_STORY_FILTER', TOGGLE_SHORTCUTS_HELP: 'UI_TOGGLE_SHORTCUTS_HELP', + SELECT_BOTTOM_PANEL: 'UI_SELECT_BOTTOM_PANEL', }; export default { diff --git a/src/modules/ui/actions/ui.js b/src/modules/ui/actions/ui.js index ddd8699..f1a6879 100755 --- a/src/modules/ui/actions/ui.js +++ b/src/modules/ui/actions/ui.js @@ -13,4 +13,11 @@ export default { type: types.TOGGLE_SHORTCUTS_HELP, }); }, + + selectDownPanel({ reduxStore }, panelName) { + reduxStore.dispatch({ + type: types.SELECT_BOTTOM_PANEL, + panelName, + }); + }, }; diff --git a/src/modules/ui/components/down_panel/index.js b/src/modules/ui/components/down_panel/index.js index ef2e1ed..e091bf1 100644 --- a/src/modules/ui/components/down_panel/index.js +++ b/src/modules/ui/components/down_panel/index.js @@ -7,20 +7,16 @@ class DownPanel extends Component { this.state = { current: Object.keys(props.panels)[0] }; } - showPanel(name) { - this.setState({ current: name }); - } - renderTab(name, panel) { let tabStyle = style.tablink; - if (this.state.current === name) { + if (this.props.selectedPanel === name) { tabStyle = Object.assign({}, style.tablink, style.activetab); } const onClick = () => { return e => { e.preventDefault(); - this.showPanel(name); + this.props.onPanelSelect(name); }; }; @@ -43,15 +39,10 @@ class DownPanel extends Component { }); } - renderPanels() { - return Object.keys(this.props.panels).sort().map(name => { - const panelStyle = { display: 'none' }; - const panel = this.props.panels[name]; - if (name === this.state.current) { - Object.assign(panelStyle, { flex: 1, display: 'flex' }); - } - return
{panel.render()}
; - }); + renderPanel() { + const panelStyle = { flex: 1, display: 'flex' }; + const panel = this.props.panels[this.props.selectedPanel]; + return
{panel.render()}
; } renderEmpty() { @@ -69,7 +60,7 @@ class DownPanel extends Component { return (
{this.renderTabs()}
-
{this.renderPanels()}
+
{this.renderPanel()}
); } @@ -77,6 +68,8 @@ class DownPanel extends Component { DownPanel.propTypes = { panels: React.PropTypes.object, + onPanelSelect: React.PropTypes.func, + selectedPanel: React.PropTypes.string, }; export default DownPanel; diff --git a/src/modules/ui/configs/reducers/ui.js b/src/modules/ui/configs/reducers/ui.js index 1276a1e..b5a027d 100755 --- a/src/modules/ui/configs/reducers/ui.js +++ b/src/modules/ui/configs/reducers/ui.js @@ -13,6 +13,13 @@ export default function (state = defaultState, action) { }; } + case types.SELECT_BOTTOM_PANEL: { + return { + ...state, + selectedDownPanel: action.panelName, + }; + } + case types.TOGGLE_SHORTCUTS_HELP: { return { ...state, diff --git a/src/modules/ui/containers/down_panel.js b/src/modules/ui/containers/down_panel.js index 9512e6b..2f28c46 100644 --- a/src/modules/ui/containers/down_panel.js +++ b/src/modules/ui/containers/down_panel.js @@ -2,9 +2,15 @@ import DownPanel from '../components/down_panel'; import { useDeps, composeAll } from 'mantra-core'; import reduxComposer from '../libs/redux_composer'; -export function composer({}, { context }) { +export function composer({ ui }, { context, actions }) { + const panels = context().provider.getPanels(); + const actionMap = actions(); + const selectedPanel = ui.selectedDownPanel || Object.keys(panels)[0]; + return { - panels: context().provider.getPanels(), + panels, + selectedPanel, + onPanelSelect: actionMap.ui.selectDownPanel, }; }