All files / packages/react-tao/src RenderHandler.js

19.23% Statements 10/52
3.45% Branches 1/29
8.33% Functions 1/12
19.23% Lines 10/52
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136                                                                              6x 6x             8x   8x                         6x 6x 6x 6x 6x                                                                                     6x                                                    
import React, { Fragment, Component } from 'react';
import PropTypes from 'prop-types';
import cartesian from 'cartesian';
 
import { normalizeClean, getPermutations } from './helpers';
import { Context } from './Provider';
 
function recursiveContextGenerator(
  ctxList,
  getContext,
  children,
  tao,
  data,
  ctxIdx = 0,
  ctxDataArgs = []
) {
  const ctxName = ctxList[ctxIdx];
  const context = getContext(ctxName);
  if (ctxList.length > ctxIdx + 1) {
    return (
      <context.Consumer name={`${ctxName}.Consumer`}>
        {ctxData => {
          ctxDataArgs.push(ctxData);
          return recursiveContextGenerator(
            ctxList,
            getContext,
            children,
            tao,
            data,
            ctxIdx + 1,
            ctxDataArgs
          );
        }}
      </context.Consumer>
    );
  }
  return (
    <context.Consumer name={`${ctxName}.Consumer`}>
      {ctxData => {
        ctxDataArgs.push(ctxData);
        return children(tao, data, ...ctxDataArgs);
      }}
    </context.Consumer>
  );
}
 
export default class RenderHandler extends Component {
  static contextType = Context;
 
  static propTypes = {
    term: PropTypes.any,
    action: PropTypes.any,
    orient: PropTypes.any,
    context: PropTypes.oneOfType([
      PropTypes.string,
      PropTypes.arrayOf(PropTypes.string)
    ]),
    debug: PropTypes.bool,
    children: PropTypes.func.isRequired
  };
 
  constructor(props) {
    super(props);
    let { shouldRender } = props;
    shouldRender = typeof shouldRender === 'undefined' ? false : shouldRender;
    this.state = { shouldRender };
    this._refreshOn = null;
  }
 
  componentDidMount() {
    const { debug = false } = this.props;
    debug && console.log('RenderHandler::props:', this.props);
    debug && console.log('RenderHandler::context:', this.context);
    const { TAO } = this.context;
    const permutations = getPermutations(this.props);
    if (permutations.length) {
      permutations.forEach(({ term, action, orient }) =>
        TAO.addInlineHandler({ term, action, orient }, this.handleRender)
      );
    }
    const { refreshOn } = this.props;
    if (refreshOn) {
      const trigram = normalizeClean(this.props);
      const refresh = normalizeClean(refreshOn);
      if (!Object.keys(refresh).length) {
        return;
      }
      this._refreshOn = cartesian({ ...trigram, ...refresh });
      this._refreshOn.forEach(({ term, action, orient }) =>
        TAO.addInlineHandler({ term, action, orient }, this.handleRender)
      );
    }
  }
 
  componentWillUnmount() {
    const { TAO } = this.context;
    const permutations = getPermutations(this.props);
    if (permutations.length) {
      permutations.forEach(({ term, action, orient }) =>
        TAO.removeInlineHandler({ term, action, orient }, this.handleRender)
      );
    }
    if (this._refreshOn && this._refreshOn.length) {
      this._refreshOn.forEach(({ term, action, orient }) =>
        TAO.removeInlineHandler({ term, action, orient }, this.handleRender)
      );
    }
  }
 
  handleRender = (tao, data) => {
    // TODO: allow shouldRender prop be a function called
    this.setState({ tao, data, shouldRender: true });
  };
 
  render() {
    const { context, children } = this.props;
    const { shouldRender, tao, data } = this.state;
    const { getDataContext } = this.context;
 
    if (!shouldRender) {
      return null;
    }
    if (!context) {
      return <Fragment>{children(tao, data)}</Fragment>;
    }
    const ctxList = Array.isArray(context) ? context : [context];
    return recursiveContextGenerator(
      ctxList,
      getDataContext,
      children,
      tao,
      data
    );
  }
}