-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlocal.js
91 lines (89 loc) · 3.29 KB
/
local.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import PropTypes from 'prop-types';
import React from 'react';
import invariant from 'invariant';
import hoistNonReactStatics from 'hoist-non-react-statics';
import { connect } from 'react-redux';
import * as UIActions from './actions.js';
import { createStore } from './localReducer.js';
export default (Config) => (Component) => {
const defaultMapStateToProps = (state) => state;
const ConnectComp = connect(
Config.mapStateToProps || defaultMapStateToProps,
Config.mapDispatchToProps,
Config.mergeProps)((props) => {
const newProps = Object.assign({}, props);
delete newProps.store;
// eslint-disable-next-line
return (<Component {...newProps} />);
});
class UI extends React.Component {
constructor(props, context) {
super(props, context);
const compKey = typeof Config.key === 'function' ?
Config.key(props, context) : Config.key;
this.store = null;
invariant(Config.key,
'[redux-fractal] - You must supply a key to the component either as a function or string');
this.compKey = compKey;
this.unsubscribe = null;
}
componentWillMount() {
const existingState = this.context.store.getState().local[this.compKey];
const storeResult = createStore(
Config.createStore, this.props,
this.compKey, existingState, this.context);
this.store = storeResult.store;
this.storeCleanup = storeResult.cleanup;
this.context.store.dispatch({
type: UIActions.CREATE_COMPONENT_STATE,
payload: { config: Config, props: this.props, store: this.store, hasStore: !!Config.createStore },
meta: { reduxFractalTriggerComponent: this.compKey },
});
}
componentWillUnmount() {
const persist = typeof Config.persist === 'function' ?
Config.persist(this.props, this.context) : Config.persist;
setTimeout(() => {
this.context.store.dispatch({
type: UIActions.DESTROY_COMPONENT_STATE,
payload: { persist, hasStore: !!Config.createStore },
meta: { reduxFractalTriggerComponent: this.compKey }
});
if (this.storeCleanup) {
this.storeCleanup();
}
this.store = null;
}, 0);
}
render() {
if (this.props.store) {
// eslint-disable-next-line
console.warn(`Props named 'store' cannot be passed to redux-fractal 'local'
HOC with key ${this.compKey} since it's a reserved prop`);
}
return (
this.store && <ConnectComp
{...this.props}
store={this.store}
/>
);
}
}
UI.contextTypes = Object.assign({}, Component.contextTypes, {
store: PropTypes.shape({
subscribe: PropTypes.func.isRequired,
dispatch: PropTypes.func.isRequired,
getState: PropTypes.func.isRequired,
}),
});
UI.propTypes = Object.assign({}, {
store: PropTypes.shape({
subscribe: PropTypes.func.isRequired,
dispatch: PropTypes.func.isRequired,
getState: PropTypes.func.isRequired,
}),
});
const displayName = Component.displayName || Component.name || 'Component';
UI.displayName = `local(${displayName})`;
return hoistNonReactStatics(UI, Component);
};