Skip to content

Commit 1c1817c

Browse files
committed
Add selectors
1 parent 1b7c783 commit 1c1817c

File tree

6 files changed

+75
-36
lines changed

6 files changed

+75
-36
lines changed

dev/register.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,24 @@ const AddonPanel = ({
4242
);
4343
};
4444

45-
register(({ global, local }) => ({
46-
nextInd: local((store) => ({
47-
currentTheme: store.currentTheme + 1,
48-
})),
49-
update: global(),
50-
}))(AddonPanel);
45+
register(
46+
{
47+
themeInd: store => store.currentTheme,
48+
themeList: store => store.themes,
49+
},
50+
({ global, local }) => ({
51+
nextInd: local(store => ({
52+
currentTheme: store.currentTheme + 1,
53+
})),
54+
update: global(),
55+
})
56+
)(AddonPanel);
5157

52-
// register({
53-
// nextInd: (store) => ({ ...store, currentTheme: store.currentTheme + 1 }),
54-
// update: null, // <-- it works :)
55-
// })(AddonPanel);
58+
/* Plain object example
59+
60+
register({
61+
nextInd: (store) => ({ ...store, currentTheme: store.currentTheme + 1 }),
62+
update: null,
63+
})(AddonPanel);
64+
65+
*/

dev/withAdk.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ const DecoratorUI = ({ context, getStory, data, parameters }) => (
1717
</div>
1818
);
1919

20-
export const withAdk = createDecorator(DecoratorUI, {isGlobal: false});
20+
export const withAdk = createDecorator()(DecoratorUI, {isGlobal: false});
2121
export const adkParams = setParameters()

