File size: 2,076 Bytes
7b71fb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useKnowledgeBaseId } from '@/hooks/knowledgeHook';
import { Form, Input, Modal } from 'antd';
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'umi';

const RenameModal = () => {
  const [form] = Form.useForm();
  const dispatch = useDispatch();
  const kFModel = useSelector((state: any) => state.kFModel);
  const loading = useSelector(
    (state: any) => state.loading.effects['kFModel/document_rename'],
  );
  const knowledgeBaseId = useKnowledgeBaseId();
  const isModalOpen = kFModel.isShowRenameModal;
  const initialName = kFModel.currentRecord?.name;
  const documentId = kFModel.currentRecord?.id;

  type FieldType = {
    name?: string;
  };

  const closeModal = () => {
    dispatch({
      type: 'kFModel/setIsShowRenameModal',
      payload: false,
    });
  };

  const handleOk = async () => {
    const ret = await form.validateFields();

    dispatch({
      type: 'kFModel/document_rename',
      payload: {
        doc_id: documentId,
        name: ret.name,
        kb_id: knowledgeBaseId,
      },
    });
  };

  const handleCancel = () => {
    closeModal();
  };

  const onFinish = (values: any) => {
    console.log('Success:', values);
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log('Failed:', errorInfo);
  };

  useEffect(() => {
    form.setFieldValue('name', initialName);
  }, [initialName, documentId]);

  return (
    <Modal
      title="Rename"
      open={isModalOpen}
      onOk={handleOk}
      onCancel={handleCancel}
      okButtonProps={{ loading }}
    >
      <Form
        name="basic"
        labelCol={{ span: 4 }}
        wrapperCol={{ span: 20 }}
        style={{ maxWidth: 600 }}
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
        autoComplete="off"
        form={form}
      >
        <Form.Item<FieldType>
          label="Name"
          name="name"
          rules={[{ required: true, message: 'Please input name!' }]}
        >
          <Input />
        </Form.Item>
      </Form>
    </Modal>
  );
};

export default RenameModal;