@bento-core/table 1.0.1-ccdihub.7 → 1.0.1-ccdihub.71
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.
- package/dist/ExtendedView.js +20 -7
- package/dist/TableView.js +62 -6
- package/dist/header/components/CheckBoxView.js +6 -2
- package/dist/index.js +6 -0
- package/dist/pagination/CustomPagination.js +4 -2
- package/dist/toolbar/DownloadButtonView.js +169 -7
- package/dist/util/Dataformat.js +41 -1
- package/dist/util/Types.js +18 -1
- package/dist/util/downloadTable.js +183 -0
- package/package.json +4 -1
- package/src/ExtendedView.js +28 -12
- package/src/TableView.js +110 -52
- package/src/header/components/CheckBoxView.js +2 -1
- package/src/index.js +1 -0
- package/src/pagination/CustomPagination.js +3 -1
- package/src/toolbar/DownloadButtonView.js +178 -10
- package/src/util/Dataformat.js +25 -0
- package/src/util/Types.js +13 -0
- package/src/util/downloadTable.js +130 -0
|
@@ -1,25 +1,193 @@
|
|
|
1
|
-
import React
|
|
1
|
+
import React, {
|
|
2
|
+
useState,
|
|
3
|
+
useEffect,
|
|
4
|
+
useRef,
|
|
5
|
+
useCallback,
|
|
6
|
+
} from 'react';
|
|
7
|
+
import { useApolloClient } from '@apollo/client';
|
|
2
8
|
import {
|
|
3
9
|
IconButton,
|
|
4
10
|
Tooltip,
|
|
11
|
+
makeStyles,
|
|
5
12
|
} from '@material-ui/core';
|
|
13
|
+
import KeyboardArrowDownOutlinedIcon from '@material-ui/icons/KeyboardArrowDownOutlined';
|
|
6
14
|
import { CloudDownload } from '@material-ui/icons';
|
|
15
|
+
import { downloadCSV, downloadJson } from '../util/downloadTable';
|
|
16
|
+
|
|
17
|
+
// const downloadButtonStyle = {
|
|
18
|
+
// color: '#d1d2d3',
|
|
19
|
+
// marginTop: '7px',
|
|
20
|
+
// };
|
|
7
21
|
|
|
8
22
|
const DownloadButton = ({
|
|
9
|
-
|
|
23
|
+
count,
|
|
24
|
+
queryVariables,
|
|
25
|
+
table,
|
|
10
26
|
}) => {
|
|
11
|
-
|
|
12
|
-
|
|
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
|
+
|
|
44
|
+
if (table.paginationAPIField === 'filesInList') {
|
|
45
|
+
return <></>;
|
|
46
|
+
}
|
|
47
|
+
// if (count === 0) {
|
|
48
|
+
// return <CloudDownload className="disableButton" style={downloadButtonStyle} />;
|
|
49
|
+
// }
|
|
50
|
+
|
|
51
|
+
const client = useApolloClient();
|
|
52
|
+
|
|
53
|
+
async function downloadSCSVFile() {
|
|
54
|
+
const {
|
|
55
|
+
query,
|
|
56
|
+
paginationAPIField,
|
|
57
|
+
} = table;
|
|
58
|
+
|
|
59
|
+
const result = await client.query({
|
|
60
|
+
query,
|
|
61
|
+
variables: {
|
|
62
|
+
...queryVariables,
|
|
63
|
+
page: 0,
|
|
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,
|
|
92
|
+
},
|
|
93
|
+
})
|
|
94
|
+
.then((response) => {
|
|
95
|
+
if (paginationAPIField && response && response.data) {
|
|
96
|
+
return response.data[paginationAPIField];
|
|
97
|
+
}
|
|
98
|
+
return response.data;
|
|
99
|
+
});
|
|
100
|
+
downloadJson(result, table, table.downloadFileName);
|
|
13
101
|
}
|
|
14
102
|
|
|
103
|
+
const downloadTableCSV = useCallback(() => {
|
|
104
|
+
downloadSCSVFile();
|
|
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
|
+
arrowdownIconDisabled: {
|
|
160
|
+
marginLeft: '5px',
|
|
161
|
+
fill: '#00000042',
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const classes = useStyles();
|
|
166
|
+
|
|
15
167
|
return (
|
|
16
|
-
|
|
17
|
-
<Tooltip title=
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
168
|
+
<div className={classes.dropdown}>
|
|
169
|
+
<Tooltip title="Download filtered results">
|
|
170
|
+
{
|
|
171
|
+
count !== 0
|
|
172
|
+
? (
|
|
173
|
+
<IconButton onClick={handleClickButton}>
|
|
174
|
+
<CloudDownload />
|
|
175
|
+
<KeyboardArrowDownOutlinedIcon className={classes.arrowdownIcon} />
|
|
176
|
+
</IconButton>
|
|
177
|
+
)
|
|
178
|
+
: (
|
|
179
|
+
<IconButton disabled>
|
|
180
|
+
<CloudDownload />
|
|
181
|
+
<KeyboardArrowDownOutlinedIcon className={classes.arrowdownIconDisabled} />
|
|
182
|
+
</IconButton>
|
|
183
|
+
)
|
|
184
|
+
}
|
|
21
185
|
</Tooltip>
|
|
22
|
-
|
|
186
|
+
<div className={classes.dropdownList} style={{ display: listDisplay }}>
|
|
187
|
+
<div id="dropdownListItemCSV" className={classes.dropdownListItem} onClick={downloadTableCSV}>CSV</div>
|
|
188
|
+
<div id="dropdownListItemJSON" className={classes.dropdownListItem} onClick={downloadTableJson}>JSON</div>
|
|
189
|
+
</div>
|
|
190
|
+
</div>
|
|
23
191
|
);
|
|
24
192
|
};
|
|
25
193
|
|
package/src/util/Dataformat.js
CHANGED
|
@@ -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
|
+
};
|
package/src/util/Types.js
CHANGED
|
@@ -6,6 +6,10 @@ export const DISPLAY = 'DISPLAY';
|
|
|
6
6
|
export const DEFAULT = 'DEFAULT';
|
|
7
7
|
export const FORMAT_DATA = 'FORMAT_DATA';
|
|
8
8
|
export const FORMAT_BYTES = 'FORMAT_BYTES';
|
|
9
|
+
export const TRANSFORM = 'TRANSFORM';
|
|
10
|
+
export const STUDY_DOWNLOAD = 'STUDY_DOWNLOAD';
|
|
11
|
+
export const EXPAND = 'EXPAND';
|
|
12
|
+
export const DBGAP = 'DBGAP';
|
|
9
13
|
|
|
10
14
|
export const cellTypes = {
|
|
11
15
|
CUSTOM_ELEM,
|
|
@@ -17,8 +21,17 @@ export const cellTypes = {
|
|
|
17
21
|
FORMAT_DATA,
|
|
18
22
|
};
|
|
19
23
|
|
|
24
|
+
export const cellStyles = {
|
|
25
|
+
TRANSFORM,
|
|
26
|
+
EXPAND,
|
|
27
|
+
DBGAP,
|
|
28
|
+
STUDY_DOWNLOAD,
|
|
29
|
+
};
|
|
30
|
+
|
|
20
31
|
export const actionCellTypes = [CHECKBOX, DELETE];
|
|
21
32
|
|
|
33
|
+
export const notIncludedCellStyle = [STUDY_DOWNLOAD];
|
|
34
|
+
|
|
22
35
|
export const FORMAT_DATA_CELLS = [FORMAT_BYTES];
|
|
23
36
|
|
|
24
37
|
export const dataFormatTypes = {
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { formatBytes, formatColumnValues } from './Dataformat';
|
|
2
|
+
import { actionCellTypes, notIncludedCellStyle } from './Types';
|
|
3
|
+
|
|
4
|
+
export function createFileName(fileName, type) {
|
|
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
|
+
if (type === 'csv') {
|
|
27
|
+
return `${fileName} ${todaysDate} ${hours}-${minutes}-${seconds}${'.csv'}`;
|
|
28
|
+
}
|
|
29
|
+
return `${fileName} ${todaysDate} ${hours}-${minutes}-${seconds}${'.json'}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function convertToCSV(jsonse, keysToInclude, header) {
|
|
33
|
+
const objArray = jsonse;
|
|
34
|
+
// To Do empty object just print headers
|
|
35
|
+
const array = typeof objArray !== 'object' ? JSON.parse(objArray) : objArray;
|
|
36
|
+
let str = header.join(',');
|
|
37
|
+
array.map((entry, index) => {
|
|
38
|
+
let line = '';
|
|
39
|
+
keysToInclude.map((keyName) => {
|
|
40
|
+
if (line !== '') line += ',';
|
|
41
|
+
if (keyName === 'file_size') {
|
|
42
|
+
line += entry[keyName] !== null ? `"${formatBytes(entry[keyName])}"` : ' ';
|
|
43
|
+
} else if (keyName === 'age_at_diagnosis' || keyName === 'participant_age_at_collection') {
|
|
44
|
+
if (entry[keyName] === -999) {
|
|
45
|
+
line += 'Not Reported';
|
|
46
|
+
} else {
|
|
47
|
+
line += entry[keyName] !== null ? `"${entry[keyName]}"` : ' ';
|
|
48
|
+
}
|
|
49
|
+
} else if (keyName === 'last_known_survival_status' || keyName === 'sample_id') {
|
|
50
|
+
if (!entry[keyName] || entry[keyName] === '[]') {
|
|
51
|
+
line += '';
|
|
52
|
+
} else if (entry[keyName].toString().charAt(0) === '[' && entry[keyName].toString().charAt(entry[keyName].toString().length - 1) === ']') {
|
|
53
|
+
line += `"${entry[keyName].toString().substring(1, entry[keyName].length - 1)}"`;
|
|
54
|
+
} else {
|
|
55
|
+
line += `"${entry[keyName]}"`;
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
line += entry[keyName] !== null ? `"${entry[keyName]}"` : ' ';
|
|
59
|
+
}
|
|
60
|
+
return line;
|
|
61
|
+
});
|
|
62
|
+
if (index === 0) {
|
|
63
|
+
// str = header.join(',');
|
|
64
|
+
str += `\r\n${line}\r\n`;
|
|
65
|
+
} else {
|
|
66
|
+
str += `${line}\r\n`;
|
|
67
|
+
}
|
|
68
|
+
return str;
|
|
69
|
+
});
|
|
70
|
+
return str;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function downloadCSV(tableData, table, downloadFileName) {
|
|
74
|
+
const { columns = [] } = table;
|
|
75
|
+
const filterColumns = columns.filter(({ cellType }) => !actionCellTypes.includes(cellType))
|
|
76
|
+
.filter(({ cellStyle }) => !notIncludedCellStyle.includes(cellStyle));
|
|
77
|
+
const formatDataVal = formatColumnValues(filterColumns, tableData);
|
|
78
|
+
const jsonse = JSON.stringify(formatDataVal);
|
|
79
|
+
const keysToInclude = filterColumns.filter(({ dataField }) => dataField)
|
|
80
|
+
.map(({ dataField }) => dataField);
|
|
81
|
+
const headers = filterColumns.filter(({ dataField }) => dataField)
|
|
82
|
+
.map(({ header, downloadHeader }) => (downloadHeader || header));
|
|
83
|
+
const csv = convertToCSV(jsonse, keysToInclude, headers);
|
|
84
|
+
const exportData = new Blob([csv], { type: 'text/csv' });
|
|
85
|
+
const JsonURL = window.URL.createObjectURL(exportData);
|
|
86
|
+
let tempLink = '';
|
|
87
|
+
tempLink = document.createElement('a');
|
|
88
|
+
tempLink.setAttribute('href', JsonURL);
|
|
89
|
+
tempLink.setAttribute('download', createFileName(downloadFileName || '', 'csv'));
|
|
90
|
+
document.body.appendChild(tempLink);
|
|
91
|
+
tempLink.click();
|
|
92
|
+
document.body.removeChild(tempLink);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function downloadJson(tableData, table, downloadFileName) {
|
|
96
|
+
const { columns = [] } = table;
|
|
97
|
+
const filterColumns = columns.filter(({ cellType }) => !actionCellTypes.includes(cellType));
|
|
98
|
+
let formatDataVal = formatColumnValues(filterColumns, tableData);
|
|
99
|
+
formatDataVal = formatDataVal.map((entry) => {
|
|
100
|
+
let survivalStatus = entry.last_known_survival_status;
|
|
101
|
+
let sampleId = entry.sample_id;
|
|
102
|
+
if (!survivalStatus || survivalStatus === '[]') {
|
|
103
|
+
survivalStatus = '';
|
|
104
|
+
} else if (survivalStatus.toString().charAt(0) === '[' && survivalStatus.toString().charAt(survivalStatus.toString().length - 1) === ']') {
|
|
105
|
+
survivalStatus = survivalStatus.toString().substring(1, survivalStatus.length - 1);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (!sampleId || sampleId === '[]') {
|
|
109
|
+
sampleId = '';
|
|
110
|
+
} else if (sampleId.toString().charAt(0) === '[' && sampleId.toString().charAt(sampleId.toString().length - 1) === ']') {
|
|
111
|
+
sampleId = sampleId.toString().substring(1, sampleId.length - 1);
|
|
112
|
+
}
|
|
113
|
+
return { ...entry, last_known_survival_status: survivalStatus, sample_id: sampleId };
|
|
114
|
+
});
|
|
115
|
+
filterColumns.forEach((column) => {
|
|
116
|
+
formatDataVal = JSON.parse(
|
|
117
|
+
JSON.stringify(formatDataVal).split(`"${column.dataField}":`).join(`"${column.header}":`),
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
const jsonse = JSON.stringify(formatDataVal);
|
|
121
|
+
const exportData = new Blob([jsonse], { type: 'application/json' });
|
|
122
|
+
const JsonURL = window.URL.createObjectURL(exportData);
|
|
123
|
+
let tempLink = '';
|
|
124
|
+
tempLink = document.createElement('a');
|
|
125
|
+
tempLink.setAttribute('href', JsonURL);
|
|
126
|
+
tempLink.setAttribute('download', createFileName(downloadFileName || '', 'json'));
|
|
127
|
+
document.body.appendChild(tempLink);
|
|
128
|
+
tempLink.click();
|
|
129
|
+
document.body.removeChild(tempLink);
|
|
130
|
+
}
|