All files / src/form/list Operations.tsx

65.11% Statements 28/43
72.5% Branches 29/40
43.75% Functions 7/16
65.11% Lines 28/43

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                    66x         1283x 1281x 1281x 1281x 1281x 1281x 1281x     1281x   1281x 1264x 639x   625x                   625x 3x   1x   4x         1281x 1250x 1250x       1281x                                                         1281x                                                       1281x                           1281x                     1281x     1281x                                 1281x     1250x                                        
import React, { ReactElement, useCallback, useEffect, useState } from 'react';
import { Divider, Dropdown, Menu } from "antd";
import { SettingOutlined, LoadingOutlined } from '@ant-design/icons';
import { useTranslation } from 'react-i18next';
import { fetchFormIdAndExtInfoByName, fetchFormIdAndExtInfoByType } from "@utils/FetchUtils";
import { OperationsProps } from "@props/RecordProps";
import { DeleteComponent, ActionButtons, UpdateAction } from "../";
import { stopPropagationAndPreventDefault } from '@utils/ObjectUtils';
import { useDomainPermit } from '@utils/DomainPermitUtils';
 
const Operations = (props: OperationsProps): ReactElement => {
  const {
    domainName, id, updateCallback, deleteCallback, ownerClass, fetchType,
    ownerId, page, zIndex, showActions, changeModalVisibleCallback,
    updateFormName, columnNameInOwnerClass,
  } = props;
  const { t } = useTranslation();
  const [updateFormId, setUpdateFormId] = useState<number>();
  const [loading, setLoading] = useState<boolean>(true);
  const [visiblePopover, setVisiblePopover] = useState<string>();
  const [menuVisible, setMenuVisible] = useState<boolean>(false);
  const domainPermit = useDomainPermit(domainName, id);
 
  /** 查看详情的 Modal 的只读状态,在查看详情的 Modal 中,可以点击编辑按钮,将其状态修改为编辑状态 */
  const [detailModalReadonly, setDetailModalReadonly] = useState<boolean>(true);
 
  useEffect(() => {
    if (menuVisible === false || updateFormId != null) {
      return;
    }
    Iif (updateFormName != null && updateFormName !== '') {
      fetchFormIdAndExtInfoByName(domainName, updateFormName).then((json) => {
        setUpdateFormId(json?.id);
      }).catch(e => {
        console.error(`Failed to get Update formId of ${domainName} with name ${updateFormName}: ${e}`);
      }).finally(() => {
        setLoading(false);
      });
      return;
    } else {
      fetchFormIdAndExtInfoByType(domainName, 'Update').then((json) => {
        setUpdateFormId(json?.id);
      }).catch(e => {
        console.error(`Failed to get Update formId of ${domainName}: ${e}`);
      }).finally(() => {
        setLoading(false);
      });
    }
  }, [domainName, menuVisible, updateFormId, updateFormName]);
 
  const setVisibleAndCallback = useCallback((visible: boolean): void => {
    setMenuVisible(visible);
    changeModalVisibleCallback?.(visible);
  }, [changeModalVisibleCallback]);
 
  const detailAction = (
    <Menu.Item
      key="detail"
      title={t('Detail')}
      onClick={() => setVisibleAndCallback(false)}
    >
      <UpdateAction
        id={id}
        domainName={domainName}
        ownerClass={ownerClass}
        columnNameInOwnerClass={columnNameInOwnerClass}
        callback={updateCallback}
        ownerId={ownerId}
        errorMsg={domainPermit.canUpdateErrorMsg}
        formId={updateFormId}
        zIndex={zIndex}
        showIconAndText={true}
        readonly={detailModalReadonly}
        switchEditable={() => {
          if (detailModalReadonly) {
            domainPermit.canUpdate && setDetailModalReadonly(false);
          } else {
            setDetailModalReadonly(true);
          }
        }}
        fetchType={fetchType}
      />
    </Menu.Item>
  );
 
  const editAction = (<Menu.Item
    key="update"
    title={t('Update')}
    onClick={() => setVisibleAndCallback(false)}
  >
    <UpdateAction
      id={id}
      domainName={domainName}
      ownerClass={ownerClass}
      columnNameInOwnerClass={columnNameInOwnerClass}
      callback={updateCallback}
      ownerId={ownerId}
      errorMsg={domainPermit.canUpdateErrorMsg}
      formId={updateFormId}
      zIndex={zIndex}
      showIconAndText={true}
      switchEditable={() => {
        if (detailModalReadonly) {
          domainPermit.canUpdate && setDetailModalReadonly(false);
        } else {
          setDetailModalReadonly(true);
        }
      }}
      readonly={!domainPermit.canUpdate || !detailModalReadonly}
      fetchType={fetchType}
    />
  </Menu.Item>);
 
  const deleteAction = (<Menu.Item
    key="delete"
    title={t('Delete')}
    onClick={() => setVisibleAndCallback(false)}
  >
    <DeleteComponent
      id={id}
      domainName={domainName}
      callback={deleteCallback}
      errorMsg={domainPermit.canDeleteErrorMsg}
      zIndex={zIndex}
    />
  </Menu.Item>);
 
  const actions = showActions ? (<ActionButtons
    domainName={domainName}
    fetchDataCallback={updateCallback}
    visiblePopover={visiblePopover}
    setVisiblePopoverCallback={(key?: string) => setVisiblePopover(key)}
    selectedData={[{ id }]}
    mode="single"
    zIndex={zIndex}
    direction="vertical"
  />) : (<></>);
 
  const isEditOrDetailPage = ["edit", "detail"].includes(page);
 
  const menus = (
    <Menu>
      <Divider>{t('Basic operations')}</Divider>
      {loading && <LoadingOutlined
        style={{
          textAlign: "center",
          margin: "auto",
          width: "100%",
          display: "inline-block",
        }} />}
      {!loading && !isEditOrDetailPage && detailAction}
      {!loading && domainPermit.canUpdate && !isEditOrDetailPage && editAction}
      {!loading && domainPermit.canDelete && deleteAction}
      {!loading && showActions && <Divider>{t('Actions')}</Divider>}
      {!loading && showActions && actions}
    </Menu>
  );
 
  return (
    <span
      className="operation-container"
      onClick={(e) => stopPropagationAndPreventDefault(e)}
    >
      <Dropdown
        overlay={menus}
        trigger={['click']}
        open={menuVisible}
        onOpenChange={setVisibleAndCallback}
        placement={"bottomRight"}
        overlayClassName="operation-dropdown"
      >
        <SettingOutlined
          className={`show-operate-icon ${(page === 'list') ? '' : 'link-icon'}`}
          title={t("Click to see available operations for this object")}
        />
      </Dropdown>
    </span>
  );
};
 
export default Operations;