2020-07-20 06:04:05 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import { IconWrapper } from "constants/IconConstants";
|
|
|
|
|
import { Colors } from "constants/Colors";
|
|
|
|
|
import { ReactComponent as DownloadIcon } from "assets/icons/control/download-table.svg";
|
2020-08-10 11:16:13 +00:00
|
|
|
import { ReactTableColumnProps } from "widgets/TableWidget";
|
2020-07-20 06:04:05 +00:00
|
|
|
import { TableIconWrapper } from "components/designSystems/appsmith/TableStyledWrappers";
|
2020-09-11 11:25:48 +00:00
|
|
|
import TableActionIcon from "components/designSystems/appsmith/TableActionIcon";
|
2020-08-11 11:53:39 +00:00
|
|
|
import { isString } from "lodash";
|
2020-07-20 06:04:05 +00:00
|
|
|
|
|
|
|
|
interface TableDataDownloadProps {
|
2020-11-03 13:05:40 +00:00
|
|
|
data: Array<Record<string, unknown>>;
|
2020-07-20 06:04:05 +00:00
|
|
|
columns: ReactTableColumnProps[];
|
|
|
|
|
widgetName: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const TableDataDownload = (props: TableDataDownloadProps) => {
|
|
|
|
|
const [selected, toggleButtonClick] = React.useState(false);
|
|
|
|
|
const downloadTableData = () => {
|
|
|
|
|
toggleButtonClick(true);
|
|
|
|
|
const csvData = [];
|
|
|
|
|
csvData.push(
|
|
|
|
|
props.columns
|
|
|
|
|
.map((column: ReactTableColumnProps) => {
|
|
|
|
|
if (column.metaProperties && !column.metaProperties.isHidden) {
|
|
|
|
|
return column.Header;
|
|
|
|
|
}
|
2020-08-10 11:16:13 +00:00
|
|
|
return null;
|
2020-07-20 06:04:05 +00:00
|
|
|
})
|
|
|
|
|
.filter(i => !!i),
|
|
|
|
|
);
|
|
|
|
|
for (let row = 0; row < props.data.length; row++) {
|
|
|
|
|
const data: { [key: string]: any } = props.data[row];
|
|
|
|
|
const csvDataRow = [];
|
|
|
|
|
for (let colIndex = 0; colIndex < props.columns.length; colIndex++) {
|
|
|
|
|
const column = props.columns[colIndex];
|
|
|
|
|
const value = data[column.accessor];
|
|
|
|
|
if (column.metaProperties && !column.metaProperties.isHidden) {
|
2020-08-11 11:53:39 +00:00
|
|
|
if (isString(value) && value.includes(",")) {
|
|
|
|
|
csvDataRow.push(`"${value}"`);
|
|
|
|
|
} else {
|
|
|
|
|
csvDataRow.push(value);
|
|
|
|
|
}
|
2020-07-20 06:04:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
csvData.push(csvDataRow);
|
|
|
|
|
}
|
|
|
|
|
let csvContent = "";
|
|
|
|
|
csvData.forEach(function(infoArray, index) {
|
|
|
|
|
const dataString = infoArray.join(",");
|
|
|
|
|
csvContent += index < csvData.length ? dataString + "\n" : dataString;
|
|
|
|
|
});
|
|
|
|
|
const fileName = `${props.widgetName}.csv`;
|
|
|
|
|
const anchor = document.createElement("a");
|
|
|
|
|
const mimeType = "application/octet-stream";
|
|
|
|
|
if (navigator.msSaveBlob) {
|
|
|
|
|
navigator.msSaveBlob(
|
|
|
|
|
new Blob([csvContent], {
|
|
|
|
|
type: mimeType,
|
|
|
|
|
}),
|
|
|
|
|
fileName,
|
|
|
|
|
);
|
|
|
|
|
} else if (URL && "download" in anchor) {
|
|
|
|
|
anchor.href = URL.createObjectURL(
|
|
|
|
|
new Blob([csvContent], {
|
|
|
|
|
type: mimeType,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
anchor.setAttribute("download", fileName);
|
|
|
|
|
document.body.appendChild(anchor);
|
|
|
|
|
anchor.click();
|
|
|
|
|
document.body.removeChild(anchor);
|
|
|
|
|
}
|
|
|
|
|
toggleButtonClick(false);
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-10 11:16:13 +00:00
|
|
|
if (props.columns.length === 0) {
|
|
|
|
|
return (
|
|
|
|
|
<TableIconWrapper disabled>
|
|
|
|
|
<IconWrapper width={20} height={20} color={Colors.CADET_BLUE}>
|
|
|
|
|
<DownloadIcon />
|
|
|
|
|
</IconWrapper>
|
|
|
|
|
</TableIconWrapper>
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-07-20 06:04:05 +00:00
|
|
|
return (
|
2020-09-11 11:25:48 +00:00
|
|
|
<TableActionIcon
|
|
|
|
|
tooltip="Download"
|
|
|
|
|
selected={selected}
|
|
|
|
|
selectMenu={() => {
|
2020-07-20 06:04:05 +00:00
|
|
|
downloadTableData();
|
|
|
|
|
}}
|
2020-08-27 11:20:39 +00:00
|
|
|
className="t--table-download-btn"
|
2020-07-20 06:04:05 +00:00
|
|
|
>
|
2020-09-11 11:25:48 +00:00
|
|
|
<DownloadIcon />
|
|
|
|
|
</TableActionIcon>
|
2020-07-20 06:04:05 +00:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default TableDataDownload;
|