@bento-core/table 1.0.0-c3dc.5 → 1.0.0-c3dc.7

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.
@@ -50,12 +50,7 @@ const ExtendedView = _ref => {
50
50
  theme: themeConfig
51
51
  }, numSelected === 0 && (download || manageViewColumns) && /*#__PURE__*/_react.default.createElement(_core.Toolbar, {
52
52
  className: "downloadColumnView"
53
- }, /*#__PURE__*/_react.default.createElement(_DownloadButtonView.default, {
54
- count: table.totalRowCount || 0,
55
- queryVariables: queryVariables,
56
- table: table,
57
- buttonConfig: downloadButtonConfig
58
- }), /*#__PURE__*/_react.default.createElement(_ManageColumnView.default, {
53
+ }, /*#__PURE__*/_react.default.createElement(_ManageColumnView.default, {
59
54
  table: table,
60
55
  manageViewColumns: manageViewColumns,
61
56
  onColumnViewChange: onColumnViewChange
@@ -73,6 +68,11 @@ const ExtendedView = _ref => {
73
68
  onRowsPerPageChange: onRowsPerPageChange,
74
69
  queryVariables: queryVariables,
75
70
  table: table
71
+ }), table.showDownloadIcon !== false && /*#__PURE__*/_react.default.createElement(_DownloadButtonView.default, {
72
+ count: table.totalRowCount || 0,
73
+ queryVariables: queryVariables,
74
+ table: table,
75
+ buttonConfig: downloadButtonConfig
76
76
  })));
77
77
  };
78
78
  var _default = ExtendedView;
@@ -7,8 +7,10 @@ exports.default = void 0;
7
7
  var _react = _interopRequireWildcard(require("react"));
8
8
  var _client = require("@apollo/client");
9
9
  var _core = require("@material-ui/core");
10
+ var _KeyboardArrowDownOutlined = _interopRequireDefault(require("@material-ui/icons/KeyboardArrowDownOutlined"));
10
11
  var _icons = require("@material-ui/icons");
11
12
  var _downloadTable = require("../util/downloadTable");
13
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
14
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
15
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
14
16
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
@@ -16,42 +18,36 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
16
18
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
19
  function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
18
20
  function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
19
- const downloadButtonStyle = {
20
- color: '#d1d2d3',
21
- marginTop: '7px'
22
- };
21
+ // const downloadButtonStyle = {
22
+ // color: '#d1d2d3',
23
+ // marginTop: '7px',
24
+ // };
23
25
 
24
- // Default style for Download Button with Text.
25
- const DEFAULT_BUTTON_STYLE = {
26
- width: '223px',
27
- height: '41px',
28
- borderRadius: '5px',
29
- backgroundColor: '#2A6E93',
30
- color: '#fff',
31
- marginTop: '10px',
32
- fontFamily: 'poppins',
33
- '& #cloudIcon': {
34
- margin: '10px',
35
- marginTop: '6px'
36
- }
37
- };
38
26
  const DownloadButton = _ref => {
39
27
  let {
40
28
  count,
41
29
  queryVariables,
42
- table,
43
- buttonConfig = {},
44
- isIcon = false
30
+ table
45
31
  } = _ref;
32
+ const [listDisplay, setListDisplay] = (0, _react.useState)('none');
33
+ const dropdownSelection = (0, _react.useRef)(null);
34
+ const useOutsideAlerter = ref => {
35
+ (0, _react.useEffect)(() => {
36
+ function handleClickOutside(event) {
37
+ if (!(event.target.getAttribute('id') && event.target.getAttribute('id').includes('dropdownListItem'))) {
38
+ setListDisplay('none');
39
+ }
40
+ }
41
+ document.addEventListener('mousedown', handleClickOutside);
42
+ return () => {
43
+ document.removeEventListener('mousedown', handleClickOutside);
44
+ };
45
+ }, [ref]);
46
+ };
47
+ useOutsideAlerter(dropdownSelection);
46
48
  if (table.paginationAPIField === 'filesInList') {
47
49
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null);
48
50
  }
49
- if (count === 0) {
50
- return /*#__PURE__*/_react.default.createElement(_icons.CloudDownload, {
51
- className: "disableButton",
52
- style: downloadButtonStyle
53
- });
54
- }
55
51
  const client = (0, _client.useApolloClient)();
