All files / hocs deploy.js

44.44% Statements 12/27
75% Branches 21/28
40% Functions 6/15
44.44% Lines 12/27

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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126                        12x                             13x 13x       13x 13x 13x 13x   13x                                                                           14x                     6x                   13x                     5x                                    
// @flow
import * as React from 'react';
import {Button} from 'antd';
import type RefId from 'canner-ref-id';
import {isArray} from 'lodash';
import {isRoutesEndAtMe} from './cache';
import type {HOCProps} from './types';
 
type State = {
};
 
export default function deploy(Com: React.ComponentType<*>) {
  return class ComponentWithDeploy extends React.Component<HOCProps, State> {
    deploy = (refId: RefId, callback: Function) => {
      const {deploy} = this.props;
      return deploy(refId.getPathArr()[0]).then(callback).catch(() => {});
    }
 
    reset = (refId: RefId, callback: Function) => {
      const {reset, rootValue} = this.props;
      const key = refId.getPathArr()[0];
      const recordId = getItemId(rootValue, refId);
      reset(key, recordId);
      callback();
    }
 
    render() {
      const {routerParams, routes, refId, path, pattern, controlDeployAndResetButtons, hideButtons} = this.props;
      const buttonContainer = {
        textAlign: 'right',
        marginTop: 60
      }
      const renderConfirmButton = genDeployButton(this.deploy, refId);
      const renderCancelButton = genCancelButton(this.reset, refId);
      const isCreateOp = routerParams.operator === 'create';
      const shouldRenderButtons = (routes.length === 1 || isRoutesEndAtMe({routes, path, pattern}) &&
      isCreateOp) && !controlDeployAndResetButtons && !hideButtons && refId.getPathArr().length <= routes.length;
      return <div>
        {/* $FlowFixMe */}
        <Com {...this.props}
          renderConfirmButton={renderConfirmButton}
          renderCancelButton={renderCancelButton}
        />
        {
          shouldRenderButtons &&
            // $FlowFixMe
            <div style={buttonContainer}>
              {renderConfirmButton({
                callback: () => {
                  // location.href = routerParams.backUrl || location.href.split('?')[0];
                },
                style: {marginRight: 16}
              })}
              {renderCancelButton({
                hidden: isCreateOp
              })}
            </div>
        }
      </div>;
    }
  };
}
 
type buttonProps = {
  disabled?: boolean,
  style?: Object,
  refId?: RefId,
  onClick?: (refId?: RefId, callback?: Function) => Promise<*>,
  callback?: Function,
  text?: React.Node | string,
  hidden?: boolean,
  component?: React.ComponentType<*>
}
 
export function genDeployButton(deploy: Function, currentRefId: RefId) {
  return function DeployButton({
    disabled = false,
    style = {marginRight: 16},
    refId = currentRefId,
    onClick = deploy,
    callback = () => {},
    // $FlowFixMe
    text = 'Confirm',
    // $FlowFixMe
    component = Button
  }: buttonProps = {}) {
    return React.createElement(component, {
      disabled,
      style,
      type: "primary",
      onClick: () => onClick(refId, callback)
    }, text);
  };
}
 
export function genCancelButton(reset: Function, currentRefId: RefId) {
  return function CancelButton({
    disabled = false,
    style = {},
    refId = currentRefId,
    onClick = reset,
    callback = () => {},
    // $FlowFixMe
    text = 'Reset',
    // $FlowFixMe
    component = Button
  }: buttonProps = {}) {
    return React.createElement(component, {
      disabled,
      style,
      onClick: () => onClick(refId, callback)
    }, text);
  };
}
 
 
function getItemId(rootValue: any, refId: RefId) {
  const [key, index] = refId.getPathArr();
  let itemId = '';
  const value = rootValue[key];
  if (isArray(value) && index) {
    itemId = value[index].id;
  }
  return itemId;
}