| | |
| | | import * as React from 'react'; |
| | | import { useCallback } from 'react'; |
| | | import DownloadIcon from '@mui/icons-material/GetApp'; |
| | | import * as React from "react"; |
| | | import DownloadIcon from "@mui/icons-material/GetApp"; |
| | | import { |
| | | Button, useDataProvider, useNotify, useListContext |
| | | } from 'react-admin'; |
| | | Button, |
| | | useDataProvider, |
| | | useNotify, |
| | | useListContext, |
| | | useUnselectAll, |
| | | } from "react-admin"; |
| | | import { useListReportActionParams } from "./listReport/useListReportOutput"; |
| | | import { |
| | | downloadBlobFile, |
| | | resolveReportMeta, |
| | | } from "./listReport/listReportUtils"; |
| | | |
| | | const MyExportButton = (props) => { |
| | | const { |
| | | maxResults = 1000, |
| | | onClick, |
| | | label = 'ra.action.export', |
| | | icon = defaultIcon, |
| | | exporter: customExporter, |
| | | meta, |
| | | ...rest |
| | | } = props; |
| | | const { |
| | | maxResults = 1000, |
| | | onClick, |
| | | ids, |
| | | label = "ra.action.export", |
| | | icon = defaultIcon, |
| | | exporter: customExporter, |
| | | meta, |
| | | reportConfig, |
| | | onExport, |
| | | loading = false, |
| | | filename, |
| | | ...rest |
| | | } = props; |
| | | |
| | | const { |
| | | filter, |
| | | filterValues, |
| | | resource, |
| | | sort, |
| | | total, |
| | | } = useListContext(); |
| | | |
| | | const dataProvider = useDataProvider(); |
| | | const notify = useNotify(); |
| | | |
| | | const handleClick = useCallback( |
| | | event => { |
| | | dataProvider.export(resource, { |
| | | sort, |
| | | 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(); |
| | | }).catch(error => { |
| | | console.error(error); |
| | | notify('ra.notification.http_error', { type: 'error' }); |
| | | }); |
| | | if (typeof onClick === 'function') { |
| | | onClick(event); |
| | | } |
| | | }, |
| | | [ |
| | | dataProvider, |
| | | filter, |
| | | filterValues, |
| | | maxResults, |
| | | notify, |
| | | onClick, |
| | | resource, |
| | | sort, |
| | | const { filter, selectedIds, filterValues, resource, sort, total } = useListContext(); |
| | | const { visibleColumns, params } = useListReportActionParams(reportConfig, { ids }); |
| | | const unSelect = useUnselectAll(resource); |
| | | const dataProvider = useDataProvider(); |
| | | const notify = useNotify(); |
| | | 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: ids ?? selectedIds, |
| | | filter: filter ? { ...filterValues, ...filter } : filterValues, |
| | | pagination: { page: 1, perPage: maxResults }, |
| | | meta, |
| | | }); |
| | | downloadBlobFile(response, filename || `${resource}.xlsx`); |
| | | } |
| | | unSelect(); |
| | | if (typeof onClick === "function") { |
| | | onClick(event); |
| | | } |
| | | } catch (error) { |
| | | console.error(error); |
| | | notify("ra.notification.http_error", { type: "error" }); |
| | | } |
| | | }; |
| | | |
| | | return ( |
| | | <Button |
| | | onClick={handleClick} |
| | | label={label} |
| | | disabled={total === 0} |
| | | {...sanitizeRestProps(rest)} |
| | | > |
| | | {icon} |
| | | </Button> |
| | | ); |
| | | const disabled = total === 0 || loading || (reportConfig?.columns && visibleColumns.length === 0); |
| | | |
| | | return ( |
| | | <Button |
| | | onClick={handleClick} |
| | | label={label} |
| | | disabled={disabled} |
| | | {...sanitizeRestProps(rest)} |
| | | > |
| | | {icon} |
| | | </Button> |
| | | ); |
| | | }; |
| | | |
| | | const defaultIcon = <DownloadIcon />; |
| | | |
| | | const sanitizeRestProps = ({ |
| | | resource, |
| | | ...rest |
| | | }) => rest; |
| | | const sanitizeRestProps = ({ resource, ...rest }) => rest; |
| | | |
| | | export default MyExportButton; |