File size: 2,972 Bytes
40a88fa
4056097
40a88fa
 
 
 
 
 
 
 
 
 
 
 
4056097
 
 
40a88fa
 
 
 
 
 
 
 
 
4056097
 
40a88fa
 
 
 
 
4056097
40a88fa
 
 
 
 
 
4056097
40a88fa
 
4056097
 
40a88fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4056097
40a88fa
 
4056097
 
40a88fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4056097
 
 
 
40a88fa
 
 
 
4056097
40a88fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4056097
 
 
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { IModalProps } from '@/interfaces/common';
import { InboxOutlined } from '@ant-design/icons';
import {
  Flex,
  Modal,
  Segmented,
  Tabs,
  TabsProps,
  Upload,
  UploadFile,
  UploadProps,
} from 'antd';
import { Dispatch, SetStateAction, useState } from 'react';
import { useHandleUploadFile } from '../hooks';

const { Dragger } = Upload;

const FileUpload = ({
  directory,
  fileList,
  setFileList,
}: {
  directory: boolean;
  fileList: UploadFile[];
  setFileList: Dispatch<SetStateAction<UploadFile[]>>;
}) => {
  const props: UploadProps = {
    multiple: true,
    onRemove: (file) => {
      const index = fileList.indexOf(file);
      const newFileList = fileList.slice();
      newFileList.splice(index, 1);
      setFileList(newFileList);
    },
    beforeUpload: (file) => {
      setFileList((pre) => {
        return [...pre, file];
      });

      return false;
    },
    directory,
    fileList,
  };

  return (
    <Dragger {...props}>
      <p className="ant-upload-drag-icon">
        <InboxOutlined />
      </p>
      <p className="ant-upload-text">
        Click or drag file to this area to upload
      </p>
      <p className="ant-upload-hint">
        Support for a single or bulk upload. Strictly prohibited from uploading
        company data or other banned files.
      </p>
    </Dragger>
  );
};

const FileUploadModal = ({ visible, hideModal }: IModalProps<any>) => {
  const [value, setValue] = useState<string | number>('local');
  const { onFileUploadOk, fileUploadLoading } = useHandleUploadFile();
  const [fileList, setFileList] = useState<UploadFile[]>([]);
  const [directoryFileList, setDirectoryFileList] = useState<UploadFile[]>([]);

  const onOk = () => {
    onFileUploadOk([...fileList, ...directoryFileList]);
  };

  const items: TabsProps['items'] = [
    {
      key: '1',
      label: 'File',
      children: (
        <FileUpload
          directory={false}
          fileList={fileList}
          setFileList={setFileList}
        ></FileUpload>
      ),
    },
    {
      key: '2',
      label: 'Directory',
      children: (
        <FileUpload
          directory
          fileList={directoryFileList}
          setFileList={setDirectoryFileList}
        ></FileUpload>
      ),
    },
  ];

  return (
    <>
      <Modal
        title="File upload"
        open={visible}
        onOk={onOk}
        onCancel={hideModal}
        confirmLoading={fileUploadLoading}
      >
        <Flex gap={'large'} vertical>
          <Segmented
            options={[
              { label: 'Local uploads', value: 'local' },
              { label: 'S3 uploads', value: 's3' },
            ]}
            block
            value={value}
            onChange={setValue}
          />
          {value === 'local' ? (
            <Tabs defaultActiveKey="1" items={items} />
          ) : (
            'coming soon'
          )}
        </Flex>
      </Modal>
    </>
  );
};

export default FileUploadModal;