2020-07-20 06:04:05 +00:00
|
|
|
import React from "react";
|
2021-04-28 13:07:35 +00:00
|
|
|
import {
|
|
|
|
|
Popover,
|
|
|
|
|
Classes,
|
|
|
|
|
PopoverInteractionKind,
|
|
|
|
|
Position,
|
|
|
|
|
} from "@blueprintjs/core";
|
2020-07-20 06:04:05 +00:00
|
|
|
import { IconWrapper } from "constants/IconConstants";
|
|
|
|
|
import { Colors } from "constants/Colors";
|
2021-05-10 09:38:23 +00:00
|
|
|
import { ReactComponent as DownloadIcon } from "assets/icons/control/download-data-icon.svg";
|
2021-09-09 15:10:22 +00:00
|
|
|
import { ReactTableColumnProps } from "./Constants";
|
|
|
|
|
import { TableIconWrapper } from "./TableStyledWrappers";
|
|
|
|
|
import TableAction from "./TableAction";
|
2021-04-28 13:07:35 +00:00
|
|
|
import styled from "styled-components";
|
2021-04-20 10:47:06 +00:00
|
|
|
import { transformTableDataIntoCsv } from "./CommonUtilities";
|
2021-04-29 09:20:43 +00:00
|
|
|
import zipcelx from "zipcelx";
|
2021-04-28 13:07:35 +00:00
|
|
|
|
|
|
|
|
const DropDownWrapper = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
background: white;
|
|
|
|
|
z-index: 1;
|
|
|
|
|
border-radius: 4px;
|
2021-05-10 09:38:23 +00:00
|
|
|
box-shadow: 0px 12px 28px -8px rgba(0, 0, 0, 0.1);
|
|
|
|
|
padding: 0;
|
2021-04-28 13:07:35 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const OptionWrapper = styled.div`
|
|
|
|
|
display: flex;
|
2021-05-10 09:38:23 +00:00
|
|
|
width: 100%;
|
2021-04-28 13:07:35 +00:00
|
|
|
justify-content: space-between;
|
|
|
|
|
align-items: center;
|
|
|
|
|
height: 32px;
|
|
|
|
|
box-sizing: border-box;
|
2021-05-10 09:38:23 +00:00
|
|
|
padding: 6px 12px;
|
|
|
|
|
color: ${Colors.CHARCOAL};
|
2021-04-28 13:07:35 +00:00
|
|
|
min-width: 200px;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
background: ${Colors.WHITE};
|
|
|
|
|
border-left: none;
|
2021-05-10 09:38:23 +00:00
|
|
|
border-radius: none;
|
2021-04-28 13:07:35 +00:00
|
|
|
.option-title {
|
|
|
|
|
font-weight: 500;
|
2021-05-10 09:38:23 +00:00
|
|
|
font-size: 13px;
|
|
|
|
|
line-height: 20px;
|
2021-04-28 13:07:35 +00:00
|
|
|
}
|
|
|
|
|
&:hover {
|
2021-05-10 09:38:23 +00:00
|
|
|
background: ${Colors.SEA_SHELL};
|
|
|
|
|
color: ${Colors.CODE_GRAY};
|
2021-04-28 13:07:35 +00:00
|
|
|
}
|
|
|
|
|
`;
|
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;
|
2021-08-18 10:33:26 +00:00
|
|
|
delimiter: string;
|
2020-07-20 06:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-28 13:07:35 +00:00
|
|
|
type FileDownloadType = "CSV" | "EXCEL";
|
|
|
|
|
|
2021-04-29 09:20:43 +00:00
|
|
|
type DataCellProps = {
|
|
|
|
|
value: string | number;
|
|
|
|
|
type: "string" | "number";
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-28 13:07:35 +00:00
|
|
|
interface DownloadOptionProps {
|
|
|
|
|
label: string;
|
|
|
|
|
value: FileDownloadType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dowloadOptions: DownloadOptionProps[] = [
|
|
|
|
|
{
|
2021-04-29 09:20:43 +00:00
|
|
|
label: "Download as CSV",
|
2021-04-28 13:07:35 +00:00
|
|
|
value: "CSV",
|
|
|
|
|
},
|
|
|
|
|
{
|
2021-04-29 09:20:43 +00:00
|
|
|
label: "Download as Excel",
|
2021-04-28 13:07:35 +00:00
|
|
|
value: "EXCEL",
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2021-04-20 10:47:06 +00:00
|
|
|
const downloadDataAsCSV = (props: {
|
|
|
|
|
csvData: Array<Array<any>>;
|
2021-08-18 10:33:26 +00:00
|
|
|
delimiter: string;
|
2021-04-20 10:47:06 +00:00
|
|
|
fileName: string;
|
|
|
|
|
}) => {
|
|
|
|
|
let csvContent = "";
|
|
|
|
|
props.csvData.forEach((infoArray: Array<any>, index: number) => {
|
2021-08-18 10:33:26 +00:00
|
|
|
const dataString = infoArray.join(props.delimiter);
|
2021-04-20 10:47:06 +00:00
|
|
|
csvContent += index < props.csvData.length ? dataString + "\n" : dataString;
|
|
|
|
|
});
|
|
|
|
|
const anchor = document.createElement("a");
|
|
|
|
|
const mimeType = "application/octet-stream";
|
|
|
|
|
if (navigator.msSaveBlob) {
|
|
|
|
|
navigator.msSaveBlob(
|
|
|
|
|
new Blob([csvContent], {
|
|
|
|
|
type: mimeType,
|
|
|
|
|
}),
|
|
|
|
|
props.fileName,
|
|
|
|
|
);
|
|
|
|
|
} else if (URL && "download" in anchor) {
|
|
|
|
|
anchor.href = URL.createObjectURL(
|
|
|
|
|
new Blob([csvContent], {
|
|
|
|
|
type: mimeType,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
anchor.setAttribute("download", props.fileName);
|
|
|
|
|
document.body.appendChild(anchor);
|
|
|
|
|
anchor.click();
|
|
|
|
|
document.body.removeChild(anchor);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
function TableDataDownload(props: TableDataDownloadProps) {
|
2021-04-28 13:07:35 +00:00
|
|
|
const [selected, selectMenu] = React.useState(false);
|
|
|
|
|
const downloadFile = (type: string) => {
|
|
|
|
|
if (type === "CSV") {
|
|
|
|
|
downloadTableDataAsCsv();
|
|
|
|
|
} else if (type === "EXCEL") {
|
|
|
|
|
downloadTableDataAsExcel();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const downloadTableDataAsExcel = () => {
|
2021-04-29 09:20:43 +00:00
|
|
|
const tableData: Array<Array<DataCellProps>> = [];
|
2021-04-29 10:03:40 +00:00
|
|
|
const tableHeaders: Array<DataCellProps> = props.columns
|
|
|
|
|
.filter((column: ReactTableColumnProps) => {
|
|
|
|
|
return column.metaProperties && !column.metaProperties.isHidden;
|
2021-04-28 13:07:35 +00:00
|
|
|
})
|
2021-04-29 10:03:40 +00:00
|
|
|
.map((column: ReactTableColumnProps) => {
|
|
|
|
|
return {
|
|
|
|
|
value: column.Header,
|
|
|
|
|
type:
|
|
|
|
|
column.columnProperties?.columnType === "number"
|
|
|
|
|
? "number"
|
|
|
|
|
: "string",
|
|
|
|
|
};
|
|
|
|
|
});
|
2021-04-28 13:07:35 +00:00
|
|
|
tableData.push(tableHeaders);
|
|
|
|
|
for (let row = 0; row < props.data.length; row++) {
|
|
|
|
|
const data: { [key: string]: any } = props.data[row];
|
2021-04-29 09:20:43 +00:00
|
|
|
const tableRow: Array<DataCellProps> = [];
|
2021-04-28 13:07:35 +00:00
|
|
|
for (let colIndex = 0; colIndex < props.columns.length; colIndex++) {
|
|
|
|
|
const column = props.columns[colIndex];
|
2021-04-29 09:20:43 +00:00
|
|
|
const type =
|
|
|
|
|
column.columnProperties?.columnType === "number"
|
|
|
|
|
? "number"
|
|
|
|
|
: "string";
|
2021-04-28 13:07:35 +00:00
|
|
|
if (column.metaProperties && !column.metaProperties.isHidden) {
|
2021-04-29 09:20:43 +00:00
|
|
|
tableRow.push({
|
|
|
|
|
value: data[column.accessor],
|
|
|
|
|
type: type,
|
|
|
|
|
});
|
2021-04-28 13:07:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tableData.push(tableRow);
|
|
|
|
|
}
|
2021-04-29 09:20:43 +00:00
|
|
|
zipcelx({
|
|
|
|
|
filename: props.widgetName,
|
|
|
|
|
sheet: {
|
|
|
|
|
data: tableData,
|
|
|
|
|
},
|
|
|
|
|
});
|
2021-04-28 13:07:35 +00:00
|
|
|
};
|
|
|
|
|
const downloadTableDataAsCsv = () => {
|
|
|
|
|
selectMenu(true);
|
2021-04-20 10:47:06 +00:00
|
|
|
const csvData = transformTableDataIntoCsv({
|
|
|
|
|
columns: props.columns,
|
|
|
|
|
data: props.data,
|
|
|
|
|
});
|
|
|
|
|
downloadDataAsCSV({
|
|
|
|
|
csvData: csvData,
|
2021-08-18 10:33:26 +00:00
|
|
|
delimiter: props.delimiter,
|
2021-04-20 10:47:06 +00:00
|
|
|
fileName: `${props.widgetName}.csv`,
|
2020-07-20 06:04:05 +00:00
|
|
|
});
|
2021-04-28 13:07:35 +00:00
|
|
|
selectMenu(false);
|
2020-07-20 06:04:05 +00:00
|
|
|
};
|
|
|
|
|
|
2021-05-11 06:40:13 +00:00
|
|
|
const handleCloseMenu = () => {
|
|
|
|
|
selectMenu(false);
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-10 11:16:13 +00:00
|
|
|
if (props.columns.length === 0) {
|
|
|
|
|
return (
|
|
|
|
|
<TableIconWrapper disabled>
|
2021-04-28 10:28:39 +00:00
|
|
|
<IconWrapper color={Colors.CADET_BLUE} height={20} width={20}>
|
2020-08-10 11:16:13 +00:00
|
|
|
<DownloadIcon />
|
|
|
|
|
</IconWrapper>
|
2021-05-10 11:24:50 +00:00
|
|
|
<span className="action-title">Download</span>
|
2020-08-10 11:16:13 +00:00
|
|
|
</TableIconWrapper>
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-07-20 06:04:05 +00:00
|
|
|
return (
|
2021-04-28 13:07:35 +00:00
|
|
|
<Popover
|
|
|
|
|
enforceFocus={false}
|
|
|
|
|
interactionKind={PopoverInteractionKind.CLICK}
|
2021-04-28 13:24:26 +00:00
|
|
|
isOpen={selected}
|
|
|
|
|
minimal
|
2021-05-11 06:40:13 +00:00
|
|
|
onClose={handleCloseMenu}
|
2021-04-28 13:24:26 +00:00
|
|
|
position={Position.BOTTOM}
|
2020-07-20 06:04:05 +00:00
|
|
|
>
|
2021-05-10 11:24:50 +00:00
|
|
|
<TableAction
|
2021-04-28 13:24:26 +00:00
|
|
|
className="t--table-download-btn"
|
2021-05-11 06:40:13 +00:00
|
|
|
selectMenu={selectMenu}
|
2021-04-28 13:24:26 +00:00
|
|
|
selected={selected}
|
2022-04-06 05:37:13 +00:00
|
|
|
title="Download"
|
2021-04-28 13:07:35 +00:00
|
|
|
>
|
|
|
|
|
<DownloadIcon />
|
2021-05-10 11:24:50 +00:00
|
|
|
</TableAction>
|
2021-04-28 13:07:35 +00:00
|
|
|
<DropDownWrapper>
|
|
|
|
|
{dowloadOptions.map((item: DownloadOptionProps, index: number) => {
|
|
|
|
|
return (
|
|
|
|
|
<OptionWrapper
|
2021-04-28 13:24:26 +00:00
|
|
|
className={`${Classes.POPOVER_DISMISS} t--table-download-data-option`}
|
2021-04-28 13:07:35 +00:00
|
|
|
key={index}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
downloadFile(item.value);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{item.label}
|
|
|
|
|
</OptionWrapper>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</DropDownWrapper>
|
|
|
|
|
</Popover>
|
2020-07-20 06:04:05 +00:00
|
|
|
);
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2020-07-20 06:04:05 +00:00
|
|
|
|
|
|
|
|
export default TableDataDownload;
|