All files / src/form/action ActionButtons.tsx

67.41% Statements 60/89
42.39% Branches 39/92
60% Functions 15/25
68.67% Lines 57/83

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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280                      66x           103x 103x 103x     103x   103x   103x 103x 103x   103x 103x 103x 103x 103x     103x 8x           103x 8x       6x 6x 4x 6x 6x   6x   6x 6x 5x 5x             5x               6x   2x   8x         103x     103x         54x 54x 54x 54x 54x 46x   8x 8x 8x 8x   8x 8x   8x 8x           103x 2x                                                                                   103x     56x 56x                                                                                                                                                                                       56x               103x   103x           103x                    
import React, { ReactElement, useCallback, useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { fetchEnabledActionsForClass } from '@utils/FetchUtils';
import { ActionProps, ActionButtonsProps, ActionMode, ActionGroupProps } from "@props/RecordProps";
import { isArrayEqual } from '@utils/ObjectUtils';
import { LoadingOutlined, CaretDownOutlined, CaretRightOutlined } from '@ant-design/icons';
import './App.less';
import { Dropdown, Menu, Space } from 'antd';
import ActionIcon from '../../components/icons/ActionIcon';
import ActionButton from './ActionButton';
 
const ActionButtons = (props: ActionButtonsProps): ReactElement => {
  const {
    domainName, fetchDataCallback, selectedData, mode, zIndex,
    visiblePopover, setVisiblePopoverCallback, displayLabel: displayLabelProp,
    ownerClass, ownerId, columnNameInOwnerClass, direction,
    labelField,
  } = props;
  const { t } = useTranslation();
  const ref = useRef<HTMLSpanElement>(null);
 
  /** Action 列表 */
  const [actions, setActions] = useState<Array<ActionProps>>([]);
  /** 是否在载入中 */
  const [loading, setLoading] = useState<boolean | undefined>();
  /** 每个 Action 的显示组件 */
  const [elements, setElements] = useState<Array<ReactElement>>([]);
  const [selectedIds, setSelectedIds] = useState<Array<number>>([]);
  const [actionGroups, setActionGroups] = useState<Array<ActionGroupProps>>([]);
 
  const isDisplayClassAction = (mode === 'class');
  const isMultipleMode = (mode === 'multiple');
  const isSingleMode = (mode === 'single');
  const alignDirection: "vertical" | "horizontal" = direction ?? (isSingleMode ? 'vertical' : 'horizontal');
  const displayLabel: boolean = (displayLabelProp ?? true);
 
  /** 切换不同的 Domain 显示后,初始化 loading 状态,到后台获取 class action 的列表 */
  useEffect(() => {
    Iif (loading !== undefined) {
      setLoading(undefined);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [domainName]);
 
  const fetchActionList = useCallback((actionMode: ActionMode, newSelectedIds: number[]) => {
    fetchEnabledActionsForClass({
      domainName, type: actionMode, ids: newSelectedIds,
      ownerClass, ownerId, columnNameInOwnerClass
    }).then(json => {
      const enabledActions = json?.filter(a => a.enable === true);
      const filterByModeActions = isSingleMode ?
        enabledActions?.filter(a => ['OBJECT_SINGLE_MULTIPLE', 'OBJECT_SINGLE'].includes(a.mode)) : enabledActions;
      Eif (!isArrayEqual(filterByModeActions, actions)) {
        setActions(filterByModeActions);
      }
      return filterByModeActions;
    }).then(actions => {
      const actionGroups: Array<ActionGroupProps> = [];
      actions?.forEach(action => {
        const actionGroup = actionGroups.find(ag => ag.id === action.group?.id);
        Iif (actionGroup) {
          if (actionGroup.actions) {
            actionGroup.actions.push(action);
          } else {
            actionGroup.actions = [action];
          }
        } else {
          Iif (action.group) {
            actionGroups.push({
              ...action.group,
              actions: [action]
            });
          }
        }
      });
      setActionGroups(actionGroups);
    })
      .catch(e => console.error(`Failed to get action list for [${domainName}, mode: ${actionMode}, selectedIds: ${newSelectedIds}]`, e))
      .finally(() => {
        setLoading(false);
      });
  }, [actions, columnNameInOwnerClass, domainName, ownerClass, ownerId, isSingleMode]);
 
  // 如果选择的数据为 1 条,同时获取 single mode 的 action
  const modeParamForFetchAction: ActionMode = (isMultipleMode && selectedData?.length === 1) ?
    'single&multiple' : mode;
 
  useEffect(() => {
    // 根据当前选中的表格数据到后台获取可用的 Action 列表 */
    // 如果当前选中的数据只有一条,则获取 single, single_multiple, multiple 类型的 action
    // 如果当前选中的数据有多条,则获取 multiple, single_multiple 类型的 action
    // 如果当前没有选中数据,则获取 class 类型的 action
    const newSelectedIds = selectedData?.map(d => d.id) ?? [];
    const selectedDataNoChange = isArrayEqual(newSelectedIds, selectedIds);
    const notClassActionAndSelectedDataNoChange = (selectedDataNoChange && !isDisplayClassAction);
    const classActionLoadedOrLoading = (isDisplayClassAction && loading !== undefined) && !(selectedIds.length !== 0);
    if (notClassActionAndSelectedDataNoChange || classActionLoadedOrLoading) {
      return;
    }
    const currentSelectedRowSize = selectedIds?.length;
    const newSelectedRowSize = newSelectedIds.length;
    const selectFirstRow = (currentSelectedRowSize === 0 && newSelectedRowSize !== 0);
    const unSelectedLastRow = (currentSelectedRowSize !== 0 && newSelectedRowSize === 0);
 
    Eif (loading === undefined || selectFirstRow || unSelectedLastRow) {
      setLoading(true);
    }
    setSelectedIds(newSelectedIds);
    fetchActionList(modeParamForFetchAction, newSelectedIds);
  }, [
    domainName, mode, selectedData, selectedIds, loading, actions, fetchActionList, isMultipleMode,
    isDisplayClassAction, modeParamForFetchAction
  ]);
 
  const buildActionElem = useCallback((action: ActionProps) => {
    return (isSingleMode || action.supportFineTuning) ? (<ActionButton
      visiblePopover={visiblePopover}
      setVisiblePopoverCallback={setVisiblePopoverCallback}
      mode={mode}
      domainName={domainName}
      zIndex={zIndex + 1}
      selectedData={selectedData}
      action={action}
      labelField={labelField}
      displayLabel={displayLabel ?? true}
      fetchDataCallback={() => {
        fetchDataCallback();
        fetchActionList(modeParamForFetchAction, selectedIds);
      }}
      key={action.name}
      ownerClass={ownerClass}
      ownerId={ownerId}
      columnNameInOwnerClass={columnNameInOwnerClass} />) : (<ActionButton
        visiblePopover={visiblePopover}
        setVisiblePopoverCallback={setVisiblePopoverCallback}
        mode={mode}
        domainName={domainName}
        zIndex={zIndex + 1}
        selectedData={selectedData}
        action={action}
        displayLabel={displayLabel ?? true}
        fetchDataCallback={() => {
          fetchDataCallback();
          fetchActionList(modeParamForFetchAction, selectedIds);
        }}
        key={action.id}
        ownerClass={ownerClass}
        ownerId={ownerId}
        labelField={labelField}
        columnNameInOwnerClass={columnNameInOwnerClass} />);
  }, [
    columnNameInOwnerClass, displayLabel, domainName, fetchActionList, fetchDataCallback,
    isSingleMode, mode, modeParamForFetchAction, ownerClass, ownerId, selectedData, selectedIds,
    setVisiblePopoverCallback, visiblePopover, zIndex, labelField
  ]);
 
 
  useEffect(() => {
    // 如果父组件传递了为 true 的 displayLabel 属性或者没有传递 displayLabel,
    let elements: ReactElement[];
    if (actionGroups.length === 0) {
      elements = actions?.map((action) => buildActionElem(action));
    } else E{
      const actionGroupElements = actionGroups.map((actionGroup) => {
        const menu = (
          <Menu>
            {(actionGroup.actions ?? []).map((action) => (
              <Menu.Item key={action.id}>
                <ActionButton
                  visiblePopover={visiblePopover}
                  setVisiblePopoverCallback={setVisiblePopoverCallback}
                  mode={mode}
                  domainName={domainName}
                  zIndex={zIndex + 1}
                  selectedData={selectedData}
                  action={action}
                  displayLabel={displayLabel ?? true}
                  fetchDataCallback={() => {
                    fetchDataCallback();
                    fetchActionList(modeParamForFetchAction, selectedIds);
                  }}
                  ownerClass={ownerClass}
                  ownerId={ownerId}
                  columnNameInOwnerClass={columnNameInOwnerClass}
                  labelField={labelField}
                />
              </Menu.Item>
            ))}
          </Menu>
        );
        const isVertical = (direction === 'vertical');
        const className = isVertical ?
          "action-group-container action-button-inactive" : "action-group-container link-icon-container";
        const spaceGap = isVertical ? 2 : 2;
        const width = (ref.current?.offsetWidth === 0) ?
          185 : (ref.current?.offsetWidth ?? 185);
        const align: { offset?: [number, number] } | undefined = isVertical ?
          { offset: [width, -33] } : undefined;
        return (
          <span key={actionGroup.id} onClick={() => {
            if (visiblePopover != null) {
              setVisiblePopoverCallback(undefined);
            }
          }}>
            <Dropdown
              overlay={menu}
              key={actionGroup.id}
              trigger={['click']}
              overlayClassName={`action-group-dropdown-${direction ?? 'horizontal'}`}
              placement={isVertical ? 'bottomLeft' : undefined}
              // getPopupContainer={(triggerNode) => {
              //   return triggerNode ?? document.body;
              // }}
              align={align}
            >
              <span
                className={className}
                onClick={(e) => e.preventDefault()}>
                <Space direction="horizontal" size={spaceGap} title={actionGroup.helpText ?? ''}>
                  {actionGroup.icon && <ActionIcon icon={actionGroup.icon} />}
                  {!actionGroup.icon && <CaretDownOutlined />}
                  <span className="action-label">
                    {t(actionGroup.label ?? actionGroup.name)}
                  </span>
                  {alignDirection === 'horizontal' && <CaretDownOutlined style={{
                    border: 0, width: "8px", height: "8px", paddingTop: "6px"
                  }} />}
                  {alignDirection === 'vertical' && <CaretRightOutlined style={{
                    paddingTop: "6px", marginLeft: "-6px"
                  }} />}
                </Space>
              </span>
            </Dropdown>
          </span>
        );
      });
 
      const actionsWithoutGroup = actions.filter((action) => !action.group);
      const actionsWithoutGroupElements = actionsWithoutGroup.map((action) => buildActionElem(action));
 
      elements = [(
        <Space
          key="has-group-actions"
          size={4}
          direction={alignDirection}
        >
          {actionsWithoutGroupElements}
          <Space direction={alignDirection} size={8}>
            {actionGroupElements}
          </Space>
        </Space>
      )];
    }
    setElements(elements);
  }, [
    actions, visiblePopover, domainName, mode, zIndex, actionGroups, displayLabelProp,
    columnNameInOwnerClass, isSingleMode, fetchDataCallback, fetchActionList, modeParamForFetchAction,
    ownerId, ownerClass, selectedData, selectedIds, setVisiblePopoverCallback, t, direction,
    alignDirection, displayLabel, buildActionElem, ref.current?.offsetWidth, labelField
  ]);
 
  const emptyMsg = (isMultipleMode && displayLabelProp !== false) ?
    t("No multiple action defined, unselected all rows to show class actions") : "";
  const elementWithSpace = (<Space
    direction={alignDirection}
    size={0}
  >
    {elements}
  </Space>);
  return loading ? (<LoadingOutlined style={{
    margin: "auto",
    width: "100%"
  }} />) : (
    <>{(actions?.length > 0) ?
      (<span ref={ref} style={{ display: "block" }}>{elementWithSpace}</span>) : emptyMsg}</>
  );
};
 
export default ActionButtons;