File size: 1,836 Bytes
b9c5b85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useParseDocument } from '@/hooks/document-hooks';
import { useResetFormOnCloseModal } from '@/hooks/logic-hooks';
import { IModalProps } from '@/interfaces/common';
import { Button, Form, Input, Popover } from 'antd';
import { PropsWithChildren } from 'react';
import { useTranslation } from 'react-i18next';

const reg =
  /^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?/;

export const PopoverForm = ({
  children,
  visible,
  switchVisible,
}: PropsWithChildren<IModalProps<any>>) => {
  const [form] = Form.useForm();
  const { parseDocument, loading } = useParseDocument();
  const { t } = useTranslation();

  useResetFormOnCloseModal({
    form,
    visible,
  });

  const onOk = async () => {
    const values = await form.validateFields();
    const val = values.url;

    if (reg.test(val)) {
      const ret = await parseDocument(val);
      if (ret?.data?.code === 0) {
        form.setFieldValue('result', ret?.data?.data);
        form.submit();
      }
    }
  };

  const content = (
    <Form form={form} name="urlForm">
      <Form.Item
        name="url"
        rules={[{ required: true, type: 'url' }]}
        className="m-0"
      >
        <Input
          onPressEnter={(e) => e.preventDefault()}
          placeholder={t('flow.pasteFileLink')}
          suffix={
            <Button
              type="primary"
              onClick={onOk}
              size={'small'}
              loading={loading}
            >
              {t('common.submit')}
            </Button>
          }
        />
      </Form.Item>
      <Form.Item name={'result'} noStyle />
    </Form>
  );

  return (
    <Popover
      content={content}
      open={visible}
      trigger={'click'}
      onOpenChange={switchVisible}
    >
      {children}
    </Popover>
  );
};