-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathExportReportButton.jsx
More file actions
71 lines (65 loc) · 2.29 KB
/
ExportReportButton.jsx
File metadata and controls
71 lines (65 loc) · 2.29 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
import { useState } from 'react';
import axios from 'axios';
import { toast } from 'react-toastify';
import { ENDPOINTS } from '../../../utils/URL';
import { EXPORT_FORMATS } from './constants';
import styles from './UtilizationChart.module.css';
function ExportReportButton({ tool, project, startDate, endDate }) {
const [exportingFormat, setExportingFormat] = useState(null);
const handleExport = async format => {
setExportingFormat(format);
try {
const params = {
format,
tool,
project,
...(startDate && { startDate: startDate.toISOString() }),
...(endDate && { endDate: endDate.toISOString() }),
};
const response = await axios.get(ENDPOINTS.BM_TOOL_UTILIZATION_EXPORT, {
params,
headers: { Authorization: localStorage.getItem('token') },
responseType: 'blob',
});
const contentDisposition = response.headers['content-disposition'] || '';
const filenameMatch = contentDisposition.match(/filename="?([^"]+)"?/);
const filename = filenameMatch ? filenameMatch[1] : `tool-utilization-report.${format}`;
const url = window.URL.createObjectURL(response.data);
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
link.remove();
window.URL.revokeObjectURL(url);
} catch {
toast.error(`Failed to export ${format.toUpperCase()} report.`);
} finally {
setExportingFormat(null);
}
};
return (
<div className={styles.exportSection}>
<span className={styles.exportLabel}>Export Report:</span>
<button
type="button"
className={styles.exportButton}
onClick={() => handleExport(EXPORT_FORMATS.PDF)}
disabled={exportingFormat !== null}
aria-label="Export as PDF"
>
{exportingFormat === EXPORT_FORMATS.PDF ? 'Exporting...' : 'PDF'}
</button>
<button
type="button"
className={styles.exportButton}
onClick={() => handleExport(EXPORT_FORMATS.CSV)}
disabled={exportingFormat !== null}
aria-label="Export as CSV"
>
{exportingFormat === EXPORT_FORMATS.CSV ? 'Exporting...' : 'CSV'}
</button>
</div>
);
}
export default ExportReportButton;