Created
September 4, 2017 05:47
-
-
Save itsdouges/c16fc33f0f631595b74ff85cee28d6f3 to your computer and use it in GitHub Desktop.
Generic hoc to fetch redux data for the armory
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
// @flow | |
import { Component } from 'react'; | |
import { connect } from 'react-redux'; | |
import { createSelector } from 'reselect'; | |
type Options = { | |
fetchResourceData: (id: string) => ReduxThunk, | |
fetchGw2Data: (Array<number>) => Promise<*>, | |
storeKeyResource: string, | |
storeKeyGw2: string, | |
resource: 'users' | 'characters' | 'guilds', | |
emptyUserData?: Array<typeof undefined>, | |
}; | |
const withBatchLoad = ({ fetchResourceData, storeKeyResource, fetchGw2Data, storeKeyGw2, resource, emptyUserData }: Options) => { | |
const selector = createSelector( | |
(store) => (store[resource].data[store[resource].selected] || {})[storeKeyResource] || emptyUserData, | |
(store) => store[storeKeyGw2] || {}, | |
(resourceData, gw2Data) => ({ | |
resourceData, | |
gw2Data, | |
}) | |
); | |
return (WrappedComponent: React.ComponentType<*>) => connect(selector, { | |
fetchResourceData, | |
fetchGw2Data, | |
})( | |
class WithBatchLoad extends Component<*> { | |
props: { | |
id: string, | |
fetchResourceData: (string) => Promise<*>, | |
fetchGw2Data: (Array<number>) => Promise<*>, | |
gw2Data: any, | |
resourceData: any, | |
}; | |
componentDidMount () { | |
this.props.fetchResourceData(this.props.id) | |
.then((action) => | |
action && this.props.fetchGw2Data(action.payload.data.map((item) => item.id))); | |
} | |
render () { | |
return <WrappedComponent {...this.props} />; | |
} | |
} | |
); | |
}; | |
export default withBatchLoad; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment