import { Component , ComponentClass , ComponentType , createElement } from 'react' ; import { identity } from 'stapp/lib/helpers/identity/identity' ; import { getDisplayName } from '../helpers/getDisplayName' ; import { Stapp } from 'stapp' ; import { ConsumeHoc } from '../models/ConsumeHoc' ; import { ConsumerProps } from '../models/Props' ; /**
 * Creates higher order component, that passes state and api from a Stapp application
 * to a wrapped component
 */ export const createConsume = < State , Api > ( Consumer , name = 'Stapp' ) : ConsumeHoc < State , Api > => { return ( < Result > ( map = identity as any ) => ( WrappedComponent ) => { return class Consume extends Component < any > { static displayName = `${ name }.Consume(${ getDisplayName ( WrappedComponent ) })` ; mapState = ( state , api ) => map ( state , api , this . props ) ; render ( ) { return createElement ( Consumer , { map : this . mapState , render : ( result , api , app ) => { return createElement ( WrappedComponent as any , Object . assign ( { } , this . props , { api , app } , result ) ) ; } } ) ; } } ; } ) as any ; } ; hidden export const createInject = createConsume ;