Modal.tsx 1.75 KB
import React, { useEffect, useState } from "react";
import { Form, Input, message, Modal as AntdModal, Spin } from "antd";
import { save<feewee>替换name</feewee>Api } from "../api";
import { useStore } from "../index";

export default function Modal() {
  const { pagination, current, setCurrent, open, setOpen } = useStore();
  const [form] = Form.useForm();
  const [confirmLoading, setConfirmLoading] = useState(false);

  useEffect(() => {
    if (open && current) {
      form.setFieldsValue(current);
    } else {
      form.resetFields();
    }
  }, [open, current]);

  function onOk(val: any) {
    const hide = message.loading('提交中,请稍后...', 0);
    setConfirmLoading(true);
    const params: <feewee>替换name</feewee>.ListVO = {
      ...val,
      id: current?.id,
    };
    save<feewee>替换name</feewee>Api(params)
      .then((res) => {
        hide();
        setConfirmLoading(false);
        message.success(res.result);
        onCancel();
        pagination.setLoading(true);
      })
      .catch((error) => {
        hide();
        setConfirmLoading(false);
        message.error(error.message);
      });
  }

  function onCancel() {
    setCurrent(undefined);
    setOpen(false);
  }

  return (
    <AntdModal
      title={`${current ? '编辑' : '添加'}<feewee>替换name</feewee>`}
      open={open}
      maskClosable={false}
      onOk={form.submit}
      confirmLoading={confirmLoading}
      onCancel={onCancel}
    >
      <Spin spinning={confirmLoading}>
        <Form form={form} onFinish={onOk}>
          <Form.Item label="name" name="name" rules={[{ required: true, message: "请填写name" }]}>
            <Input allowClear placeholder="请填写name" />
          </Form.Item>
        </Form>
      </Spin>
    </AntdModal>
  );
}