|
| 1 | +import { faFileCsv, faFileExcel } from "@fortawesome/free-solid-svg-icons"; |
| 2 | +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; |
| 3 | +import DownloadIcon from "@mui/icons-material/Download"; |
| 4 | +import { ListItemIcon } from "@mui/material"; |
| 5 | +import Button from "@mui/material/Button"; |
| 6 | +import Menu from "@mui/material/Menu"; |
| 7 | +import MenuItem from "@mui/material/MenuItem"; |
| 8 | +import queryString from "query-string"; |
| 9 | +import { Fragment, MouseEvent, useState } from "react"; |
| 10 | +import { useListContext, useNotify } from "react-admin"; |
| 11 | + |
| 12 | +import axios_instance from "../../access_control/auth_provider/axios_instance"; |
| 13 | +import { getIconAndFontColor } from "../../commons/functions"; |
| 14 | + |
| 15 | +const ExportMenu = () => { |
| 16 | + const notify = useNotify(); |
| 17 | + const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null); |
| 18 | + const open = Boolean(anchorEl); |
| 19 | + const { filterValues, sort } = useListContext(); |
| 20 | + const handleClick = (event: MouseEvent<HTMLButtonElement>) => { |
| 21 | + setAnchorEl(event.currentTarget); |
| 22 | + }; |
| 23 | + const handleClose = () => { |
| 24 | + setAnchorEl(null); |
| 25 | + }; |
| 26 | + |
| 27 | + const exportDataCsv = async (url: string, filename: string, message: string) => { |
| 28 | + axios_instance |
| 29 | + .get(url) |
| 30 | + .then(function (response) { |
| 31 | + const blob = new Blob([response.data], { type: "text/csv" }); |
| 32 | + const url = window.URL.createObjectURL(blob); |
| 33 | + const link = document.createElement("a"); |
| 34 | + link.href = url; |
| 35 | + link.download = filename; |
| 36 | + link.click(); |
| 37 | + |
| 38 | + notify(message + " downloaded", { |
| 39 | + type: "success", |
| 40 | + }); |
| 41 | + }) |
| 42 | + .catch(function (error) { |
| 43 | + notify(error.message, { |
| 44 | + type: "warning", |
| 45 | + }); |
| 46 | + }); |
| 47 | + handleClose(); |
| 48 | + }; |
| 49 | + |
| 50 | + const exportDataExcel = async (url: string, filename: string, message: string) => { |
| 51 | + axios_instance |
| 52 | + .get(url, { |
| 53 | + responseType: "arraybuffer", |
| 54 | + headers: { Accept: "*/*" }, |
| 55 | + }) |
| 56 | + .then(function (response) { |
| 57 | + const blob = new Blob([response.data], { |
| 58 | + type: "application/zip", |
| 59 | + }); |
| 60 | + const url = window.URL.createObjectURL(blob); |
| 61 | + const link = document.createElement("a"); |
| 62 | + link.href = url; |
| 63 | + link.download = filename; |
| 64 | + link.click(); |
| 65 | + |
| 66 | + notify(message + " downloaded", { |
| 67 | + type: "success", |
| 68 | + }); |
| 69 | + }) |
| 70 | + .catch(function (error) { |
| 71 | + notify(error.message, { |
| 72 | + type: "warning", |
| 73 | + }); |
| 74 | + }); |
| 75 | + handleClose(); |
| 76 | + }; |
| 77 | + |
| 78 | + const exportObservationsExcel = async () => { |
| 79 | + exportDataExcel("/observations/export_excel/?" + queryParams(), "open_observations.xlsx", "Observations"); |
| 80 | + }; |
| 81 | + |
| 82 | + const exportObservationsCsv = async () => { |
| 83 | + exportDataCsv("/observations/export_csv/?" + queryParams(), "open_observations.csv", "Observations"); |
| 84 | + }; |
| 85 | + |
| 86 | + const queryParams = () => { |
| 87 | + const query = { ...filterValues, ...sort }; |
| 88 | + return queryString.stringify(query); |
| 89 | + }; |
| 90 | + |
| 91 | + return ( |
| 92 | + <Fragment> |
| 93 | + <Button |
| 94 | + id="export-button" |
| 95 | + aria-controls={open ? "export-menu" : undefined} |
| 96 | + aria-haspopup="true" |
| 97 | + aria-expanded={open ? "true" : undefined} |
| 98 | + onClick={handleClick} |
| 99 | + size="small" |
| 100 | + sx={{ paddingTop: 0, paddingBottom: 0, paddingLeft: "5px", paddingRight: "5px" }} |
| 101 | + startIcon={<DownloadIcon />} |
| 102 | + > |
| 103 | + Export |
| 104 | + </Button> |
| 105 | + <Menu |
| 106 | + id="basic-menu" |
| 107 | + anchorEl={anchorEl} |
| 108 | + open={open} |
| 109 | + onClose={handleClose} |
| 110 | + MenuListProps={{ |
| 111 | + "aria-labelledby": "basic-button", |
| 112 | + }} |
| 113 | + > |
| 114 | + <MenuItem onClick={exportObservationsExcel}> |
| 115 | + <ListItemIcon> |
| 116 | + <FontAwesomeIcon icon={faFileExcel} color={getIconAndFontColor()} /> |
| 117 | + </ListItemIcon> |
| 118 | + Observations / Excel |
| 119 | + </MenuItem> |
| 120 | + <MenuItem onClick={exportObservationsCsv}> |
| 121 | + <ListItemIcon> |
| 122 | + <FontAwesomeIcon icon={faFileCsv} color={getIconAndFontColor()} /> |
| 123 | + </ListItemIcon> |
| 124 | + Observations / CSV |
| 125 | + </MenuItem> |
| 126 | + </Menu> |
| 127 | + </Fragment> |
| 128 | + ); |
| 129 | +}; |
| 130 | + |
| 131 | +export default ExportMenu; |
0 commit comments