Skip to content

Instantly share code, notes, and snippets.

@itsdouges
Created September 4, 2017 05:47
Show Gist options
  • Save itsdouges/c16fc33f0f631595b74ff85cee28d6f3 to your computer and use it in GitHub Desktop.
Save itsdouges/c16fc33f0f631595b74ff85cee28d6f3 to your computer and use it in GitHub Desktop.
Generic hoc to fetch redux data for the armory
// @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