@bento-core/table 1.0.0 → 1.0.1-CDS.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,21 +1,75 @@
1
- import React from 'react';
1
+ import React, { useCallback } from 'react';
2
+ import { useApolloClient } from '@apollo/client';
2
3
  import {
3
4
  IconButton,
4
5
  Tooltip,
5
6
  } from '@material-ui/core';
6
7
  import { CloudDownload } from '@material-ui/icons';
8
+ import { downloadJson } from '../util/downloadTable';
7
9
 
8
10
  const DownloadButton = ({
9
11
  download,
12
+ rows,
13
+ table,
14
+ queryVariables,
15
+ server,
10
16
  }) => {
11
17
  if (!download) {
12
18
  return null;
13
19
  }
20
+ const {
21
+ downloadCsv = 'download',
22
+ downloadTable,
23
+ downloadFileName,
24
+ } = download;
25
+
26
+ const client = useApolloClient();
27
+
28
+ async function downloadSCSVFile() {
29
+ const {
30
+ query,
31
+ paginationAPIField,
32
+ } = table;
33
+
34
+ const result = await client.query({
35
+ query,
36
+ variables: {
37
+ ...queryVariables,
38
+ page: 0,
39
+ first: 10000,
40
+ },
41
+ })
42
+ .then((response) => {
43
+ if (paginationAPIField && response && response.data) {
44
+ return response.data[paginationAPIField];
45
+ }
46
+ return response.data;
47
+ });
48
+ downloadJson(result, table, downloadFileName);
49
+ }
50
+
51
+ const downloadTableCSV = useCallback(() => {
52
+ // custom download from bento app
53
+ if (downloadTable) {
54
+ downloadTable(queryVariables);
55
+ } else if (!downloadTable && !server) {
56
+ // download client table
57
+ downloadJson(rows, table, downloadFileName);
58
+ } else {
59
+ // donwload server table
60
+ downloadSCSVFile();
61
+ }
62
+ }, [queryVariables]);
14
63
 
15
64
  return (
16
65
  <>
17
- <Tooltip title={download.downloadCsv}>
18
- <IconButton>
66
+ <Tooltip
67
+ title={downloadCsv}
68
+ className="download-icon"
69
+ >
70
+ <IconButton
71
+ onClick={downloadTableCSV}
72
+ >
19
73
  <CloudDownload />
20
74
  </IconButton>
21
75
  </Tooltip>
@@ -26,7 +26,7 @@ const ManageColumnView = ({
26
26
  return null;
27
27
  }
28
28
  const [anchorEl, setAnchorEl] = useState(null);
29
- const handleClick = (event) => {
29
+ const toggleDisplay = (event) => {
30
30
  setAnchorEl(event.currentTarget);
31
31
  };
32
32
 
@@ -39,7 +39,11 @@ const ManageColumnView = ({
39
39
  return (
40
40
  <>
41
41
  <Tooltip title={manageViewColumns.title}>
42
- <IconButton variant="contained" onClick={handleClick}>
42
+ <IconButton
43
+ variant="contained"
44
+ onClick={toggleDisplay}
45
+ className="manageViewColumnBtn"
46
+ >
43
47
  <ViewColumn />
44
48
  </IconButton>
45
49
  </Tooltip>
@@ -70,7 +74,6 @@ const ManageColumnView = ({
70
74
  <Checkbox
71
75
  icon={(
72
76
  <CheckBoxBlankIcon
73
- style={{ fontSize: 18 }}
74
77
  className="checkBoxIcon"
75
78
  />
76
79
  )}
@@ -78,9 +81,6 @@ const ManageColumnView = ({
78
81
  checked={column.display}
79
82
  checkedIcon={(
80
83
  <CheckBoxIcon
81
- style={{
82
- fontSize: 18,
83
- }}
84
84
  className="checkBoxIcon"
85
85
  />
86
86
  )}
@@ -88,6 +88,9 @@ const ManageColumnView = ({
88
88
  color="secondary"
89
89
  className="checkBox"
90
90
  />
91
+ {column.icon && (
92
+ <img src={column.icon} alt={column.header} className={column.dataField} />
93
+ )}
91
94
  <Typography>{column.header}</Typography>
92
95
  </ListItem>
93
96
  ))}
@@ -6,3 +6,28 @@ export function formatBytes(bytes, decimals = 2) {
6
6
  const i = Math.floor(Math.log(bytes) / Math.log(1024));
7
7
  return `${parseFloat((bytes / (1024 ** i)).toFixed(dm))} ${sizes[i]}`;
8
8
  }
9
+
10
+ /**
11
+ * convert zero (0) value to custom val
12
+ * @param {*} columns
13
+ * @param {*} data
14
+ * @returns
15
+ */
16
+ export const formatColumnValues = (columns, data) => {
17
+ const formatColumns = columns.filter(({ columnDefaultValues }) => columnDefaultValues);
18
+ if (formatColumns.length > 0) {
19
+ const formatCoumnData = [...data].reduce((acc, item) => {
20
+ const updateRow = { ...item };
21
+ formatColumns.forEach((col) => {
22
+ const { columnDefaultValues, dataField } = col;
23
+ if (columnDefaultValues[item[dataField]]) {
24
+ updateRow[dataField] = columnDefaultValues[item[dataField]];
25
+ }
26
+ });
27
+ acc.push(updateRow);
28
+ return acc;
29
+ }, []);
30
+ return formatCoumnData;
31
+ }
32
+ return data;
33
+ };
@@ -0,0 +1,74 @@
1
+ import { formatBytes, formatColumnValues } from './Dataformat';
2
+ import { actionCellTypes } from './Types';
3
+
4
+ export function createFileName(fileName) {
5
+ const date = new Date();
6
+ const yyyy = date.getFullYear();
7
+ let dd = date.getDate();
8
+ let mm = (date.getMonth() + 1);
9
+
10
+ if (dd < 10) { dd = `0${dd}`; }
11
+
12
+ if (mm < 10) { mm = `0${mm}`; }
13
+
14
+ const todaysDate = `${yyyy}-${mm}-${dd}`;
15
+
16
+ let hours = date.getHours();
17
+ let minutes = date.getMinutes();
18
+ let seconds = date.getSeconds();
19
+
20
+ if (hours < 10) { hours = `0${hours}`; }
21
+
22
+ if (minutes < 10) { minutes = `0${minutes}`; }
23
+
24
+ if (seconds < 10) { seconds = `0${seconds}`; }
25
+
26
+ return `${fileName} ${todaysDate} ${hours}-${minutes}-${seconds}${'.csv'}`;
27
+ }
28
+
29
+ export function convertToCSV(jsonse, keysToInclude, header) {
30
+ const objArray = jsonse;
31
+ // To Do empty object just print headers
32
+ const array = typeof objArray !== 'object' ? JSON.parse(objArray) : objArray;
33
+ let str = header.join(',');
34
+ array.map((entry, index) => {
35
+ let line = '';
36
+ keysToInclude.map((keyName) => {
37
+ if (line !== '') line += ',';
38
+ if (keyName === 'file_size') {
39
+ line += entry[keyName] !== null ? `"${formatBytes(entry[keyName])}"` : ' ';
40
+ } else {
41
+ line += entry[keyName] !== null ? `"${entry[keyName]}"` : ' ';
42
+ }
43
+ return line;
44
+ });
45
+ if (index === 0) {
46
+ // str = header.join(',');
47
+ str += `\r\n${line}\r\n`;
48
+ } else {
49
+ str += `${line}\r\n`;
50
+ }
51
+ return str;
52
+ });
53
+ return str;
54
+ }
55
+
56
+ export function downloadJson(tableData, table, downloadFileName) {
57
+ const { columns = [] } = table;
58
+ const filterColumns = columns.filter(({ cellType }) => !actionCellTypes.includes(cellType));
59
+ const formatDataVal = formatColumnValues(filterColumns, tableData);
60
+ const jsonse = JSON.stringify(formatDataVal);
61
+ const keysToInclude = columns.filter(({ dataField }) => dataField)
62
+ .map(({ dataField }) => dataField);
63
+ const headers = columns.filter(({ dataField }) => dataField).map(({ header }) => header);
64
+ const csv = convertToCSV(jsonse, keysToInclude, headers);
65
+ const exportData = new Blob([csv], { type: 'text/csv' });
66
+ const JsonURL = window.URL.createObjectURL(exportData);
67
+ let tempLink = '';
68
+ tempLink = document.createElement('a');
69
+ tempLink.setAttribute('href', JsonURL);
70
+ tempLink.setAttribute('download', createFileName(downloadFileName || ''));
71
+ document.body.appendChild(tempLink);
72
+ tempLink.click();
73
+ document.body.removeChild(tempLink);
74
+ }