ConfirmReceiptModal.tsx
3.26 KB
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
import { postServiceOrderConfirmReceipt } from '@/services';
import { UploadOutlined } from '@ant-design/icons';
import { ModalForm } from '@ant-design/pro-components';
import { Button, Form, Upload } from 'antd';
import { RcFile, UploadFile, UploadProps } from 'antd/es/upload';
import { useState } from 'react';
export default ({ data, onClose }) => {
const [form] = Form.useForm<{ name: string; company: string }>();
const [previewOpen, setPreviewOpen] = useState(false);
const [previewImage, setPreviewImage] = useState('');
const [previewTitle, setPreviewTitle] = useState('');
console.log(previewOpen);
console.log(previewImage);
console.log(previewTitle);
const getBase64 = (file: RcFile): Promise<string> =>
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result as string);
reader.onerror = (error) => reject(error);
});
// const beforeUpload = (file: RcFile) => {
// const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
// if (!isJpgOrPng) {
// message.error('You can only upload JPG/PNG file!');
// }
// const isLt2M = file.size / 1024 / 1024 < 2;
// if (!isLt2M) {
// message.error('Image must smaller than 2MB!');
// }
// return isJpgOrPng && isLt2M;
// };
const [fileList, setFileList] = useState<UploadFile[]>([]);
const [uploading, setUploading] = useState(false);
const handlePreview = async (file: UploadFile) => {
if (!file.url && !file.preview) {
file.preview = await getBase64(file.originFileObj as RcFile);
}
setPreviewImage(file.url || (file.preview as string));
setPreviewOpen(true);
setPreviewTitle(
file.name || file.url!.substring(file.url!.lastIndexOf('/') + 1),
);
};
const handleUpload = async () => {
const formData = new FormData();
// fileList.forEach((file) => {
// formData.append('files[]', file as RcFile);
// });
formData.append('file', fileList[0] as RcFile);
formData.append('id', data.id);
setUploading(true);
// You can use any AJAX library you like
const res = await postServiceOrderConfirmReceipt({ data: formData });
console.log(res);
setUploading(false);
};
const props: UploadProps = {
onRemove: (file) => {
const index = fileList.indexOf(file);
const newFileList = fileList.slice();
newFileList.splice(index, 1);
setFileList(newFileList);
},
beforeUpload: (file) => {
setFileList([...fileList, file]);
return false;
},
onPreview: handlePreview,
fileList,
};
return (
<ModalForm<{
name: string;
company: string;
}>
width={500}
open
title="确认收货"
form={form}
autoFocusFirstInput
submitTimeout={2000}
onFinish={async () => {
onClose();
}}
>
<Upload {...props}>
<Button icon={<UploadOutlined />}>Select File</Button>
</Upload>
<Button
type="primary"
onClick={handleUpload}
disabled={fileList.length === 0}
loading={uploading}
style={{ marginTop: 16 }}
>
{uploading ? 'Uploading' : 'Start Upload'}
</Button>
</ModalForm>
);
};