56
52
  async function downloadSCSVFile() {
57
53
  const {
@@ -62,7 +58,9 @@ const DownloadButton = _ref => {
62
58
  query,
63
59
  variables: _objectSpread(_objectSpread({}, queryVariables), {}, {
64
60
  page: 0,
65
- first: 10000
61
+ first: 10000,
62
+ order_by: table.sortBy,
63
+ sort_direction: table.sortOrder
66
64
  })
67
65
  }).then(response => {
68
66
  if (paginationAPIField && response && response.data) {
@@ -70,29 +68,114 @@ const DownloadButton = _ref => {
70
68
  }
71
69
  return response.data;
72
70
  });
73
- (0, _downloadTable.downloadJson)(result, table, table.downloadFileName);
71
+ (0, _downloadTable.downloadData)(result, table, table.downloadFileName, 'csv');
72
+ }
73
+ async function downloadJsonFile() {
74
+ const {
75
+ query,
76
+ paginationAPIField
77
+ } = table;
78
+ const result = await client.query({
79
+ query,
80
+ variables: _objectSpread(_objectSpread({}, queryVariables), {}, {
81
+ page: 0,
82
+ first: 10000,
83
+ order_by: table.sortBy,
84
+ sort_direction: table.sortOrder
85
+ })
86
+ }).then(response => {
87
+ if (paginationAPIField && response && response.data) {
88
+ return response.data[paginationAPIField];
89
+ }
90
+ return response.data;
91
+ });
92
+ (0, _downloadTable.downloadData)(result, table, table.downloadFileName, 'json');
74
93
  }
75
94
  const downloadTableCSV = (0, _react.useCallback)(() => {
76
95
  downloadSCSVFile();
77
- }, [queryVariables]);
78
- const {
79
- buttonStyle,
80
- title = 'DOWNLOAD CSV',
81
- cloudIcon = false
82
- } = buttonConfig;
83
- return isIcon ? /*#__PURE__*/_react.default.createElement(_core.Tooltip, {
84
- title: "Download filtered results as a CSV"
85
- }, /*#__PURE__*/_react.default.createElement(_core.IconButton, {
86
- onClick: downloadTableCSV
87
- }, /*#__PURE__*/_react.default.createElement(_icons.CloudDownload, null))) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_core.Button, {
88
- onClick: downloadTableCSV,
89
- style: buttonStyle || DEFAULT_BUTTON_STYLE,
90
- disableRipple: true
91
- }, title, ' ', cloudIcon ? /*#__PURE__*/_react.default.createElement(_icons.CloudDownload, {
96
+ setListDisplay('none');
97
+ }, [queryVariables, table]);
98
+ const downloadTableJson = (0, _react.useCallback)(() => {
99
+ downloadJsonFile();
100
+ setListDisplay('none');
101
+ }, [queryVariables, table]);
102
+ const handleClickButton = () => {
103
+ if (listDisplay === 'none') {
104
+ setListDisplay('block');
105
+ } else {
106
+ setListDisplay('none');
107
+ }
108
+ };
109
+ const useStyles = (0, _core.makeStyles)({
110
+ dropdown: {
111
+ width: '60px',
112
+ height: '25px',
113
+ marginTop: '8px',
114
+ paddingLeft: '5px',
115
+ border: '0.75px solid #606060',
116
+ borderRadius: '5px',
117
+ display: 'inline-block',
118
+ position: 'relative'
119
+ },
120
+ dropdownList: {
121
+ display: 'block',
122
+ position: 'absolute',
123
+ width: '60px',
124
+ marginTop: '3px',
125
+ marginLeft: '-5px',
126
+ overflow: 'auto',
127
+ zIndex: '5',
128
+ border: '1.5px solid #41545E',
129
+ borderRadius: '5px',
130
+ background: '#ffffff'
131
+ },
132
+ dropdownListItem: {
133
+ padding: '2px 4px',
134
+ font: 'Poppins',
135
+ fontSize: '14px',
136
+ fontWeight: 400,
137
+ lineHeight: '21px',
138
+ '&:hover': {
139
+ background: '#D7D7D7',
140
+ cursor: 'pointer'
141
+ }
142
+ },
143
+ arrowdownIcon: {
144
+ marginLeft: '5px',
145
+ fill: '#606060'
146
+ },
147
+ arrowdownIconDisabled: {
148
+ marginLeft: '5px',
149
+ fill: '#00000042'
150
+ }
151
+ });
152
+ const classes = useStyles();
153
+ return /*#__PURE__*/_react.default.createElement("div", {
154
+ className: classes.dropdown
155
+ }, /*#__PURE__*/_react.default.createElement(_core.Tooltip, {
156
+ title: "Download filtered results"
157
+ }, count !== 0 ? /*#__PURE__*/_react.default.createElement(_core.IconButton, {
158
+ onClick: handleClickButton
159
+ }, /*#__PURE__*/_react.default.createElement(_icons.CloudDownload, null), /*#__PURE__*/_react.default.createElement(_KeyboardArrowDownOutlined.default, {
160
+ className: classes.arrowdownIcon
161
+ })) : /*#__PURE__*/_react.default.createElement(_core.IconButton, {
162
+ disabled: true
163
+ }, /*#__PURE__*/_react.default.createElement(_icons.CloudDownload, null), /*#__PURE__*/_react.default.createElement(_KeyboardArrowDownOutlined.default, {
164
+ className: classes.arrowdownIconDisabled
165
+ }))), /*#__PURE__*/_react.default.createElement("div", {
166
+ className: classes.dropdownList,
92
167
  style: {
93
- margin: '6px 0px 10px 10px'
168
+ display: listDisplay
94
169
  }
95
- }) : ''));
170
+ }, /*#__PURE__*/_react.default.createElement("div", {
171
+ id: "dropdownListItemCSV",
172
+ className: classes.dropdownListItem,
173
+ onClick: downloadTableCSV
174
+ }, "CSV"), /*#__PURE__*/_react.default.createElement("div", {
175
+ id: "dropdownListItemJSON",
176
+ className: classes.dropdownListItem,
177
+ onClick: downloadTableJson
178
+ }, "JSON")));
96
179
  };
97
180
  var _default = DownloadButton;
98
181
  exports.default = _default;
@@ -5,10 +5,10 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.convertToCSV = convertToCSV;
7
7
  exports.createFileName = createFileName;
8
- exports.downloadJson = downloadJson;
8
+ exports.downloadData = downloadData;
9
9
  var _Dataformat = require("./Dataformat");
10
10
  var _Types = require("./Types");
11
- function createFileName(fileName) {
11
+ function createFileName(fileName, type) {
12
12
  const date = new Date();
13
13
  const yyyy = date.getFullYear();
14
14
  let dd = date.getDate();
@@ -32,7 +32,7 @@ function createFileName(fileName) {
32
32
  if (seconds < 10) {
33
33
  seconds = "0".concat(seconds);
34
34
  }
35
- return "".concat(fileName, " ").concat(todaysDate, " ").concat(hours, "-").concat(minutes, "-").concat(seconds, '.csv');
35
+ return "".concat(fileName, " ").concat(todaysDate, " ").concat(hours, "-").concat(minutes, "-").concat(seconds).concat(type);
36
36
  }
37
37
  function convertToCSV(jsonse, keysToInclude, header) {
38
38
  const objArray = jsonse;
@@ -66,7 +66,8 @@ function convertToCSV(jsonse, keysToInclude, header) {
66
66
  });
67
67
  return str;
68
68
  }
69
- function downloadJson(tableData, table, downloadFileName) {
69
+ function downloadData(tableData, table, downloadFileName) {
70
+ let format = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'csv';
70
71
  const {
71
72
  columns = []
72
73
  } = table;
@@ -76,40 +77,53 @@ function downloadJson(tableData, table, downloadFileName) {
76
77
  } = _ref;
77
78
  return !_Types.actionCellTypes.includes(cellType);
78
79
  });
79
- const formatDataVal = (0, _Dataformat.formatColumnValues)(filterColumns, tableData);
80
- const jsonse = JSON.stringify(formatDataVal);
81
- const keysToInclude = columns.filter(_ref2 => {
82
- let {
83
- dataField
84
- } = _ref2;
85
- return dataField;
86
- }).map(_ref3 => {
87
- let {
88
- dataField
89
- } = _ref3;
90
- return dataField;
91
- });
92
- const headers = columns.filter(_ref4 => {
93
- let {
94
- dataField
95
- } = _ref4;
96
- return dataField;
97
- }).map(_ref5 => {
98
- let {
99
- header,
100
- downloadHeader
101
- } = _ref5;
102
- return downloadHeader || header;
103
- });
104
- const csv = convertToCSV(jsonse, keysToInclude, headers);
105
- const exportData = new Blob([csv], {
106
- type: 'text/csv'
80
+ let formatDataVal = (0, _Dataformat.formatColumnValues)(filterColumns, tableData);
81
+ let fileContent;
82
+ let fileType;
83
+ let fileExtension;
84
+ if (format === 'json') {
85
+ filterColumns.forEach(column => {
86
+ formatDataVal = JSON.parse(JSON.stringify(formatDataVal).split("\"".concat(column.dataField, "\":")).join("\"".concat(column.header, "\":")));
87
+ });
88
+ fileContent = JSON.stringify(formatDataVal);
89
+ fileType = 'application/json';
90
+ fileExtension = 'json';
91
+ } else {
92
+ const jsonse = JSON.stringify(formatDataVal);
93
+ const keysToInclude = columns.filter(_ref2 => {
94
+ let {
95
+ dataField
96
+ } = _ref2;
97
+ return dataField;
98
+ }).map(_ref3 => {
99
+ let {
100
+ dataField
101
+ } = _ref3;
102
+ return dataField;
103
+ });
104
+ const headers = columns.filter(_ref4 => {
105
+ let {
106
+ dataField
107
+ } = _ref4;
108
+ return dataField;
109
+ }).map(_ref5 => {
110
+ let {
111
+ header,
112
+ downloadHeader
113
+ } = _ref5;
114
+ return downloadHeader || header;
115
+ });
116
+ fileContent = convertToCSV(jsonse, keysToInclude, headers);
117
+ fileType = 'text/csv';
118
+ fileExtension = 'csv';
119
+ }
120
+ const exportData = new Blob([fileContent], {
121
+ type: fileType
107
122
  });
108
- const JsonURL = window.URL.createObjectURL(exportData);
109
- let tempLink = '';
110
- tempLink = document.createElement('a');
111
- tempLink.setAttribute('href', JsonURL);
112
- tempLink.setAttribute('download', createFileName(downloadFileName || ''));
123
+ const fileURL = window.URL.createObjectURL(exportData);
124
+ const tempLink = document.createElement('a');
125
+ tempLink.setAttribute('href', fileURL);
126
+ tempLink.setAttribute('download', createFileName(downloadFileName || '', fileExtension));
113
127
  document.body.appendChild(tempLink);
114
128
  tempLink.click();
115
129
  document.body.removeChild(tempLink);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bento-core/table",
3
- "version": "1.0.0-c3dc.5",
3
+ "version": "1.0.0-c3dc.7",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "build": "npm run lint && cross-env-shell rm -rf dist && NODE_ENV=production BABEL_ENV=es babel src --out-dir dist --copy-files",
@@ -39,45 +39,43 @@ const ExtendedView = ({
39
39
  return (
40
40
  <ThemeProvider theme={themeConfig}>
41
41
  {(numSelected === 0 && (download || manageViewColumns)) && (
42
- <Toolbar className="downloadColumnView">
43
- <DownloadButton
44
- count={table.totalRowCount || 0}
45
- queryVariables={queryVariables}
46
- table={table}
47
- buttonConfig={downloadButtonConfig}
48
- />
49
- <ManageColumnView
50
- table={table}
51
- manageViewColumns={manageViewColumns}
52
- onColumnViewChange={onColumnViewChange}
53
- />
54
- </Toolbar>
42
+ <Toolbar className="downloadColumnView">
43
+
44
+ <ManageColumnView
45
+ table={table}
46
+ manageViewColumns={manageViewColumns}
47
+ onColumnViewChange={onColumnViewChange}
48
+ />
49
+ </Toolbar>
55
50
  )}
56
51
  {
57
- (pagination) && (
58
- <div className="downloadArea" style={(table.paginationCustomStyle && table.paginationCustomStyle.topPagination) ? table.paginationCustomStyle.topPagination : downloadAreaStyle}>
59
- <CustomPagination
60
- customTheme={customTheme.tblTopPgn}
61
- rowsPerPageOptions={[10, 25, 50, 100]}
62
- component="div"
52
+ (pagination) && (
53
+ <div className="downloadArea" style={(table.paginationCustomStyle && table.paginationCustomStyle.topPagination) ? table.paginationCustomStyle.topPagination : downloadAreaStyle}>
54
+ <CustomPagination
55
+ customTheme={customTheme.tblTopPgn}
56
+ rowsPerPageOptions={[10, 25, 50, 100]}
57
+ component="div"
58
+ count={table.totalRowCount || 0}
59
+ rowsPerPage={table.rowsPerPage || 10}
60
+ page={table.page || 0}
61
+ onPageChange={onPageChange}
62
+ onRowsPerPageChange={onRowsPerPageChange}
63
+ queryVariables={queryVariables}
64
+ table={table}
65
+ />
66
+ {table.showDownloadIcon !== false
67
+ && (
68
+ <DownloadButton
63
69
  count={table.totalRowCount || 0}
64
- rowsPerPage={table.rowsPerPage || 10}
65
- page={table.page || 0}
66
- onPageChange={onPageChange}
67
- onRowsPerPageChange={onRowsPerPageChange}
68
70
  queryVariables={queryVariables}
69
71
  table={table}
72
+ buttonConfig={downloadButtonConfig}
70
73
  />
71
- {/* TODO: Commented out this section, Make this options configrable on Bento. */}
72
- {/* <DownloadButton
73
- count={table.totalRowCount || 0}
74
- queryVariables={queryVariables}
75
- table={table}
76
- isIcon={false}
77
- /> */}
78
- </div>
79
- )
80
- }
74
+ )}
75
+
76
+ </div>
77
+ )
78
+ }
81
79
  </ThemeProvider>
82
80
  );
83
81
  };
@@ -1,47 +1,49 @@
1
- import React, { useCallback } from 'react';
1
+ import React, {
2
+ useState,
3
+ useEffect,
4
+ useRef,
5
+ useCallback,
6
+ } from 'react';
2
7
  import { useApolloClient } from '@apollo/client';
3
8
  import {
4
9
  IconButton,
5
10
  Tooltip,
6
- Button,
11
+ makeStyles,
7
12
  } from '@material-ui/core';
8
- // import ToolTip from '@bento-core/tool-tip';
13
+ import KeyboardArrowDownOutlinedIcon from '@material-ui/icons/KeyboardArrowDownOutlined';
9
14
  import { CloudDownload } from '@material-ui/icons';
10
- import { downloadJson } from '../util/downloadTable';
15
+ import { downloadData } from '../util/downloadTable';
11
16
 
12
- const downloadButtonStyle = {
13
- color: '#d1d2d3',
14
- marginTop: '7px',
15
- };
16
-
17
- // Default style for Download Button with Text.
18
- const DEFAULT_BUTTON_STYLE = {
19
- width: '223px',
20
- height: '41px',
21
- borderRadius: '5px',
22
- backgroundColor: '#2A6E93',
23
- color: '#fff',
24
- marginTop: '10px',
25
- fontFamily: 'poppins',
26
- '& #cloudIcon': {
27
- margin: '10px',
28
- marginTop: '6px',
29
- },
30
- };
17
+ // const downloadButtonStyle = {
18
+ // color: '#d1d2d3',
19
+ // marginTop: '7px',
20
+ // };
31
21
 
32
22
  const DownloadButton = ({
33
23
  count,
34
24
  queryVariables,
35
25
  table,
36
- buttonConfig = {},
37
- isIcon = false,
38
26
  }) => {
27
+ const [listDisplay, setListDisplay] = useState('none');
28
+ const dropdownSelection = useRef(null);
29
+ const useOutsideAlerter = (ref) => {
30
+ useEffect(() => {
31
+ function handleClickOutside(event) {
32
+ if (!(event.target.getAttribute('id') && event.target.getAttribute('id').includes('dropdownListItem'))) {
33
+ setListDisplay('none');
34
+ }
35
+ }
36
+ document.addEventListener('mousedown', handleClickOutside);
37
+ return () => {
38
+ document.removeEventListener('mousedown', handleClickOutside);
39
+ };
40
+ }, [ref]);
41
+ };
42
+ useOutsideAlerter(dropdownSelection);
43
+
39
44
  if (table.paginationAPIField === 'filesInList') {
40
45
  return <></>;
41
46
  }
42
- if (count === 0) {
43
- return <CloudDownload className="disableButton" style={downloadButtonStyle} />;
44
- }
45
47
 
46
48
  const client = useApolloClient();
47
49
 
@@ -57,6 +59,33 @@ const DownloadButton = ({
57
59
  ...queryVariables,
58
60
  page: 0,
59
61
  first: 10000,
62
+ order_by: table.sortBy,
63
+ sort_direction: table.sortOrder,
64
+ },
65
+ })
66
+ .then((response) => {
67
+ if (paginationAPIField && response && response.data) {
68
+ return response.data[paginationAPIField];
69
+ }
70
+ return response.data;
71
+ });
72
+ downloadData(result, table, table.downloadFileName, 'csv');
73
+ }
74
+
75
+ async function downloadJsonFile() {
76
+ const {
77
+ query,
78
+ paginationAPIField,
79
+ } = table;
80
+
81
+ const result = await client.query({
82
+ query,
83
+ variables: {
84
+ ...queryVariables,
85
+ page: 0,
86
+ first: 10000,
87
+ order_by: table.sortBy,
88
+ sort_direction: table.sortOrder,
60
89
  },
61
90
  })
62
91
  .then((response) => {
@@ -65,37 +94,97 @@ const DownloadButton = ({
65
94
  }
66
95
  return response.data;
67
96
  });
68
- downloadJson(result, table, table.downloadFileName);
97
+ downloadData(result, table, table.downloadFileName, 'json');
69
98
  }
70
99
 
71
100
  const downloadTableCSV = useCallback(() => {
72
101
  downloadSCSVFile();
73
- }, [queryVariables]);
102
+ setListDisplay('none');
103
+ }, [queryVariables, table]);
104
+
105
+ const downloadTableJson = useCallback(() => {
106
+ downloadJsonFile();
107
+ setListDisplay('none');
108
+ }, [queryVariables, table]);
74
109
 
75
- const {
76
- buttonStyle, title = 'DOWNLOAD CSV', cloudIcon = false,
77
- } = buttonConfig;
110
+ const handleClickButton = () => {
111
+ if (listDisplay === 'none') {
112
+ setListDisplay('block');
113
+ } else {
114
+ setListDisplay('none');
115
+ }
116
+ };
78
117
 
79
- return isIcon ? (
80
- <Tooltip title="Download filtered results as a CSV">
81
- <IconButton
82
- onClick={downloadTableCSV}
83
- >
84
- <CloudDownload />
85
- </IconButton>
86
- </Tooltip>
87
- ) : (
88
- <>
89
- <Button
90
- onClick={downloadTableCSV}
91
- style={buttonStyle || DEFAULT_BUTTON_STYLE}
92
- disableRipple
93
- >
94
- {title}
95
- {' '}
96
- {cloudIcon ? <CloudDownload style={{ margin: '6px 0px 10px 10px' }} /> : ''}
97
- </Button>
98
- </>
118
+ const useStyles = makeStyles({
119
+ dropdown: {
120
+ width: '60px',
121
+ height: '25px',
122
+ marginTop: '8px',
123
+ paddingLeft: '5px',
124
+ border: '0.75px solid #606060',
125
+ borderRadius: '5px',
126
+ display: 'inline-block',
127
+ position: 'relative',
128
+ },
129
+ dropdownList: {
130
+ display: 'block',
131
+ position: 'absolute',
132
+ width: '60px',
133
+ marginTop: '3px',
134
+ marginLeft: '-5px',
135
+ overflow: 'auto',
136
+ zIndex: '5',
137
+ border: '1.5px solid #41545E',
138
+ borderRadius: '5px',
139
+ background: '#ffffff',
140
+ },
141
+ dropdownListItem: {
142
+ padding: '2px 4px',
143
+ font: 'Poppins',
144
+ fontSize: '14px',
145
+ fontWeight: 400,
146
+ lineHeight: '21px',
147
+ '&:hover': {
148
+ background: '#D7D7D7',
149
+ cursor: 'pointer',
150
+ },
151
+ },
152
+ arrowdownIcon: {
153
+ marginLeft: '5px',
154
+ fill: '#606060',
155
+ },
156
+ arrowdownIconDisabled: {
157
+ marginLeft: '5px',
158
+ fill: '#00000042',
159
+ },
160
+ });
161
+
162
+ const classes = useStyles();
163
+
164
+ return (
165
+ <div className={classes.dropdown}>
166
+ <Tooltip title="Download filtered results">
167
+ {
168
+ count !== 0
169
+ ? (
170
+ <IconButton onClick={handleClickButton}>
171
+ <CloudDownload />
172
+ <KeyboardArrowDownOutlinedIcon className={classes.arrowdownIcon} />
173
+ </IconButton>
174
+ )
175
+ : (
176
+ <IconButton disabled>
177
+ <CloudDownload />
178
+ <KeyboardArrowDownOutlinedIcon className={classes.arrowdownIconDisabled} />
179
+ </IconButton>
180
+ )
181
+ }
182
+ </Tooltip>
183
+ <div className={classes.dropdownList} style={{ display: listDisplay }}>
184
+ <div id="dropdownListItemCSV" className={classes.dropdownListItem} onClick={downloadTableCSV}>CSV</div>
185
+ <div id="dropdownListItemJSON" className={classes.dropdownListItem} onClick={downloadTableJson}>JSON</div>
186
+ </div>
187
+ </div>
99
188
  );
100
189
  };
101
190
 
@@ -1,7 +1,7 @@
1
1
  import { formatBytes, formatColumnValues } from './Dataformat';
2
2
  import { actionCellTypes } from './Types';
3
3
 
4
- export function createFileName(fileName) {
4
+ export function createFileName(fileName, type) {
5
5
  const date = new Date();
6
6
  const yyyy = date.getFullYear();
7
7
  let dd = date.getDate();
@@ -22,8 +22,7 @@ export function createFileName(fileName) {
22
22
  if (minutes < 10) { minutes = `0${minutes}`; }
23
23
 
24
24
  if (seconds < 10) { seconds = `0${seconds}`; }
25
-
26
- return `${fileName} ${todaysDate} ${hours}-${minutes}-${seconds}${'.csv'}`;
25
+ return `${fileName} ${todaysDate} ${hours}-${minutes}-${seconds}${type}`;
27
26
  }
28
27
 
29
28
  export function convertToCSV(jsonse, keysToInclude, header) {
@@ -59,22 +58,40 @@ export function convertToCSV(jsonse, keysToInclude, header) {
59
58
  return str;
60
59
  }
61
60
 
62
- export function downloadJson(tableData, table, downloadFileName) {
61
+ export function downloadData(tableData, table, downloadFileName, format = 'csv') {
63
62
  const { columns = [] } = table;
64
63
  const filterColumns = columns.filter(({ cellType }) => !actionCellTypes.includes(cellType));
65
- const formatDataVal = formatColumnValues(filterColumns, tableData);
66
- const jsonse = JSON.stringify(formatDataVal);
67
- const keysToInclude = columns.filter(({ dataField }) => dataField)
68
- .map(({ dataField }) => dataField);
69
- const headers = columns.filter(({ dataField }) => dataField)
70
- .map(({ header, downloadHeader }) => (downloadHeader || header));
71
- const csv = convertToCSV(jsonse, keysToInclude, headers);
72
- const exportData = new Blob([csv], { type: 'text/csv' });
73
- const JsonURL = window.URL.createObjectURL(exportData);
74
- let tempLink = '';
75
- tempLink = document.createElement('a');
76
- tempLink.setAttribute('href', JsonURL);
77
- tempLink.setAttribute('download', createFileName(downloadFileName || ''));
64
+ let formatDataVal = formatColumnValues(filterColumns, tableData);
65
+
66
+ let fileContent;
67
+ let fileType;
68
+ let fileExtension;
69
+
70
+ if (format === 'json') {
71
+ filterColumns.forEach((column) => {
72
+ formatDataVal = JSON.parse(
73
+ JSON.stringify(formatDataVal).split(`"${column.dataField}":`).join(`"${column.header}":`),
74
+ );
75
+ });
76
+ fileContent = JSON.stringify(formatDataVal);
77
+ fileType = 'application/json';
78
+ fileExtension = 'json';
79
+ } else {
80
+ const jsonse = JSON.stringify(formatDataVal);
81
+ const keysToInclude = columns.filter(({ dataField }) => dataField)
82
+ .map(({ dataField }) => dataField);
83
+ const headers = columns.filter(({ dataField }) => dataField)
84
+ .map(({ header, downloadHeader }) => (downloadHeader || header));
85
+ fileContent = convertToCSV(jsonse, keysToInclude, headers);
86
+ fileType = 'text/csv';
87
+ fileExtension = 'csv';
88
+ }
89
+
90
+ const exportData = new Blob([fileContent], { type: fileType });
91
+ const fileURL = window.URL.createObjectURL(exportData);
92
+ const tempLink = document.createElement('a');
93
+ tempLink.setAttribute('href', fileURL);
94
+ tempLink.setAttribute('download', createFileName(downloadFileName || '', fileExtension));
78
95
  document.body.appendChild(tempLink);
79
96
  tempLink.click();
80
97
  document.body.removeChild(tempLink);