All files / src/form/create CreateAction.tsx

48% Statements 72/150
38.96% Branches 30/77
36.58% Functions 15/41
48.29% Lines 71/147

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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335                                                          66x           36x 36x     36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x   36x 36x 36x 36x 36x 36x   36x     36x             36x             36x             36x   4x     36x       4x           36x 4x 4x     36x 4x 4x     36x 12x 12x 9x 7x 7x 7x 7x             7x     12x 4x                             4x 3x 3x 3x 3x             3x       8x       36x 4x 1x   3x 3x             36x                                                       36x                               36x                               36x       36x   36x                                 36x                                                     36x                                           36x                                                    
import React, { useCallback, useContext, useEffect, useState } from 'react';
import { PlusOutlined } from '@ant-design/icons';
import { useTranslation } from 'react-i18next';
import {
  ContinueOperate, CreateProps, FormExtInfoProps, SaveOperation, SaveRecordProps, TableMetaProps,
  UpdateProps
} from '@props/RecordProps';
import { Modal, Space, Spin } from "antd";
import CreateComponent from "../create/App";
import { fullDomainNameToHumanReadable } from "@utils/StringUtils";
import {
  fetchCanCreate, fetchFormIdAndExtInfoByType, fetchDomainMeta,
  fetchFormExtInfo
} from "@utils/FetchUtils";
import { UpdateComponent, modalPropsBuilder, SaveLoadingButtonType } from "../";
import { emptyMethod } from '@utils/Constants';
import { shouldDisplayDetailPanel } from '@utils/ColumnsUtils';
import { useLocation } from 'react-router-dom';
import { useHotkeys } from 'react-hotkeys-hook';
import { ModalLockContext } from '@utils/context';
import { stopPropagationAndPreventDefault } from '@utils/ObjectUtils';
import { ModalPropsBuilderProps } from "../ModalPropsBuilder";
 
export type CallbackProps = {
  continueOperate: ContinueOperate;
  data?: SaveRecordProps;
  status: 'success' | 'fail';
};
 
