Skip to content

Commit 1f21d83

Browse files
committed
This API is much better
1 parent 79b51a7 commit 1f21d83

File tree

9 files changed

+97
-119
lines changed

9 files changed

+97
-119
lines changed

examples/counter/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"babel-core": "^5.6.18",
2727
"babel-loader": "^5.1.4",
2828
"node-libs-browser": "^0.5.2",
29-
"react-dock": "^0.1.0",
29+
"react-dock": "^0.2.1",
3030
"react-hot-loader": "^1.3.0",
3131
"redux-devtools": "^3.0.0-alpha-8",
3232
"redux-devtools-log-monitor": "^1.0.0-alpha-8",

examples/counter/src/containers/DevTools.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import LogMonitor from 'redux-devtools-log-monitor';
44
import DockMonitor from '../dock/DockMonitor';
55

66
export default createDevTools(
7-
<DockMonitor defaultPosition='bottom'>
7+
<DockMonitor>
88
<LogMonitor theme='ocean' />
99
</DockMonitor>
1010
);
Lines changed: 33 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
1-
//
2-
// TODO: extract to a separate project.
3-
//
4-
5-
import React, { cloneElement, Children, Component, PropTypes } from 'react';
1+
import React, { cloneElement, Component, PropTypes } from 'react';
62
import Dock from 'react-dock';
7-
import { combineReducers } from 'redux';
8-
9-
const POSITIONS = ['left', 'top', 'right', 'bottom'];
3+
import { POSITIONS } from './constants';
4+
import { toggleVisibility, changePosition, changeSize } from './actions';
5+
import reducer from './reducers';
106

