All files / src/form/delete DeleteComponent.tsx

63.63% Statements 7/11
80% Branches 8/10
25% Functions 1/4
63.63% Lines 7/11

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                    66x     2x 2x 2x   2x 2x                               2x                                    
import React from 'react';
import { useTranslation } from 'react-i18next';
import { Alert, Popconfirm, Space } from 'antd';
import { DeleteOutlined } from '@ant-design/icons';
import { DeletionProps } from '@props/RecordProps';
import { deleteRecord } from "@utils/FetchUtils";
import { stopPropagationAndPreventDefault } from '@utils/ObjectUtils';
import { fullDomainNameToHumanReadable } from "@utils/StringUtils";
 
//FIXME change to default export
export const DeleteComponent: React.FC<DeletionProps> = (props: DeletionProps) => {
  const {
    domainName, errorMsg, text, renderWithoutContainer, zIndex, displayElement
  } = props;
  const { t } = useTranslation();
  const displayLabel = (text ?? t("Delete"));
 
  if (errorMsg == null) {
    const content = displayElement ?? ((renderWithoutContainer) ? (
      <span
        onClick={(event: React.MouseEvent<unknown>) => stopPropagationAndPreventDefault(event)}
      > <DeleteOutlined /> {displayLabel} </span>
    ) : (
      <a
        href="/#"
        title={t('Delete current object')}
        onClick={(event: React.MouseEvent<unknown>) => stopPropagationAndPreventDefault(event)}
      >
        <Space size={8}>
          <DeleteOutlined />
          {t('Delete current object')}
        </Space>
      </a>
    ));
    return (<Popconfirm
      title={t('Are you sure to delete this', { domainTitle: fullDomainNameToHumanReadable(domainName) })}
      onConfirm={(): void => {
        //Callback is called inside deleteRecord method
        deleteRecord(props);
      }}
      okText={t("Confirm")}
      cancelText={t("Cancel")}
      // 这是为了避免在某些情况下, 删除的弹出确认框会被遮挡
      zIndex={(zIndex ?? 3) + 1050}
    >
      {content}
    </Popconfirm>
    );
  } else E{
    return (<Alert message="Error" description={errorMsg} type="error" showIcon />);
  }
};