src/ChannelStore.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export default class ChannelStore {
6262
onInitChannel = initData => {
6363
const { data, id } = initData;
6464
const selectorId = id || GLOBAL;
65-
console.log('TCL: ChannelStore -> onConnectedFn -> selectorId', selectorId);
6665
const selectedData = { ...(this.store[selectorId] || {}) };
6766
selectedData.init = data;
6867
selectedData.over = selectedData.over || {};
@@ -126,8 +125,6 @@ export default class ChannelStore {
126125
return payload => {
127126
const subId = getSubId();
128127
const subData = this.store[subId];
129-
console.log('TCL: ChannelStore -> _createAction -> subId', subId);
130-
console.log('TCL: ChannelStore -> _createAction -> subData', subData);
131128
const current = {
132129
...subData.init,
133130
...subData.over,
@@ -145,8 +142,6 @@ export default class ChannelStore {
145142
this._createAction(reducer, () => this.selectorId || this.id);
146143

147144
sendInit = data => {
148-
console.log('TCL: ChannelStore -> onConnectedFn -> id', this.id);
149-
150145
this.init(data);
151146
};
152147

src/decorator.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import withChannel from './withChannel';
33

44
import { getConfig } from './config';
55

6-
export const createDecorator = (Component, { isGlobal = true } = {}) => initData => (getStory, context) => {
6+
export const createDecorator = (selectors, createActions) => (Component, { isGlobal = true } = {}) => initData => (getStory, context) => {
77
const {
88
ADDON_ID,
99
EVENT_ID_INIT,
@@ -24,6 +24,7 @@ export const createDecorator = (Component, { isGlobal = true } = {}) => initData
2424
panel: false,
2525
parameters,
2626
storyId,
27+
createActions,
2728
})(Component);
2829

2930
return <WithChannel getStory={getStory} context={context} />;

src/register.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class PanelHOC extends React.Component {
9090
<div ref={ref} name="addon-holder" style={{ height: '100%' }}>
9191
<Panel
9292
{...this.props.actions}
93+
{...this.props.selectors}
9394
api={api}
9495
active={active}
9596
data={data}
@@ -111,7 +112,7 @@ class PanelHOC extends React.Component {
111112
}
112113
}
113114

114-
export const register = createActions => (Panel, { type = addonTypes.PANEL, initData } = {}) => {
115+
export const register = (storeSelectors, createActions) => (Panel, { type = addonTypes.PANEL, initData } = {}) => {
115116
const config = getConfig();
116117
const {
117118
EVENT_ID_INIT,
@@ -129,6 +130,7 @@ export const register = createActions => (Panel, { type = addonTypes.PANEL, init
129130
ADDON_ID,
130131
initData,
131132
panel: true,
133+
storeSelectors,
132134
createActions,
133135
})(PanelHOC);
134136

src/withChannel.js

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import ChannelStore from './ChannelStore';
55
const getDisplayName = WrappedComponent =>
66
WrappedComponent.displayName || WrappedComponent.name || 'Component';
77

8+
const tryToSelect = fn => store => {
9+
try {
10+
return fn(store);
11+
} catch (err) {
12+
console.warn(err);
13+
return undefined;
14+
}
15+
};
16+
817
const withChannel = ({
918
EVENT_ID_INIT,
1019
EVENT_ID_DATA,
@@ -14,30 +23,48 @@ const withChannel = ({
1423
panel,
1524
parameters,
1625
storyId,
26+
storeSelectors = {},
1727
createActions = {},
1828
}) => WrappedComponent =>
1929
class extends React.Component {
2030
static displayName = `WithChannel(${getDisplayName(WrappedComponent)})`;
2131

22-
state = {
23-
data: {
32+
constructor(props, ...args) {
33+
super(props, ...args);
34+
const initStateData = {
2435
...initData,
25-
...this.props.initData,
36+
...props.initData,
2637
...parameters,
27-
},
28-
isReceived: false,
29-
};
38+
};
39+
40+
this.state = {
41+
data: initStateData,
42+
selectors: this.prepareSelectors(initStateData),
43+
isReceived: false,
44+
};
45+
46+
this.store = new ChannelStore({
47+
EVENT_ID_INIT,
48+
EVENT_ID_DATA,
49+
EVENT_ID_BACK,
50+
name: props.pointName,
51+
initData: this.state.data,
52+
isPanel: this.isPanel,
53+
storyId,
54+
});
55+
56+
this.actions = this.prepareActions();
57+
}
3058

3159
isPanel = this.props.panel || panel;
32-
store = new ChannelStore({
33-
EVENT_ID_INIT,
34-
EVENT_ID_DATA,
35-
EVENT_ID_BACK,
36-
name: this.props.pointName,
37-
initData: this.state.data,
38-
isPanel: this.isPanel,
39-
storyId,
40-
});
60+
61+
prepareSelectors = store => {
62+
return Object.entries(storeSelectors)
63+
.map(([name, selector]) => ({
64+
[name]: tryToSelect(selector)(store),
65+
}))
66+
.reduce((akk, cur) => ({ ...akk, ...cur }), {});
67+
};
4168

4269
prepareActions = () => {
4370
const {
@@ -52,7 +79,6 @@ const withChannel = ({
5279
.reduce((acc, cur) => ({ ...acc, ...cur }), {});
5380
return actions;
5481
};
55-
actions = this.prepareActions();
5682

5783
componentDidMount() {
5884
this.debugLog('componentDidMount');
@@ -83,12 +109,16 @@ const withChannel = ({
83109
};
84110

85111
onData = data => {
86-
this.setState({ data, isReceived: true });
112+
this.setState({
113+
data,
114+
isReceived: true,
115+
selectors: this.prepareSelectors(data),
116+
});
87117
};
88118

89119
render() {
90120
const { pointName, initData, active, onData, ...props } = this.props;
91-
const { data, isReceived } = this.state;
121+
const { data, isReceived, selectors } = this.state;
92122

93123
if (active === false) return null;
94124
if (!isReceived) return null;
@@ -100,6 +130,7 @@ const withChannel = ({
100130
store={this.store}
101131
active={active}
102132
parameters={parameters}
133+
selectors={selectors}
103134
actions={this.actions}
104135
{...props}
105136
/>

0 commit comments

Comments
 (0)