EditModal.tsx 2.95 KB
import React, { useEffect, useState } from "react";
import { Modal, Form, Select, message, Skeleton } from "antd";
import { save, ListVO, queryBacklogList, BacklogItemVO, queryThresholds } from "../api";
import useInitial from "@/hooks/useInitail";
import SettingList from "./SettingList";

interface Props {
  visible: boolean;
  onCancel: () => void;
  item?: ListVO;
  onRefresh?: () => void;
}

export default ({ visible, onCancel, item, onRefresh }: Props) => {
  const [form] = Form.useForm();
  const [saveLoading, setSaveLoading] = useState(false);
  const [loading, setLoading] = useState<boolean>();
  const { data: BacklogList, setParams } = useInitial<BacklogItemVO[], any>(queryBacklogList, [], {});

  useEffect(() => {
    if (visible) {
      setParams({}, true);
    }
    if (visible && item?.id) {
      setLoading(true);
      queryThresholds({ id: item?.id })
        .then((res) => {
          form.setFieldsValue({
            configId: { value: item.configId, label: item.itemName },
            interveneList: res.data || [],
          });
          setLoading(false);
        })
        .catch((e) => {
          message.error(`干预配置查询失败:${e.message}`);
          setLoading(false);
        });
    }
    if (!visible) {
      setSaveLoading(false);
      form?.resetFields();
    }
  }, [visible]);

  function handleSave(feildValue: any) {
    setSaveLoading(true);
    const params = {
      ...feildValue,
      configId: feildValue?.configId.value,
      id: item?.id,
    };
    save(params)
      .then(() => {
        message.success("操作成功");
        onCancel?.();
        onRefresh?.();
      })
      .catch((e) => message.error(e.message))
      .finally(() => setSaveLoading(false));
  }

  return (
    <Modal
      open={visible}
      onCancel={onCancel}
      onOk={form.submit}
      title={item?.id ? "编辑超时干预" : "新增超时干预"}
      confirmLoading={saveLoading}
      width="65%"
    >
      <Skeleton active loading={loading}>
        <Form form={form} onFinish={handleSave} wrapperCol={{ span: 18 }} labelCol={{ span: 4 }}>
          <Form.Item name="configId" label="待办" rules={[{ required: true, message: "待办不能为空" }]}>
            <Select
              placeholder="请选择"
              disabled={!!item?.id}
              showSearch
              optionFilterProp="children"
              labelInValue
              filterOption={(input, option: any) => option?.children.indexOf(input) >= 0}
            >
              {BacklogList.map((item) => (
                <Select.Option value={item.id} key={item.id}>
                  {item.itemName}
                </Select.Option>
              ))}
            </Select>
          </Form.Item>
          <Form.Item name="interveneList" label="干预设置" rules={[{ required: true, message: "干预设置不能为空" }]}>
            <SettingList saveLoading={saveLoading} />
          </Form.Item>
        </Form>
      </Skeleton>
    </Modal>
  );
};