-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCrossSearchExportMenuItem.tsx
More file actions
61 lines (56 loc) · 1.92 KB
/
CrossSearchExportMenuItem.tsx
File metadata and controls
61 lines (56 loc) · 1.92 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
import { useState } from 'react'
import { useNotify } from '@/hooks/notification'
import { BACKEND_URL } from '@/util/config'
import { MenuItem } from '@mui/material'
import { useUser } from '@/hooks/user'
import { usePageContext } from '../Page'
import { currentDateAsString } from '@/shared/currentDateAsString'
import { downloadExportFileWithProgress } from '@/util/exportProgress'
export const CrossSearchExportMenuItem = ({ handleClose }: { handleClose: () => void }) => {
const [loading, setLoading] = useState(false)
const { sqlColumnFilters, sqlOrderBy } = usePageContext()
const { notify, setMessage: setNotificationMessage } = useNotify()
const token = useUser().token
const fetchOptions = token ? { headers: { Authorization: `Bearer ${token}` } } : {}
const filename = `locality-species-${currentDateAsString()}.csv`
const fetchCSVFile = async () => {
setLoading(true)
try {
await downloadExportFileWithProgress({
url: `${BACKEND_URL}/crosssearch/export`,
filename,
fetchOptions: {
...fetchOptions,
method: 'POST',
headers: {
...fetchOptions.headers,
'Content-Type': 'application/json',
},
body: JSON.stringify({ columnFilters: sqlColumnFilters, sorting: sqlOrderBy }),
},
notify,
setNotificationMessage,
startMessage: 'Generating cross-search CSV export...',
waitingMessage: 'Waiting for cross-search rows',
downloadMessage: 'Downloading CSV file',
failureMessage: 'Downloading cross-search export failed.',
contentType: 'text/csv',
})
} catch {
// downloadExportFileWithProgress owns the failure notification.
} finally {
setLoading(false)
}
}
return (
<MenuItem
onClick={() => {
void fetchCSVFile()
handleClose()
}}
disabled={loading}
>
Export table
</MenuItem>
)
}