export const CreateAction: React.FC<CreateProps> = (props: CreateProps) => {
  const {
    formId, domainName, ownerClass, ownerId,
    callback, columnNameInOwnerClass, initialCanCreate,
    initShowCreateModal, zIndex, showIconAndText, onCancelCallback,
    queryParameters, refererUrl, formName,
  } = props;
  const [showModal, setShowModal] = useState<boolean>(
    initShowCreateModal == null ? false : initShowCreateModal
  );
  const [saveLoadingButton, setSaveLoadingButton] = useState<SaveLoadingButtonType>(undefined);
  const [canCreate, setCanCreate] = useState<boolean>(false);
  const [continueOperate, setContinueOperate] = useState<ContinueOperate>(false);
  const refreshListPageRef = React.useRef<boolean>(false);
  const [isValid, setIsValid] = useState<boolean>(true);
  const [data, setData] = useState<SaveRecordProps>();
  const [columns, setColumns] = useState<Array<TableMetaProps>>();
  const [hasRelateObjectField, setHasRelateObjectField] = useState<boolean>(false);
  const [updateFormId, setUpdateFormId] = useState<number>();
  const [updateColumns, setUpdateColumns] = useState<Array<TableMetaProps>>();
  const [updateHasRelateObjectField, setUpdateHasRelateObjectField] = useState<boolean>(false);
  const [triggerSave, setTriggerSave] = useState<number | false>(false);
  const [mode, setMode] = useState<SaveOperation>('create');
  const { t } = useTranslation();
  const location = useLocation();
  const currentUrl = refererUrl ?? location.pathname;
  /** 编辑 Modal 的只读状态,在点击创建并编辑弹出的 Modal 中,可以点击编辑按钮,将其状态修改为查看状态 */
  const [detailModalReadonly, setDetailModalReadonly] = useState<boolean>(false);
  const [loading, setLoading] = useState(true);
  const { acquireLock, releaseLock, hasLock } = useContext(ModalLockContext);
  const [lockId, setLockId] = useState(0);
  const [cancelConfirmMessage, setCancelConfirmMessage] = useState<string | undefined>();
  const [extInfo, setExtInfo] = useState<FormExtInfoProps>();
 
  const useHotKeysConfig = {
    enableOnFormTags: true
  };
  useHotkeys('shift+ctrl+s, shift+cmd+s', () => {
    if (!hasLock(lockId)) {
      console.log('Shortcut shift+ctrl+s ignored due to global lock.');
      return;
    }
    onSaveAndContinue(mode, mode as ContinueOperate);
  }, useHotKeysConfig);
  useHotkeys('ctrl+enter, cmd+enter', () => {
    if (!hasLock(lockId)) {
      console.log('Shortcut ctrl+enter ignored due to global lock.');
      return;
    }
    onSaveAndContinue(mode, false);
  }, useHotKeysConfig);
  useHotkeys('shift+ctrl+e, shift+cmd+e', () => {
    if (!hasLock(lockId)) {
      console.log('Shortcut shift+ctrl+e ignored due to global lock.');
    }
    onSaveAndContinue("create", "edit");
  }, useHotKeysConfig);
 
  useEffect(() => {
    //Update show modal state when domainName changes
    setShowModal(initShowCreateModal == null ? false : initShowCreateModal);
  }, [domainName, initShowCreateModal]);
 
  useEffect(() => {
    //Only get lock when modal is shown and current lockId is 0, which means
    //this modal is just shown for the first time. This is to avoid infinite
    //loop of acquiring lock when popup other modal on top of a create modal.
    Iif (showModal && lockId === 0) {
      const newLockId = acquireLock();
      setLockId(newLockId);
    }
  }, [showModal, acquireLock, hasLock, lockId]);
 
  useEffect(() => {
    const dataIsNull = (data == null);
    setMode(dataIsNull ? "create" : "edit");
  }, [data]);
 
  useEffect(() => {
    setColumns([]);
    setUpdateColumns([]);
  }, [domainName]);
 
  useEffect(() => {
    setLoading(true);
    if (updateFormId == null) {
      fetchFormIdAndExtInfoByType(domainName, 'Update').then(json => {
        fetchDomainMeta(domainName, json.id).then((domainMeta: Array<TableMetaProps>) => {
          setUpdateFormId(json.id);
          setUpdateColumns(domainMeta);
          setUpdateHasRelateObjectField(domainMeta.filter(c => shouldDisplayDetailPanel(mode, c)).length > 0);
        }).catch(e => {
          console.error(`Failed to get form(${json.id}) meta of domain ${domainName} form: ${e}`);
        });
      }).catch(e => {
        console.error(`Failed to get form Id of domain ${domainName} Update form: ${e}`);
      }).finally(() => {
        setLoading(false);
      });
    }
    if (columns == null) {
      Iif (formId != null) {
        fetchDomainMeta(domainName, formId).then((domainMeta: Array<TableMetaProps>) => {
          setColumns(domainMeta);
          setHasRelateObjectField(domainMeta.filter(c => shouldDisplayDetailPanel(mode, c)).length > 0);
        }).catch(e => {
          console.error(`Failed to get form(${formId}) meta of domain ${domainName} form: ${e}`);
        }).finally(() => {
          setLoading(false);
        });
        fetchFormExtInfo(formId).then(json => {
          setExtInfo(json.extInfo);
        }).catch(e => {
          console.error(`Failed to get form(${formId}) meta of domain ${domainName} form: ${e}`);
        });
      } else {
        fetchFormIdAndExtInfoByType(domainName, 'Create').then(json => {
          setExtInfo(json.extInfo);
          fetchDomainMeta(domainName, json.id).then((domainMeta: Array<TableMetaProps>) => {
            setColumns(domainMeta);
            setHasRelateObjectField(domainMeta.filter(c => shouldDisplayDetailPanel(mode, c)).length > 0);
          }).catch(e => {
            console.error(`Failed to get form(${json.id}) meta of domain ${domainName} form: ${e}`);
          });
        }).catch(e => {
          console.error(`Failed to get form Id of domain ${domainName} Update form: ${e}`);
        }).finally(() => {
          setLoading(false);
        });
      }
    } else {
      setLoading(false);
    }
  }, [columns, domainName, formId, mode, updateFormId]);
 
  useEffect(() => {
    if (initialCanCreate != null) {
      setCanCreate(initialCanCreate);
    } else {
      fetchCanCreate(domainName).then(json => {
        setCanCreate(json.create);
      }).catch(e => {
        console.error(`Failed to get canCreate for ${domainName}: ${JSON.stringify(e)}`);
      });
    }
  }, [domainName, initialCanCreate]);
 
  const onSaveCallback = (props: CallbackProps, operation: SaveOperation): void => {
    const { continueOperate, data, status } = props;
    if (status === 'success') {
      setShowModal(continueOperate !== false);
      setContinueOperate(continueOperate);
      setData((continueOperate === 'edit' ? data : undefined));
      // 如果用户点击了保存并编辑按钮后,将 mode 设置为 edit,
      // 这样就会显示编辑的 Modal, 隐藏创建的 Modal
      if (continueOperate === 'edit' && mode === 'create') {
        setMode('edit');
      }
      //Only refresh after return to list panel to avoid duplicate API call
      if (continueOperate === false) {
        callback?.(props);
        onCancelCallback?.();
        releaseLock(lockId);
      }
    } else {
      setShowModal(true);
      if (!['wizard', 'edit-related', 'create-related']
        .includes(operation)) {
        setContinueOperate(operation as ContinueOperate);
      }
    }
    setTriggerSave(false);
    setSaveLoadingButton(undefined);
  };
 
  const onCancel = useCallback(() => {
    setCancelConfirmMessage(undefined);
    setShowModal(false);
    setTriggerSave(false);
    onCancelCallback?.();
    // Only trigger list page refresh when return to list page
    if (refreshListPageRef.current) {
      refreshListPageRef.current = false;
      callback?.({ data, continueOperate: false, status: 'success' });
      setData(undefined);
    }
    if (lockId != null) {
      releaseLock(lockId);
    }
  }, [callback, data, onCancelCallback, lockId, releaseLock]);
 
  const onSaveAndContinue = (operation: SaveOperation, nextAction: ContinueOperate): void => {
    setCancelConfirmMessage(undefined);
    if (nextAction === false) {
      setSaveLoadingButton("saveClose");
    } else if (operation === 'create' && nextAction === 'edit') {
      //创建时点击保存并编辑的处理
      setSaveLoadingButton("saveEdit");
    } else if (operation === nextAction) {
      setSaveLoadingButton("saveContinueCreate");
    }
    refreshListPageRef.current = true;
    setContinueOperate(nextAction);
    const myTriggerSave = (operation === mode) ? Math.random() : false;
    setTriggerSave(myTriggerSave);
  };
 
  const switchEditable = (editable: boolean): void => {
    setDetailModalReadonly(!editable);
  };
 
  const isEditMode = (mode === 'edit');
 
  const modalParams = {
    isValid, mode, open: showModal, domainName,
    hasRelateObjectField: isEditMode ? updateHasRelateObjectField : hasRelateObjectField,
    // 保存并关闭的回调
    onSaveAndClose: () => onSaveAndContinue(mode, false),
    // 保存并继续当前操作的回调(如在创建时,保存并继续创建,在编辑时保存并继续编辑)
    onSaveAndContinue: () => onSaveAndContinue(mode, mode as ContinueOperate),
    // 创建时,保存并跳转到编辑页面的回调
    onSaveAndContinueEdit: () => onSaveAndContinue("create", "edit"),
    onCancel, id: data?.id, onDelete: emptyMethod, saveLoadingButton,
    zIndex: zIndex + 1, t, refererUrl: currentUrl,
    switchEditable,
    readonly: detailModalReadonly,
    key: `create_${domainName}_${mode}_modal_editable`,
    cancelConfirmMessage,
    modalTitle: extInfo?.domainTitle,
  } as ModalPropsBuilderProps;
  const componentParams: CreateProps | UpdateProps = {
    formId: isEditMode ? updateFormId : formId,
    domainName, showContinueOperate: true,
    ownerClass, ownerId, columnNameInOwnerClass, continueOperate,
    triggerSave, zIndex, queryParameters,
    validationCallback: (v: boolean) => {
      setIsValid(v);
 
      //如果 valid 是 true, 则重置 button 的 loading 状态的逻辑由 saveCallback 处理
      //此处只处理 valid 是 false 的状态
      if (!v) {
        setSaveLoadingButton(undefined);
      }
 
      setTimeout(() => {
        setIsValid(true);
      }, 2000);
    },
    callback: (props: CallbackProps) => onSaveCallback(props, mode),
    columns: isEditMode ? updateColumns : columns,
    switchEditable,
    readonly: detailModalReadonly,
    formName,
    setCancelConfirmMessage,
  };
 
  const linkAndText = (
    <a
      href="/#"
      onClick={(event: React.MouseEvent<unknown>) => {
        if (!loading) {
          stopPropagationAndPreventDefault(event);
          setShowModal(true);
        }
      }}
      title={t("Create new domain", {
        domainTitle: fullDomainNameToHumanReadable(domainName),
      })}
      style={{ cursor: loading ? "not-allowed" : "pointer" }}
    >
      {loading ? (
        <Spin />
      ) : canCreate ? (<Space className="link-icon link-icon-with-label" size={2}>
        <PlusOutlined />
        {t('Create')}
      </Space>) : null}
    </a>
  );
 
  return (columns == null) ? (<Spin />) : (
    <span className="create-action">
      {showIconAndText ? linkAndText : (<></>)}
      {mode === "create" && (<Modal
        {...modalPropsBuilder(modalParams)}
        destroyOnClose={true}
        style={{ minWidth: "410px", width: "410px" }}
      >
        <CreateComponent
          {...componentParams}
        />
      </Modal>
      )}
      {mode === "edit" && data?.id != null && (
        <Modal {...modalPropsBuilder(modalParams)}>
          <UpdateComponent
            {...componentParams}
            id={data.id}
          />
        </Modal>
      )}
    </span>
  );
};
 
export default CreateAction;