All files / src/form/action ActionComponent.tsx

3.7% Statements 1/27
0% Branches 0/29
0% Functions 0/9
3.7% Lines 1/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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176                                                          66x                                                                                                                                                                                                                                                                                                    
import React, { ReactElement } from 'react';
import { DoubleRightOutlined, ExperimentOutlined } from '@ant-design/icons';
import { Alert, Button, Form, Popconfirm, Space, Spin } from 'antd';
import { useTranslation } from 'react-i18next';
import { DataCollectForm } from '../';
import { emptyMethod } from '@utils/Constants';
import {
  ActionExecResult, ActionProps, MultipleExecResults, RecordProps, TableMetaProps
} from '@props/RecordProps';
import { wrapAsHtml } from '@utils/ComponentUtils';
import { ObjectCell } from "../../form/cells";
import { stopPropagationAndPreventDefault } from "@utils/ObjectUtils";
 
export type ActionComponentProps = {
  action: ActionProps;
  executeCallback: (isFinalRound: boolean) => void;
  element: ReactElement;
  zIndex: number;
  setVisiblePopoverCallback: (key?: string) => void;
  setResults: (results: MultipleExecResults | ActionExecResult) => void;
  parameters?: Array<TableMetaProps>;
  formValues?: RecordProps;
  setFormValues: (value: RecordProps) => void;
  selectedData?: Array<RecordProps>;
  domainName: string;
  labelField?: string;
  loadingParameters?: boolean;
};
 
const ActionComponent = (props: ActionComponentProps): ReactElement => {
  const {
    action, zIndex, executeCallback, setResults, parameters, domainName,
    formValues, setFormValues, setVisiblePopoverCallback, selectedData,
    loadingParameters, labelField
  } = props;
  const { t } = useTranslation();
  const { id, helpText, confirmMessage, confirmType, supportFineTuning } = action;
  const [form] = Form.useForm();
 
  const validAndExecuteAction = (isFinalRound: boolean, event?: React.MouseEvent<HTMLElement>): void => {
    event?.preventDefault();
    form.validateFields().then(() => {
      executeCallback(isFinalRound);
    });
  };
 
  const needParameter = ((parameters?.length ?? 0) > 0);
  const isNoConfirmAction = (['NO_CONFIRM', 'NO_POPUP_NO_CONFIRM'].includes(confirmType));
  const isDisplayConfirmAction = (confirmType === 'DISPLAY_CONFIRM');
  const hasConfirmMessage = (confirmMessage != null && confirmMessage !== '');
  const hasSelectedData = ((selectedData?.length ?? 0) > 0);
  const objectsDisplay = selectedData?.map(item => {
    let label = undefined;
    if (labelField) {
      label = item[labelField];
    }
    return <ObjectCell
      domainName={domainName}
      displayText={label}
      id={item.id}
      zIndex={zIndex}
      key={item.id}
    />;
  });
  // const dryRunTitle = action.supportFineTuning ? t('Dry run') : t('Run');
  // const dryRunIcon = action.supportFineTuning ? <ExperimentOutlined /> : <DoubleRightOutlined />;
  // const dryRunButton = (<Space direction="horizontal">
  //   {dryRunTitle}
  //   {dryRunIcon}
  // </Space>);
  return (
    <div className="dmac-container">
      {!needParameter &&
        <Alert
          message={t("No parameters needed to run this action")}
          type="info"
          showIcon
          className="action-message-info"
        />
      }
      {hasSelectedData && (
        <Alert
          message={t('Selected objects')}
          type="info"
          className="action-target-objects-info action-message-info"
          showIcon={true}
          description={objectsDisplay}
        />
      )}
      {
        hasConfirmMessage && <Alert
          message={wrapAsHtml(confirmMessage)}
          type="info"
          className="action-message-info"
        />
      }
      {loadingParameters && <Spin className="para-loading-spin"/>}
      {!loadingParameters && needParameter &&
        <DataCollectForm
          page="action"
          labelAlign="left"
          onChange={(params: RecordProps) => {
            setFormValues(params);
          }}
          onFinishFailed={emptyMethod}
          onFinish={emptyMethod}
          operation="create"
          columns={parameters ?? []}
          domainName={""}
          hideDetailPanel={true}
          groups={[]}
          form={form}
          readonly={false}
          hideFields={[]}
          record={formValues}
          zIndex={zIndex}
          formType={"Action"}
        />
      }
      <div
        className="action-with-msg-run-button-container"
        title={helpText}
      >
        <Space size="middle">
          {supportFineTuning &&
            <Button
              title={t("Final run the action with current parameters and selected dry run result")}
              onClick={(e) => validAndExecuteAction(false, e)}
              icon={<ExperimentOutlined />}
              type="primary"
              size="middle"
            >
              {t('Run')}
            </Button>}
          {!supportFineTuning && isDisplayConfirmAction && <Popconfirm
            title={t("Confirm to run this action")}
            okText={t("Confirm")}
            cancelText={t("Cancel")}
            placement="top"
            onConfirm={() => validAndExecuteAction(true, undefined)}
            overlayStyle={{ zIndex: zIndex + 1 }}
          >
            <Button
              onClick={(e: React.MouseEvent<HTMLElement>) => {
                stopPropagationAndPreventDefault(e);
                //Hide other action popups
                setVisiblePopoverCallback(id.toString());
                // Reset result for other results
                setResults({});
              }}
              icon={<DoubleRightOutlined />}
              type="primary"
              size="middle"
            >
              {t("Run and save")}
            </Button>
          </Popconfirm>
          }
          {!supportFineTuning && isNoConfirmAction &&
            <Button
              onClick={(e) => validAndExecuteAction(true, e)}
              icon={<DoubleRightOutlined />}
              type="primary"
              size="middle"
            >
              {t("Run and save")}
            </Button>
          }
        </Space>
      </div>
    </div>
  );
};
 
export default ActionComponent;