import React from "react"; import { connect } from "react-redux"; import * as Images from "~/state/images/actions"; import { getImagesToArray } from "~/state/images/selectors"; const mapStateToProps = (state: RootState, ownProps: T) => { const imageList = getImagesToArray(state); return { imageList }; }; const mapDispatchToProps = { fetchImages: Images.Actions.fetchImages }; type StateProps = ReturnType; type DispatchProps = typeof mapDispatchToProps; interface InjectedProps { isLoading: boolean; } export type WithImageListProps = StateProps & DispatchProps & InjectedProps; const ImagesFetcher =

( Component: React.ComponentType

) => class extends React.Component { static displayName = `ImagesFetcher(${Component.name})`; state = { isLoading: false }; public componentDidMount() { if (!this.props.imageList.length && !this.state.isLoading) { this.props.fetchImages("wheredidthesodago"); this.setState({ isLoading: true }); } } public componentDidUpdate() { if (this.props.imageList.length && this.state.isLoading) { this.setState({ isLoading: false }); } } public render() { return ; } }; /** * Connecter that fetches and adds props.images[]; * @param {React.ComponentType} Cmp React Component to be wrapped */ export const withImageList =

>( Cmp: React.ComponentType

) => connect>( mapStateToProps, mapDispatchToProps )(ImagesFetcher(Cmp)); export default withImageList;