skyouc
7 天以前 b9d414bc2d61b4824ce6a019b1c10f526f71ced1
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { useEffect, useState, createContext, useContext } from 'react';
import { Box, CircularProgress, Stack, Typography } from '@mui/material';
import Alert from '@mui/material/Alert';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
import MuiLink from '@mui/material/Link';
import {
    Button,
    FileField,
    FileInput,
    Form,
    Toolbar,
    useRefresh,
    useTranslate,
    useNotify
} from 'react-admin';
import { Link } from 'react-router-dom';
import DialogCloseButton from './DialogCloseButton';
import { usePapaParse } from './usePapaParse';
import MatnrList from '../basicInfo/matnr/MatnrList';
import request from '@/utils/request'
 
const ImportModal = ({ open, onClose, importTemp, useCodeImport, onceBatch = 10, value, parmas = {} }) => {
    const refresh = useRefresh();
    const translate = useTranslate();
 
    // const { processBatch } = useCodeImport();
    const { importer, parseCsv, reset } = usePapaParse({
        batchSize: onceBatch,
        // processBatch,
    });
 
    const [file, setFile] = useState(null);
    const notify = useNotify();
 
    useEffect(() => {
        if (importer.state === 'complete') {
            refresh();
        }
    }, [importer.state, refresh]);
 
    const handleFileChange = (file) => {
        setFile(file);
    };
 
    const startImport = async () => {
        if (!file) {
            return;
        }
        const form = new FormData();
        for (const key in parmas) {
            if (parmas.hasOwnProperty(key)) {
                form.append(key, parmas[key]);
            }
        }
        form.append('file', file);
        const { data: { code, data, msg } } = await request.post(`/${value}/import`, form)
 
        if (code === 200) {
            handleClose()
        } else {
            notify(msg);
 
        }
 
    };
 
 
 
    const handleClose = () => {
        reset();
        onClose();
    };
 
    const handleReset = (e) => {
        e.preventDefault();
        reset();
    };
 
    const downloadTemplate = async (type) => {
        const res = await request.post(`/${value}/template/download`, {}, {
            responseType: "blob",
        })
        const url = window.URL.createObjectURL(
            new Blob([res.data], { type: res.headers["content-type"] }),
        );
 
        const link = document.createElement("a");
        link.href = url;
        link.setAttribute("download", `${value}.xlsx`);
        document.body.appendChild(link);
        link.click();
        link.remove();
    }
 
    return (
        <Dialog open={open} maxWidth="md" fullWidth>
            <DialogCloseButton onClose={handleClose} />
            <DialogTitle>{translate('common.action.import.title')}</DialogTitle>
            <DialogContent>
                <Form>
                    <Stack spacing={2}>
                        {importer.state === 'running' && (
                            <Stack gap={2}>
                                <Alert
                                    severity="info"
                                    action={
                                        <Box
                                            sx={{
                                                display: 'flex',
                                                height: '100%',
                                                alignItems: 'center',
                                                padding: '0',
                                            }}
                                        >
                                            <CircularProgress size={20} />
                                        </Box>
                                    }
                                    sx={{
                                        alignItems: 'center',
                                        '& .MuiAlert-action': {
                                            padding: 0,
                                            marginRight: 0,
                                        },
                                    }}
                                >
                                    {translate('common.action.import.tips')}
                                </Alert>
                                <Typography variant="body2">
                                    Imported{' '}
                                    <strong>
                                        {importer.importCount} /{' '}
                                        {importer.rowCount}
                                    </strong>{' '}
                                    contacts, with{' '}
                                    <strong>{importer.errorCount}</strong>{' '}
                                    errors.
                                    {importer.remainingTime !== null && (
                                        <>
                                            {' '}
                                            Estimated remaining time:{' '}
                                            <strong>
                                                {millisecondsToTime(
                                                    importer.remainingTime
                                                )}
                                            </strong>
                                            .{' '}
                                            <MuiLink
                                                href="#"
                                                onClick={handleReset}
                                                color="error"
                                            >
                                                {translate('common.action.import.stop')}
                                            </MuiLink>
                                        </>
                                    )}
                                </Typography>
                            </Stack>
                        )}
 
                        {importer.state === 'error' && (
                            <Alert severity="error">
                                {translate('common.action.import.err')}
                            </Alert>
                        )}
 
                        {importer.state === 'complete' && (
                            <Alert severity="success">
                                {translate('common.action.import.result', {
                                    success: importer.importCount,
                                    error: importer.errorCount
                                })}
                            </Alert>
                        )}
 
                        {importer.state === 'idle' && (
                            <>
                                <Alert
                                    severity="info"
                                    action={
                                        <MatnrList.Context.Consumer>
                                            {context => (
                                                <Button
                                                    component={Link}
                                                    onClick={() => {
                                                        downloadTemplate(context)
                                                    }}
                                                    label="common.action.import.download"
                                                    color="info"
                                                    to={importTemp}
                                                    download={'import_template.csv'}
                                                />
                                            )}
 
                                        </MatnrList.Context.Consumer>
                                    }
                                >
                                    {translate('common.action.import.msg')}
                                </Alert>
 
                                <FileInput
                                    source="xlsx"
                                    label="Xlsx File"
                                    accept={{ 'text/xlsx': ['.xls', '.xlsx'] }}
                                    onChange={handleFileChange}
                                >
                                    <FileField source="src" title="title" />
                                </FileInput>
                            </>
                        )}
                    </Stack>
                </Form>
            </DialogContent>
            <DialogActions
                sx={{
                    p: 0,
                    justifyContent: 'flex-start',
                }}
            >
                <Toolbar
                    sx={{
                        width: '100%',
                    }}
                >
                    {importer.state === 'idle' ? (
                        <>
                            <Button
                                label="common.action.import.title"
                                variant="contained"
                                onClick={startImport}
                                disabled={!file}
                            />
                        </>
                    ) : (
                        <Button
                            label="ra.action.close"
                            onClick={handleClose}
                            disabled={importer.state === 'running'}
                        />
                    )}
                </Toolbar>
            </DialogActions>
        </Dialog>
    );
}
{/**下载打印模板,传入type类型,调用下载模板接口 */ }
 
 
function millisecondsToTime(ms) {
    var seconds = Math.floor((ms / 1000) % 60);
    var minutes = Math.floor((ms / (60 * 1000)) % 60);
 
    return `${minutes}m ${seconds}s`;
}
 
export default ImportModal;