Skip to content

Commit ca70e0c

Browse files
committed
Manage Store via reducers
1 parent 39668ef commit ca70e0c

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

dev/register.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,26 @@ const AddonPanel = ({
1717
rect,
1818
Layout,
1919
Block,
20+
selectInd
2021
}) => {
2122
return (
2223
<Layout style={{ padding: 0 }}>
2324
<Block style={{ ...blockStyle, minWidth: 50 }} size={200}>
2425
kind: {kind}
2526
<br />
26-
<button onClick={() => setData({ panel: 'bar' })}>setData</button>
27+
<button onClick={() => selectInd(3)}>setData</button>
2728
</Block>
2829
{/* <Block style={blockStyle}>
2930
<small>{JSON.stringify(api.getCurrentStoryData())}</small>
3031
</Block> */}
31-
<Block style={blockStyle} >
32+
<Block style={blockStyle}>
3233
channel store data: <br /> ({JSON.stringify(data)})
3334
</Block>
3435
{/* <Block style={blockStyle}>data ({JSON.stringify(rect, null, 2)})</Block> */}
3536
</Layout>
3637
);
3738
};
3839

39-
register(AddonPanel);
40+
register(({ global, local }) => ({
41+
selectInd: global((store, ind) => ({ store, currentTheme: ind })),
42+
}))(AddonPanel);

src/ChannelStore.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export default class ChannelStore {
8484
selectedData.over = data;
8585
this.selectorId = selectorId;
8686
} else {
87-
this.store = updData;
87+
this.store = data;
8888
}
8989

9090
this.subscriber();
@@ -93,8 +93,8 @@ export default class ChannelStore {
9393
selectData = () => {
9494
const id = this.isPanel ? this.selectorId : this.id;
9595

96-
const { global } = this.store;
97-
const local = this.store[id];
96+
const { global = {} } = this.store;
97+
const local = this.store[id] || {};
9898

9999
const finalData = {
100100
...global.init,

src/register.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class PanelHOC extends React.Component {
8989
return (
9090
<div ref={ref} name="addon-holder" style={{ height: '100%' }}>
9191
<Panel
92+
{...this.props.actions}
9293
api={api}
9394
active={active}
9495
data={data}
@@ -110,7 +111,7 @@ class PanelHOC extends React.Component {
110111
}
111112
}
112113

113-
export const register = (Panel, { type = addonTypes.PANEL, initData } = {}) => {
114+
export const register = createActions => (Panel, { type = addonTypes.PANEL, initData } = {}) => {
114115
const config = getConfig();
115116
const {
116117
EVENT_ID_INIT,
@@ -128,6 +129,7 @@ export const register = (Panel, { type = addonTypes.PANEL, initData } = {}) => {
128129
ADDON_ID,
129130
initData,
130131
panel: true,
132+
createActions,
131133
})(PanelHOC);
132134

133135
addons.register(ADDON_ID, api => {

src/withChannel.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const withChannel = ({
1414
panel,
1515
parameters,
1616
storyId,
17+
createActions = {},
1718
}) => WrappedComponent =>
1819
class extends React.Component {
1920
static displayName = `WithChannel(${getDisplayName(WrappedComponent)})`;
@@ -37,6 +38,22 @@ const withChannel = ({
3738
storyId,
3839
});
3940

41+
42+
prepareActions = () => {
43+
const {
44+
createGlobalAction: global,
45+
createLocalAction: local,
46+
} = this.store;
47+
const isFn = typeof createActions === 'function';
48+
const actions = isFn
49+
? createActions({ global, local })
50+
: Object.entries(createActions)
51+
.map(([name, reducer]) => ({ [name]: global(reducer) }))
52+
.reduce((acc, cur) => ({ ...acc, ...cur }), {});
53+
return actions;
54+
};
55+
actions = this.prepareActions();
56+
4057
componentDidMount() {
4158
this.debugLog('componentDidMount');
4259
this.store.onData(this.onData);
@@ -81,6 +98,7 @@ const withChannel = ({
8198
store={this.store}
8299
active={active}
83100
parameters={parameters}
101+
actions = {this.actions}
84102
{...props}
85103
/>
86104
);

0 commit comments

Comments
 (0)