117
export default class DockMonitor extends Component {
8+
static reducer = reducer;
9+
1210
static propTypes = {
1311
defaultPosition: PropTypes.oneOf(POSITIONS).isRequired,
1412
defaultIsVisible: PropTypes.bool.isRequired,
13+
defaultSize: PropTypes.number.isRequired,
1514
toggleVisibilityShortcut: PropTypes.string.isRequired,
1615
changePositionShortcut: PropTypes.string.isRequired,
16+
children: PropTypes.element,
1717

18+
dispatch: PropTypes.func,
1819
monitorState: PropTypes.shape({
1920
position: PropTypes.oneOf(POSITIONS).isRequired,
21+
size: PropTypes.number.isRequired,
2022
isVisible: PropTypes.bool.isRequired,
21-
child: PropTypes.any
22-
}),
23-
24-
monitorActions: PropTypes.shape({
25-
toggleVisibility: PropTypes.func.isRequired,
26-
changePosition: PropTypes.func.isRequired
23+
childMonitorState: PropTypes.any
2724
})
2825
};
2926

3027
static defaultProps = {
3128
defaultIsVisible: true,
3229
defaultPosition: 'right',
30+
defaultSize: 0.3,
3331
toggleVisibilityShortcut: 'H',
3432
changePositionShortcut: 'Q'
3533
};
3634

37-
componentDidMount() {
35+
constructor(props) {
36+
super(props);
3837
this.handleKeyDown = this.handleKeyDown.bind(this);
38+
this.handleSizeChange = this.handleSizeChange.bind(this);
39+
}
40+
41+
componentDidMount() {
3942
window.addEventListener('keydown', this.handleKeyDown);
4043
}
4144

@@ -53,81 +56,36 @@ export default class DockMonitor extends Component {
5356
const char = String.fromCharCode(key);
5457
switch (char.toUpperCase()) {
5558
case this.props.toggleVisibilityShortcut.toUpperCase():
56-
this.props.monitorActions.toggleVisibility();
59+
this.props.dispatch(toggleVisibility());
5760
break;
5861
case this.props.changePositionShortcut.toUpperCase():
59-
this.props.monitorActions.changePosition();
62+
this.props.dispatch(changePosition());
6063
break;
6164
default:
6265
break;
6366
}
6467
}
6568

66-
render() {
67-
const {
68-
monitorState,
69-
monitorActions,
70-
historyState,
71-
historyActions,
72-
children
73-
} = this.props;
69+
handleSizeChange(requestedSize) {
70+
this.props.dispatch(changeSize(requestedSize));
71+
}
7472

75-
const {
76-
position,
77-
isVisible
78-
} = monitorState;
73+
render() {
74+
const { monitorState, children, ...rest } = this.props;
75+
const { position, isVisible, size } = monitorState;
76+
const childProps = {
77+
...rest,
78+
monitorState: monitorState.childMonitorState
79+
};
7980

8081
return (
8182
<Dock position={position}
8283
isVisible={isVisible}
84+
size={size}
85+
onSizeChange={this.handleSizeChange}
8386
dimMode='none'>
84-
{cloneElement(Children.only(children), {
85-
monitorState: monitorState.child,
86-
monitorActions: monitorActions.child,
87-
historyState,
88-
historyActions
89-
})}
87+
{cloneElement(children, childProps)}
9088
</Dock>
9189
);
9290
}
9391
}
94-
95-
const TOGGLE_VISIBILITY = '@@redux-devtools/dock/TOGGLE_VISIBILITY';
96-
function toggleVisibility() {
97-
return { type: TOGGLE_VISIBILITY };
98-
}
99-
100-
const CHANGE_POSITION = '@@redux-devtools/dock/CHANGE_POSITION';
101-
function changePosition() {
102-
return { type: CHANGE_POSITION };
103-
}
104-
105-
DockMonitor.setup = function setup(props) {
106-
function position(state = props.defaultPosition, action) {
107-
return (action.type === CHANGE_POSITION) ?
108-
POSITIONS[(POSITIONS.indexOf(state) + 1) % POSITIONS.length] :
109-
state;
110-
}
111-
112-
function isVisible(state = props.defaultIsVisible, action) {
113-
return (action.type === TOGGLE_VISIBILITY) ?
114-
!state :
115-
state;
116-
}
117-
118-
const child = Children.only(props.children);
119-
const childSetupResult = child.type.setup(child.props);
120-
121-
return {
122-
reducer: combineReducers({
123-
position,
124-
isVisible,
125-
child: childSetupResult.reducer
126-
}),
127-
actionCreators: {
128-
toggleVisibility,
129-
changePosition,
130-
child: childSetupResult.actionCreators
131-
}
132-
};
133-
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const TOGGLE_VISIBILITY = '@@redux-devtools/dock/TOGGLE_VISIBILITY';
2+
export function toggleVisibility() {
3+
return { type: TOGGLE_VISIBILITY };
4+
}
5+
6+
export const CHANGE_POSITION = '@@redux-devtools/dock/CHANGE_POSITION';
7+
export function changePosition() {
8+
return { type: CHANGE_POSITION };
9+
}
10+
11+
export const CHANGE_SIZE = '@@redux-devtools/dock/CHANGE_SIZE';
12+
export function changeSize(size) {
13+
return { type: CHANGE_SIZE, size: size };
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const POSITIONS = ['left', 'top', 'right', 'bottom'];
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { CHANGE_POSITION, CHANGE_SIZE, TOGGLE_VISIBILITY } from './actions';
2+
import { POSITIONS } from './constants';
3+
4+
function position(state = props.defaultPosition, action, props) {
5+
return (action.type === CHANGE_POSITION) ?
6+
POSITIONS[(POSITIONS.indexOf(state) + 1) % POSITIONS.length] :
7+
state;
8+
}
9+
10+
function size(state = props.defaultSize, action, props) {
11+
return (action.type === CHANGE_SIZE) ?
12+
action.size :
13+
state;
14+
}
15+
16+
function isVisible(state = props.defaultIsVisible, action, props) {
17+
return (action.type === TOGGLE_VISIBILITY) ?
18+
!state :
19+
state;
20+
}
21+
22+
function childMonitorState(state, action, props) {
23+
const child = props.children;
24+
return child.type.reducer(state, action, child.props);
25+
}
26+
27+
export default function reducer(state = {}, action, props) {
28+
return {
29+
position: position(state.position, action, props),
30+
isVisible: isVisible(state.isVisible, action, props),
31+
size: size(state.size, action, props),
32+
childMonitorState: childMonitorState(state.childMonitorState, action, props)
33+
};
34+
}

src/bindActionCreatorsDeep.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/createDevTools.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,30 @@
11
import React, { Children, Component, PropTypes } from 'react';
22
import { connect } from 'react-redux';
3-
import { bindActionCreators } from 'redux';
4-
import bindActionCreatorsDeep from './bindActionCreatorsDeep';
5-
import instrument, { ActionCreators as historyActionCreators } from './instrument';
3+
import instrument from './instrument';
64

75
export default function createDevTools(children) {
8-
const child = Children.only(children);
9-
const { type: Monitor } = child;
10-
const { reducer, actionCreators } = Monitor.setup(child.props);
6+
const monitorElement = Children.only(children);
7+
const monitorProps = monitorElement.props;
8+
const Monitor = monitorElement.type;
9+
10+
const enhancer = instrument((state, action) =>
11+
Monitor.reducer(state, action, monitorProps)
12+
);
1113

1214
function mapStateToProps(state) {
1315
return {
14-
historyState: state.historyState,
16+
...state.historyState,
1517
monitorState: state.monitorState
1618
};
1719
}
18-
19-
function mapDispatchToProps(dispatch) {
20-
return {
21-
historyActions: bindActionCreators(historyActionCreators, dispatch),
22-
monitorActions: bindActionCreatorsDeep(actionCreators, dispatch)
23-
};
24-
}
25-
26-
const ConnectedMonitor = connect(
27-
mapStateToProps,
28-
mapDispatchToProps
29-
)(Monitor);
20+
const ConnectedMonitor = connect(mapStateToProps)(Monitor);
3021

3122
return class DevTools extends Component {
3223
static contextTypes = {
3324
store: PropTypes.object.isRequired
3425
};
3526

36-
static instrument = () => instrument(reducer);
27+
static instrument = () => enhancer;
3728

3829
constructor(props, context) {
3930
super(props, context);
@@ -42,7 +33,7 @@ export default function createDevTools(children) {
4233

4334
render() {
4435
return (
45-
<ConnectedMonitor {...child.props}
36+
<ConnectedMonitor {...monitorProps}
4637
store={this.instrumentedStore} />
4738
);
4839
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export { default as instrument, ActionTypes } from './instrument';
1+
export { default as instrument, ActionCreators, ActionTypes } from './instrument';
22
export { default as persistState } from './persistState';
33
export { default as createDevTools } from './createDevTools';

0 commit comments

Comments
 (0)