zhou zhou
11 小时以前 6e5ff559023efd2d24fdca2adcb7268d06420e46
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import * as React from "react";
import DownloadIcon from "@mui/icons-material/GetApp";
import {
  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,
    ids,
    label = "ra.action.export",
    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);
  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" });
    }
  };
 
  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;
 
export default MyExportButton;