Guest User

Untitled

a guest
Jan 12th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // @flow
  2.  
  3. import type {Component, Node} from 'react';
  4. import {connect} from 'react-redux';
  5.  
  6. function pick(obj: {[string]: mixed}, props: string[]) {
  7.     const result = {};
  8.  
  9.     for (let i = 0; i < props.length; ++i) {
  10.         result[props[i]] = obj[i];
  11.     }
  12.  
  13.     return result;
  14. }
  15.  
  16. function wrapMapper(mapDataToProps: Function): Function {
  17.     return (store, {id}) => {
  18.         const meta = store.meta[id];
  19.         const data = store.widgets[meta.name][id];
  20.         const collections = pick(store.collections, meta.collections);
  21.  
  22.         return mapDataToProps(data, collections);
  23.     };
  24. }
  25.  
  26. export default function <D, C, P> (
  27.     mapDataToProps: ?(D, C) => $Shape<P>,
  28.     mapDispatchToProps: ?(mixed => void) => $Shape<P>
  29. ): (Component<P> | P => Node) => Component<P> {
  30.     const mapData = mapDataToProps ? wrapMapper(mapDataToProps) : null;
  31.     const mapDispatch = mapDispatchToProps ? d => mapDispatchToProps(d) : null;
  32.  
  33.     return connect(mapData, mapDispatch);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment