All files / src/hocs onDeploy.js

80.56% Statements 29/36
57.14% Branches 8/14
88.89% Functions 8/9
80.56% Lines 29/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                2x         2x 2x 2x         2x 2x                         1x 1x       1x 1x       1x   1x 1x                   2x                                   2x   2x 2x   2x             2x 7x 7x 7x 3x       3x 3x   4x 1x 1x   7x                
// @flow
 
import * as React from 'react';
import {get, isArray, isPlainObject, set} from 'lodash';
import RefId from 'canner-ref-id';
import type {HOCProps} from './types';
 
export default function withOndeploy(Com: React.ComponentType<*>) {
  return class ComponentWithOnDeploy extends React.Component<HOCProps> {
    key: string;
    id: ?string;
 
    constructor(props: HOCProps) {
      super(props);
      const {pattern, refId, rootValue} = props;
      const {key, id} = splitRefId({
        refId,
        rootValue,
        pattern
      });
      this.key = key;
      this.id = id;
    }
 
    removeOnDeploy = (arg1: string, callbackId: string) => {
      const {removeOnDeploy} = this.props;
      if (callbackId) {
        return removeOnDeploy(arg1, callbackId);
      } else {
        return removeOnDeploy(this.key, arg1);
      }
    }
 
    onDeploy = (arg1: string | Function, callback: Function): string => {
      const {onDeploy, refId} = this.props;
      Iif (typeof arg1 === 'string') {
        return onDeploy(arg1, callback);
      } else {
        // first arguments is a function
        return onDeploy(this.key, result => {
          let restPathArr = refId.getPathArr();
          // if (this.id) {
          //   restPathArr = restPathArr.slice(2);
          // } else {
            restPathArr = restPathArr.slice(1);
          // }
          const {paths, value} = getValueAndPaths(result.data, restPathArr);
          return {
            ...result,
            // $FlowFixMe
            data: set(result.data, paths, arg1(value))
          }
        });
      }
    }
 
    render() {
      return <Com {...this.props}
        onDeploy={this.onDeploy}
        removeOnDeploy={this.removeOnDeploy}
      />
  }
  };
}
 
 
export function splitRefId({
  refId,
  rootValue,
  pattern
}: {
  refId: RefId,
  rootValue: any,
  pattern: string
}) {
  const [key, index] = refId.getPathArr();
  let id;
  Eif (pattern.startsWith('array')) {
    id = get(rootValue, [key, index, 'id']);
  }
  return {
    key,
    id
  }
}
 
export function getValueAndPaths(value: Map<string, *>, idPathArr: Array<string>) {
  return idPathArr.reduce((result: any, key: string) => {
    let v = result.value;
    let paths = result.paths;
    if (isPlainObject(v)) {
      Iif ('edges' in v && 'pageInfo' in v) {
        v = get(v, ['edges', key, 'node']);
        paths = paths.concat(['edges', key, 'node']);
      } else {
        v = get(v, key);
        paths = paths.concat(key);
      }
    } else if (isArray(v)) {
      v = get(v, key);
      paths = paths.concat(key);
    }
    return {
      value: v,
      paths
    }
  }, {
    value,
    paths: []
  });
}