create.config.ts 2.74 KB
import { EntityTypeEnum } from '../../../enum/form';
import { useRuleChainI18n } from '../../../hook/useRuleChainI18n';
import {
  JavascriptEditorWithTestModal,
  ScriptEditorValueType,
} from '../../../src/components/JavaScriptFilterModal';
import { DefaultNodeConfig } from '../../../types/node';
import { getEntityIdSelect } from '../../Filter/CheckRelation/create.config';
import { FormSchema, useComponentRegister } from '/@/components/Form';

const { tLabel, tPlaceholder, t } = useRuleChainI18n('action', 'generator');

useComponentRegister('JavascriptEditorWithTestModal', JavascriptEditorWithTestModal);

export interface FormFieldsValue {
  [GeneratorFieldsEnum.JS_SCRIPT]: ScriptEditorValueType;
}

export interface NodeConfigValue extends DefaultNodeConfig, ScriptEditorValueType {}

export enum GeneratorFieldsEnum {
  MSG_COUNT = 'msgCount',
  PERIOD_IN_SECONDS = 'periodInSeconds',
  JS_SCRIPT = 'jsScript',
  ORIGINATOR_ID = 'originatorId',
  ORIGINATOR_TYPE = 'originatorType',
}

export const formSchemas: FormSchema[] = [
  {
    field: GeneratorFieldsEnum.MSG_COUNT,
    component: 'InputNumber',
    label: tLabel(GeneratorFieldsEnum.MSG_COUNT),
    required: true,
    componentProps: {
      min: 0,
      placeholder: tPlaceholder(GeneratorFieldsEnum.MSG_COUNT),
    },
  },
  {
    field: GeneratorFieldsEnum.PERIOD_IN_SECONDS,
    component: 'InputNumber',
    label: tLabel(GeneratorFieldsEnum.PERIOD_IN_SECONDS),
    required: true,
    componentProps: {
      min: 0,
      placeholder: tPlaceholder(GeneratorFieldsEnum.PERIOD_IN_SECONDS),
    },
  },
  {
    field: GeneratorFieldsEnum.ORIGINATOR_TYPE,
    component: 'Select',
    label: tLabel(GeneratorFieldsEnum.ORIGINATOR_TYPE),
    colProps: { span: 8 },
    componentProps: {
      options: Object.keys(EntityTypeEnum).map((value) => ({
        label: t(`enum.entityType.${value}`),
        value,
      })),
      placeholder: tPlaceholder(GeneratorFieldsEnum.ORIGINATOR_TYPE, 'choose'),
      getPopupContainer: () => document.body,
    },
  },
  {
    field: GeneratorFieldsEnum.ORIGINATOR_ID,
    component: 'ApiSearchSelect',
    label: ' ',
    colProps: { span: 16 },
    componentProps: ({ formModel }) => {
      const type = formModel[GeneratorFieldsEnum.ORIGINATOR_TYPE];
      return getEntityIdSelect(type);
    },
  },
  {
    field: GeneratorFieldsEnum.JS_SCRIPT,
    component: 'JavascriptEditorWithTestModal',
    label: tLabel(GeneratorFieldsEnum.JS_SCRIPT),
    valueField: 'value',
    changeEvent: 'update:value',
    componentProps: {
      javaScriptEditorProps: {
        functionName: 'Generate',
        scriptType: 'generate',
        paramsName: ['prevMsg', 'prevMetadata', 'prevMsgType'],
      },
      buttonName: t('rule.chain.action.generator.fields.jsScriptButtonName'),
    },
  },
];