EditModal.jsx 2.8 KB
Newer Older
皮倩雯's avatar
皮倩雯 committed
1 2 3 4
/*
 * @Description:
 * @Author: leizhe
 * @Date: 2022-01-13 17:26:14
5
 * @LastEditTime: 2022-03-22 16:22:17
皮倩雯's avatar
皮倩雯 committed
6 7
 * @LastEditors: leizhe
 */
Maofei94's avatar
Maofei94 committed
8 9
import React, { useState, useEffect, useRef } from 'react';
import { Form, Input, notification } from 'antd';
10
import SiteModal from '@/components/Modal/SiteModa';
邓超's avatar
邓超 committed
11
import { editStation } from '@/services/siteManage/api';
Maofei94's avatar
Maofei94 committed
12 13
const { Item } = Form;
const EditModal = props => {
14 15
  const [form] = Form.useForm();
  const [formLayout, setFormLayout] = useState('horizontal');
Maofei94's avatar
Maofei94 committed
16 17
  const [loading, setLoading] = useState(false);
  const flag = useRef();
18
  const { confirmModal, stationObj, des } = props;
Maofei94's avatar
Maofei94 committed
19 20 21 22 23 24 25 26 27
  const onSubmit = () => {
    form
      .validateFields()
      .then(res => {
        setLoading(true);
        flag.current = res;
        editStation({
          stationName: res.stationName,
          description: res.description,
28
          stationID: stationObj.id,
Maofei94's avatar
Maofei94 committed
29 30 31
        })
          .then(res => {
            setLoading(false);
皮倩雯's avatar
皮倩雯 committed
32
            if (res.msg === '') {
Maofei94's avatar
Maofei94 committed
33 34 35 36 37 38 39 40 41 42 43
              form.resetFields();
              notification.success({
                message: '通知',
                duration: 3,
                description: '编辑成功',
              });
              confirmModal();
            } else {
              notification.error({
                message: '提示',
                duration: 3,
44
                description: res.msg,
Maofei94's avatar
Maofei94 committed
45 46 47 48 49 50 51 52 53 54
              });
            }
          })
          .catch(err => {
            setLoading(false);
            notification.error({
              message: '提示',
              duration: 3,
              description: err,
            });
55
          });
Maofei94's avatar
Maofei94 committed
56 57 58 59 60 61
      })
      .catch(err => {
        console.error(err);
      });
  };
  useEffect(() => {
62
    form.setFieldsValue({
Maofei94's avatar
Maofei94 committed
63
      stationName: stationObj.text,
64
      description: stationObj.describe,
Maofei94's avatar
Maofei94 committed
65 66 67 68 69 70 71 72
    });
  }, [stationObj]);
  return (
    <SiteModal
      {...props}
      title="编辑站点"
      bodyStyle={{ width: '100%', minHeight: '100px' }}
      style={{ top: 200, borderRadius: '20px' }}
73
      width="600px"
皮倩雯's avatar
皮倩雯 committed
74
      maskClosable={false}
Maofei94's avatar
Maofei94 committed
75 76
      destroyOnClose
      cancelText="取消"
Maofei94's avatar
Maofei94 committed
77
      okText="确认"
Maofei94's avatar
Maofei94 committed
78
      onOk={() => onSubmit()}
79 80
      confirmLoading={loading}
    >
Maofei94's avatar
Maofei94 committed
81
      <Form form={form} layout={formLayout} labelCol={{ span: 4 }}>
Maofei94's avatar
Maofei94 committed
82 83 84 85 86 87 88 89 90
        <Item
          label="站点名称"
          name="stationName"
          rules={[
            {
              required: true,
              message: '请输入站点名称',
            },
          ]}
91
        >
92
          <Input placeholder="请输入站点名称" style={{ width: '95%' }} maxLength="20" />
Maofei94's avatar
Maofei94 committed
93 94
        </Item>
        <Item label="站点描述" name="description">
95
          <Input placeholder="请输入站点描述" style={{ width: '95%' }} maxLength="100" />
Maofei94's avatar
Maofei94 committed
96 97
        </Item>
      </Form>
98
    </SiteModal>
Maofei94's avatar
Maofei94 committed
99 100
  );
};
101

Maofei94's avatar
Maofei94 committed
102
export default EditModal;