Use a generic setMonitorState action so other monitors can impleemnt other logic

This commit is contained in:
Dan Abramov 2015-07-20 23:24:04 +03:00
parent 276efcca03
commit f6e8d24b33
2 changed files with 16 additions and 29 deletions

View File

@ -6,8 +6,7 @@ const ActionTypes = {
SWEEP: 'SWEEP', SWEEP: 'SWEEP',
TOGGLE_ACTION: 'TOGGLE_ACTION', TOGGLE_ACTION: 'TOGGLE_ACTION',
JUMP_TO_STATE: 'JUMP_TO_STATE', JUMP_TO_STATE: 'JUMP_TO_STATE',
HIDE: 'HIDE', SET_MONITOR_STATE: 'SET_MONITOR_STATE'
SHOW: 'SHOW'
}; };
const INIT_ACTION = { const INIT_ACTION = {
@ -133,21 +132,13 @@ function liftReducer(reducer, initialState) {
currentStateIndex = Math.max(currentStateIndex, stagedActions.length - 1); currentStateIndex = Math.max(currentStateIndex, stagedActions.length - 1);
break; break;
case ActionTypes.PERFORM_ACTION: case ActionTypes.PERFORM_ACTION:
const { action } = liftedAction;
if (currentStateIndex === stagedActions.length - 1) { if (currentStateIndex === stagedActions.length - 1) {
currentStateIndex++; currentStateIndex++;
} }
stagedActions = [...stagedActions, action]; stagedActions = [...stagedActions, liftedAction.action];
break; break;
case ActionTypes.HIDE: case ActionTypes.SET_MONITOR_STATE:
monitorState = { monitorState = liftedAction.monitorState;
isVisible: false
};
break;
case ActionTypes.SHOW:
monitorState = {
isVisible: true
};
break; break;
default: default:
break; break;
@ -227,17 +218,14 @@ export const ActionCreators = {
sweep() { sweep() {
return { type: ActionTypes.SWEEP }; return { type: ActionTypes.SWEEP };
}, },
show() {
return { type: ActionTypes.SHOW };
},
hide() {
return { type: ActionTypes.HIDE };
},
toggleAction(index) { toggleAction(index) {
return { type: ActionTypes.TOGGLE_ACTION, index }; return { type: ActionTypes.TOGGLE_ACTION, index };
}, },
jumpToState(index) { jumpToState(index) {
return { type: ActionTypes.JUMP_TO_STATE, index }; return { type: ActionTypes.JUMP_TO_STATE, index };
},
setMonitorState(monitorState) {
return { type: ActionTypes.SET_MONITOR_STATE, monitorState };
} }
}; };

View File

@ -13,18 +13,18 @@ export default class LogMonitor {
stagedActions: PropTypes.array.isRequired, stagedActions: PropTypes.array.isRequired,
skippedActions: PropTypes.object.isRequired, skippedActions: PropTypes.object.isRequired,
reset: PropTypes.func.isRequired, reset: PropTypes.func.isRequired,
hide: PropTypes.func.isRequired,
show: PropTypes.func.isRequired,
commit: PropTypes.func.isRequired, commit: PropTypes.func.isRequired,
rollback: PropTypes.func.isRequired, rollback: PropTypes.func.isRequired,
sweep: PropTypes.func.isRequired, sweep: PropTypes.func.isRequired,
toggleAction: PropTypes.func.isRequired, toggleAction: PropTypes.func.isRequired,
jumpToState: PropTypes.func.isRequired, jumpToState: PropTypes.func.isRequired,
setMonitorState: PropTypes.func.isRequired,
select: PropTypes.func.isRequired select: PropTypes.func.isRequired
}; };
static defaultProps = { static defaultProps = {
select: (state) => state select: (state) => state,
monitorState: { isVisible: true }
}; };
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
@ -81,15 +81,14 @@ export default class LogMonitor {
} }
handleKeyPress(event) { handleKeyPress(event) {
let { isVisible } = this.props.monitorState; const { monitorState } = this.props;
if (event.ctrlKey && event.keyCode === 72) { if (event.ctrlKey && event.keyCode === 72) { // Ctrl+H
event.preventDefault(); event.preventDefault();
if (isVisible) { this.props.setMonitorState({
this.props.hide(); ...monitorState,
} else { isVisible: !monitorState.isVisible
this.props.show(); });
}
} }
} }