forked from woltapp/redux-autoloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create collection and withCollection HOCs to allow global reload
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* eslint-disable react/no-unused-prop-types, | ||
react/prefer-stateless-function, | ||
react/forbid-prop-types | ||
*/ | ||
import { PureComponent, createElement } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import getDisplayName from 'react-display-name'; | ||
import reduxAutoloader from './reduxAutoloader'; | ||
|
||
import { assert } from './utils'; | ||
|
||
const hashTable = {}; | ||
|
||
export const collection = (options, mapStateToProps) => { | ||
const name = options.name; | ||
|
||
assert(name, 'name is required'); | ||
assert(typeof name === 'function' || typeof name === 'string', 'name must be a function or a string'); | ||
|
||
const getReducerName = typeof name === 'function' ? name : () => name; | ||
|
||
return (WrappedComponent) => { | ||
class CollectionComponent extends PureComponent { | ||
static propTypes = { | ||
$refresh: PropTypes.func.isRequired, | ||
$name: PropTypes.string.isRequired, | ||
}; | ||
|
||
componentDidMount() { | ||
hashTable[this.props.$name] = this.props.$refresh; | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
if (nextProps.$refresh !== this.props.$refresh) { | ||
if (this.props.$name !== nextProps.$name) { | ||
delete hashTable[this.props.$name]; | ||
} | ||
|
||
hashTable[nextProps.$name] = nextProps.$refresh; | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
delete hashTable[this.props.$name]; | ||
} | ||
|
||
render() { | ||
const { $name, $refresh, ...props } = this.props; // eslint-disable-line no-unused-vars | ||
return createElement(WrappedComponent, props); | ||
} | ||
} | ||
|
||
CollectionComponent.displayName = `collection-${getDisplayName(WrappedComponent)}`; | ||
CollectionComponent.WrappedComponent = WrappedComponent; | ||
|
||
return reduxAutoloader(options, (state, props) => ({ | ||
$name: getReducerName(props), | ||
$refresh: state.refresh, | ||
...mapStateToProps(state), | ||
}))(CollectionComponent); | ||
}; | ||
}; | ||
|
||
const refresh = () => { | ||
Object.values(hashTable).forEach(loader => loader()); | ||
}; | ||
|
||
export const withCollection = (WrappedComponent) => { | ||
const WithCollection = props => createElement(WrappedComponent, { ...props, refresh }); | ||
|
||
WithCollection.displayName = `withCollection-${getDisplayName(WrappedComponent)}`; | ||
|
||
return WithCollection; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters