@bento-core/table 1.0.1-ccdihub.55 → 1.0.1-ccdihub.57

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.
@@ -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; }
@@ -26,6 +28,22 @@ const DownloadButton = _ref => {
26
28
  queryVariables,
27
29
  table
28
30
  } = _ref;
31
+ const [listDisplay, setListDisplay] = (0, _react.useState)('none');
32
+ const dropdownSelection = (0, _react.useRef)(null);
33
+ const useOutsideAlerter = ref => {
34
+ (0, _react.useEffect)(() => {
35
+ function handleClickOutside(event) {
36
+ if (!event.target.getAttribute('class').includes('dropdownListItem')) {
37
+ setListDisplay('none');
38
+ }
39
+ }
40
+ document.addEventListener('mousedown', handleClickOutside);
41
+ return () => {
42
+ document.removeEventListener('mousedown', handleClickOutside);
43
+ };
44
+ }, [ref]);
45
+ };
46
+ useOutsideAlerter(dropdownSelection);
29
47
  if (table.paginationAPIField === 'filesInList') {
30
48
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null);
31
49
  }
@@ -45,7 +63,30 @@ const DownloadButton = _ref => {
45
63
  query,
46
64
  variables: _objectSpread(_objectSpread({}, queryVariables), {}, {
47
65
  page: 0,
48
- first: 10000
66
+ first: 10000,
67
+ order_by: table.sortBy,
68
+ sort_direction: table.sortOrder
69
+ })
70
+ }).then(response => {
71
+ if (paginationAPIField && response && response.data) {
72
+ return response.data[paginationAPIField];
73
+ }
74
+ return response.data;
75
+ });
76
+ (0, _downloadTable.downloadCSV)(result, table, table.downloadFileName);
77
+ }
78
+ async function downloadJsonFile() {
79
+ const {
80
+ query,
81
+ paginationAPIField
82
+ } = table;
83
+ const result = await client.query({
84
+ query,
85
+ variables: _objectSpread(_objectSpread({}, queryVariables), {}, {
86
+ page: 0,
87
+ first: 10000,
88
+ order_by: table.sortBy,
89
+ sort_direction: table.sortOrder
49
90
  })
50
91
  }).then(response => {
51
92
  if (paginationAPIField && response && response.data) {
@@ -57,12 +98,79 @@ const DownloadButton = _ref => {
57
98
  }
58
99
  const downloadTableCSV = (0, _react.useCallback)(() => {
59
100
  downloadSCSVFile();
60
- }, [queryVariables]);
61
- return /*#__PURE__*/_react.default.createElement(_core.Tooltip, {
62
- title: "Download filtered results as a CSV"
101
+ setListDisplay('none');
102
+ }, [queryVariables, table]);
103
+ const downloadTableJson = (0, _react.useCallback)(() => {
104
+ downloadJsonFile();
105
+ setListDisplay('none');
106
+ }, [queryVariables, table]);
107
+ const handleClickButton = () => {
108
+ if (listDisplay === 'none') {
109
+ setListDisplay('block');
110
+ } else {
111
+ setListDisplay('none');
112
+ }
113
+ };
114
+ const useStyles = (0, _core.makeStyles)({
115
+ dropdown: {
116
+ width: '60px',
117
+ height: '25px',
118
+ marginTop: '8px',
119
+ paddingLeft: '5px',
120
+ border: '0.75px solid #606060',
121
+ borderRadius: '5px',
122
+ display: 'inline-block',
123
+ position: 'relative'
124
+ },
125
+ dropdownList: {
126
+ display: 'block',
127
+ position: 'absolute',
128
+ width: '60px',
129
+ marginTop: '3px',
130
+ marginLeft: '-5px',
131
+ overflow: 'auto',
132
+ zIndex: '5',
133
+ border: '1.5px solid #41545E',
134
+ borderRadius: '5px',
135
+ background: '#ffffff'
136
+ },
137
+ dropdownListItem: {
138
+ padding: '2px 4px',
139
+ font: 'Poppins',
140
+ fontSize: '14px',
141
+ fontWeight: 400,
142
+ lineHeight: '21px',
143
+ '&:hover': {
144
+ background: '#D7D7D7',
145
+ cursor: 'pointer'
146
+ }
147
+ },
148
+ arrowdownIcon: {
149
+ marginLeft: '5px',
150
+ fill: '#606060'
151
+ }
152
+ });
153
+ const classes = useStyles();
154
+ return /*#__PURE__*/_react.default.createElement("div", {
155
+ className: classes.dropdown
156
+ }, /*#__PURE__*/_react.default.createElement(_core.Tooltip, {
157
+ title: "Download filtered results"
63
158
  }, /*#__PURE__*/_react.default.createElement(_core.IconButton, {
159
+ onClick: handleClickButton
160
+ }, /*#__PURE__*/_react.default.createElement(_icons.CloudDownload, null), /*#__PURE__*/_react.default.createElement(_KeyboardArrowDownOutlined.default, {
161
+ className: classes.arrowdownIcon
162
+ }))), /*#__PURE__*/_react.default.createElement("div", {
163
+ className: classes.dropdownList,
164
+ style: {
165
+ display: listDisplay
166
+ }
167
+ }, /*#__PURE__*/_react.default.createElement("div", {
168
+ className: classes.dropdownListItem,
64
169
  onClick: downloadTableCSV
65
- }, /*#__PURE__*/_react.default.createElement(_icons.CloudDownload, null)));
170
+ }, "CSV"), /*#__PURE__*/_react.default.createElement("div", {
171
+ className: classes.dropdownListItem,
172
+ onClick: downloadTableJson
173
+ }, "JSON")));
66
174
  };
67
175
  var _default = DownloadButton;
68
176
  exports.default = _default;
@@ -5,10 +5,11 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.convertToCSV = convertToCSV;
7
7
  exports.createFileName = createFileName;
8
+ exports.downloadCSV = downloadCSV;
8
9
  exports.downloadJson = downloadJson;
9
10
  var _Dataformat = require("./Dataformat");
10
11
  var _Types = require("./Types");
11
- function createFileName(fileName) {
12
+ function createFileName(fileName, type) {
12
13
  const date = new Date();
13
14
  const yyyy = date.getFullYear();
14
15
  let dd = date.getDate();
@@ -32,7 +33,10 @@ function createFileName(fileName) {
32
33
  if (seconds < 10) {
33
34
  seconds = "0".concat(seconds);
34
35
  }
35
- return "".concat(fileName, " ").concat(todaysDate, " ").concat(hours, "-").concat(minutes, "-").concat(seconds, '.csv');
36
+ if (type === 'csv') {
37
+ return "".concat(fileName, " ").concat(todaysDate, " ").concat(hours, "-").concat(minutes, "-").concat(seconds, '.csv');
38
+ }
39
+ return "".concat(fileName, " ").concat(todaysDate, " ").concat(hours, "-").concat(minutes, "-").concat(seconds, '.json');
36
40
  }
37
41
  function convertToCSV(jsonse, keysToInclude, header) {
38
42
  const objArray = jsonse;
@@ -66,7 +70,7 @@ function convertToCSV(jsonse, keysToInclude, header) {
66
70
  });
67
71
  return str;
68
72
  }
69
- function downloadJson(tableData, table, downloadFileName) {
73
+ function downloadCSV(tableData, table, downloadFileName) {
70
74
  const {
71
75
  columns = []
72
76
  } = table;
@@ -109,7 +113,31 @@ function downloadJson(tableData, table, downloadFileName) {
109
113
  let tempLink = '';
110
114
  tempLink = document.createElement('a');
111
115
  tempLink.setAttribute('href', JsonURL);
112
- tempLink.setAttribute('download', createFileName(downloadFileName || ''));
116
+ tempLink.setAttribute('download', createFileName(downloadFileName || '', 'csv'));
117
+ document.body.appendChild(tempLink);
118
+ tempLink.click();
119
+ document.body.removeChild(tempLink);
120
+ }
121
+ function downloadJson(tableData, table, downloadFileName) {
122
+ const {
123
+ columns = []
124
+ } = table;
125
+ const filterColumns = columns.filter(_ref6 => {
126
+ let {
127
+ cellType
128
+ } = _ref6;
129
+ return !_Types.actionCellTypes.includes(cellType);
130
+ });
131
+ const formatDataVal = (0, _Dataformat.formatColumnValues)(filterColumns, tableData);
132
+ const jsonse = JSON.stringify(formatDataVal);
133
+ const exportData = new Blob([jsonse], {
134
+ type: 'application/json'
135
+ });
136
+ const JsonURL = window.URL.createObjectURL(exportData);
137
+ let tempLink = '';
138
+ tempLink = document.createElement('a');
139
+ tempLink.setAttribute('href', JsonURL);
140
+ tempLink.setAttribute('download', createFileName(downloadFileName || '', 'json'));
113
141
  document.body.appendChild(tempLink);
114
142
  tempLink.click();
115
143
  document.body.removeChild(tempLink);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bento-core/table",
3
- "version": "1.0.1-ccdihub.55",
3
+ "version": "1.0.1-ccdihub.57",
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",
@@ -1,11 +1,18 @@
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,
11
+ makeStyles,
6
12
  } from '@material-ui/core';
13
+ import KeyboardArrowDownOutlinedIcon from '@material-ui/icons/KeyboardArrowDownOutlined';
7
14
  import { CloudDownload } from '@material-ui/icons';
8
- import { downloadJson } from '../util/downloadTable';
15
+ import { downloadCSV, downloadJson } from '../util/downloadTable';
9
16
 
10
17
  const downloadButtonStyle = {
11
18
  color: '#d1d2d3',
@@ -17,6 +24,23 @@ const DownloadButton = ({
17
24
  queryVariables,
18
25
  table,
19
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('class').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
+
20
44
  if (table.paginationAPIField === 'filesInList') {
21
45
  return <></>;
22
46
  }
@@ -38,6 +62,33 @@ const DownloadButton = ({
38
62
  ...queryVariables,
39
63
  page: 0,
40
64
  first: 10000,
65
+ order_by: table.sortBy,
66
+ sort_direction: table.sortOrder,
67
+ },
68
+ })
69
+ .then((response) => {
70
+ if (paginationAPIField && response && response.data) {
71
+ return response.data[paginationAPIField];
72
+ }
73
+ return response.data;
74
+ });
75
+ downloadCSV(result, table, table.downloadFileName);
76
+ }
77
+
78
+ async function downloadJsonFile() {
79
+ const {
80
+ query,
81
+ paginationAPIField,
82
+ } = table;
83
+
84
+ const result = await client.query({
85
+ query,
86
+ variables: {
87
+ ...queryVariables,
88
+ page: 0,
89
+ first: 10000,
90
+ order_by: table.sortBy,
91
+ sort_direction: table.sortOrder,
41
92
  },
42
93
  })
43
94
  .then((response) => {
@@ -51,16 +102,79 @@ const DownloadButton = ({
51
102
 
52
103
  const downloadTableCSV = useCallback(() => {
53
104
  downloadSCSVFile();
54
- }, [queryVariables]);
105
+ setListDisplay('none');
106
+ }, [queryVariables, table]);
107
+
108
+ const downloadTableJson = useCallback(() => {
109
+ downloadJsonFile();
110
+ setListDisplay('none');
111
+ }, [queryVariables, table]);
112
+
113
+ const handleClickButton = () => {
114
+ if (listDisplay === 'none') {
115
+ setListDisplay('block');
116
+ } else {
117
+ setListDisplay('none');
118
+ }
119
+ };
120
+
121
+ const useStyles = makeStyles({
122
+ dropdown: {
123
+ width: '60px',
124
+ height: '25px',
125
+ marginTop: '8px',
126
+ paddingLeft: '5px',
127
+ border: '0.75px solid #606060',
128
+ borderRadius: '5px',
129
+ display: 'inline-block',
130
+ position: 'relative',
131
+ },
132
+ dropdownList: {
133
+ display: 'block',
134
+ position: 'absolute',
135
+ width: '60px',
136
+ marginTop: '3px',
137
+ marginLeft: '-5px',
138
+ overflow: 'auto',
139
+ zIndex: '5',
140
+ border: '1.5px solid #41545E',
141
+ borderRadius: '5px',
142
+ background: '#ffffff',
143
+ },
144
+ dropdownListItem: {
145
+ padding: '2px 4px',
146
+ font: 'Poppins',
147
+ fontSize: '14px',
148
+ fontWeight: 400,
149
+ lineHeight: '21px',
150
+ '&:hover': {
151
+ background: '#D7D7D7',
152
+ cursor: 'pointer',
153
+ },
154
+ },
155
+ arrowdownIcon: {
156
+ marginLeft: '5px',
157
+ fill: '#606060',
158
+ },
159
+ });
160
+
161
+ const classes = useStyles();
55
162
 
56
163
  return (
57
- <Tooltip title="Download filtered results as a CSV">
58
- <IconButton
59
- onClick={downloadTableCSV}
60
- >
61
- <CloudDownload />
62
- </IconButton>
63
- </Tooltip>
164
+ <div className={classes.dropdown}>
165
+ <Tooltip title="Download filtered results">
166
+ <IconButton
167
+ onClick={handleClickButton}
168
+ >
169
+ <CloudDownload />
170
+ <KeyboardArrowDownOutlinedIcon className={classes.arrowdownIcon} />
171
+ </IconButton>
172
+ </Tooltip>
173
+ <div className={classes.dropdownList} style={{ display: listDisplay }}>
174
+ <div className={classes.dropdownListItem} onClick={downloadTableCSV}>CSV</div>
175
+ <div className={classes.dropdownListItem} onClick={downloadTableJson}>JSON</div>
176
+ </div>
177
+ </div>
64
178
  );
65
179
  };
66
180
 
@@ -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();
@@ -23,7 +23,10 @@ export function createFileName(fileName) {
23
23
 
24
24
  if (seconds < 10) { seconds = `0${seconds}`; }
25
25
 
26
- return `${fileName} ${todaysDate} ${hours}-${minutes}-${seconds}${'.csv'}`;
26
+ if (type === 'csv') {
27
+ return `${fileName} ${todaysDate} ${hours}-${minutes}-${seconds}${'.csv'}`;
28
+ }
29
+ return `${fileName} ${todaysDate} ${hours}-${minutes}-${seconds}${'.json'}`;
27
30
  }
28
31
 
29
32
  export function convertToCSV(jsonse, keysToInclude, header) {
@@ -59,7 +62,7 @@ export function convertToCSV(jsonse, keysToInclude, header) {
59
62
  return str;
60
63
  }
61
64
 
62
- export function downloadJson(tableData, table, downloadFileName) {
65
+ export function downloadCSV(tableData, table, downloadFileName) {
63
66
  const { columns = [] } = table;
64
67
  const filterColumns = columns.filter(({ cellType }) => !actionCellTypes.includes(cellType));
65
68
  const formatDataVal = formatColumnValues(filterColumns, tableData);
@@ -74,7 +77,23 @@ export function downloadJson(tableData, table, downloadFileName) {
74
77
  let tempLink = '';
75
78
  tempLink = document.createElement('a');
76
79
  tempLink.setAttribute('href', JsonURL);
77
- tempLink.setAttribute('download', createFileName(downloadFileName || ''));
80
+ tempLink.setAttribute('download', createFileName(downloadFileName || '', 'csv'));
81
+ document.body.appendChild(tempLink);
82
+ tempLink.click();
83
+ document.body.removeChild(tempLink);
84
+ }
85
+
86
+ export function downloadJson(tableData, table, downloadFileName) {
87
+ const { columns = [] } = table;
88
+ const filterColumns = columns.filter(({ cellType }) => !actionCellTypes.includes(cellType));
89
+ const formatDataVal = formatColumnValues(filterColumns, tableData);
90
+ const jsonse = JSON.stringify(formatDataVal);
91
+ const exportData = new Blob([jsonse], { type: 'application/json' });
92
+ const JsonURL = window.URL.createObjectURL(exportData);
93
+ let tempLink = '';
94
+ tempLink = document.createElement('a');
95
+ tempLink.setAttribute('href', JsonURL);
96
+ tempLink.setAttribute('download', createFileName(downloadFileName || '', 'json'));
78
97
  document.body.appendChild(tempLink);
79
98
  tempLink.click();
80
99
  document.body.removeChild(tempLink);