| | |
| | | import * as React from "react"; |
| | | import { useCallback } from "react"; |
| | | import DownloadIcon from "@mui/icons-material/GetApp"; |
| | | import { |
| | | Button, |
| | |
| | | useListContext, |
| | | useUnselectAll, |
| | | } from "react-admin"; |
| | | import { useListReportActionParams } from "./listReport/useListReportOutput"; |
| | | import { |
| | | downloadBlobFile, |
| | | resolveReportMeta, |
| | | } from "./listReport/listReportUtils"; |
| | | |
| | | const MyExportButton = (props) => { |
| | | const { |
| | |
| | | icon = defaultIcon, |
| | | exporter: customExporter, |
| | | meta, |
| | | reportConfig, |
| | | onExport, |
| | | loading = false, |
| | | filename, |
| | | ...rest |
| | | } = props; |
| | | |
| | | const { filter, selectedIds, filterValues, resource, sort, total } = useListContext(); |
| | | const { visibleColumns, params } = useListReportActionParams(reportConfig, { ids }); |
| | | const unSelect = useUnselectAll(resource); |
| | | console.log(selectedIds); |
| | | const dataProvider = useDataProvider(); |
| | | const notify = useNotify(); |
| | | const handleClick = |
| | | // useCallback( |
| | | (event) => { |
| | | dataProvider |
| | | .export(resource, { |
| | | const handleClick = async (event) => { |
| | | try { |
| | | const hasReportConfig = Boolean(reportConfig?.resource && Array.isArray(reportConfig?.columns)); |
| | | if (hasReportConfig) { |
| | | const actionParams = { |
| | | ...params, |
| | | columns: visibleColumns, |
| | | }; |
| | | if (typeof onExport === "function") { |
| | | await onExport(actionParams, event); |
| | | } else { |
| | | const resolvedResource = reportConfig.resource || params.resource; |
| | | const response = await dataProvider.export(resolvedResource, { |
| | | sort: actionParams.sort, |
| | | ids: actionParams.ids, |
| | | filter: actionParams.filter |
| | | ? { ...actionParams.filterValues, ...actionParams.filter } |
| | | : actionParams.filterValues, |
| | | pagination: { page: 1, perPage: maxResults }, |
| | | meta, |
| | | columns: visibleColumns, |
| | | reportMeta: resolveReportMeta(reportConfig, actionParams), |
| | | }); |
| | | downloadBlobFile( |
| | | response, |
| | | filename || reportConfig.exportFileName || `${resolvedResource}.xlsx`, |
| | | ); |
| | | } |
| | | } else { |
| | | const response = await dataProvider.export(resource, { |
| | | sort, |
| | | ids: selectedIds, |
| | | ids: ids ?? selectedIds, |
| | | filter: filter ? { ...filterValues, ...filter } : filterValues, |
| | | pagination: { page: 1, perPage: maxResults }, |
| | | meta, |
| | | }) |
| | | .then((res) => { |
| | | 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", `${resource}.xlsx`); |
| | | document.body.appendChild(link); |
| | | link.click(); |
| | | link.remove(); |
| | | unSelect(); |
| | | }) |
| | | .catch((error) => { |
| | | console.error(error); |
| | | notify("ra.notification.http_error", { type: "error" }); |
| | | }); |
| | | downloadBlobFile(response, filename || `${resource}.xlsx`); |
| | | } |
| | | unSelect(); |
| | | if (typeof onClick === "function") { |
| | | onClick(event); |
| | | } |
| | | } catch (error) { |
| | | console.error(error); |
| | | notify("ra.notification.http_error", { type: "error" }); |
| | | } |
| | | // [ |
| | | // dataProvider, |
| | | // filter, |
| | | // filterValues, |
| | | // maxResults, |
| | | // notify, |
| | | // onClick, |
| | | // resource, |
| | | // sort, |
| | | // meta, |
| | | // ], |
| | | // ); |
| | | }; |
| | | |
| | | const disabled = total === 0 || loading || (reportConfig?.columns && visibleColumns.length === 0); |
| | | |
| | | return ( |
| | | <Button |
| | | onClick={handleClick} |
| | | label={label} |
| | | disabled={total === 0} |
| | | disabled={disabled} |
| | | {...sanitizeRestProps(rest)} |
| | | > |
| | | {icon} |