Skip to content

Commit

Permalink
feat: add csv download of currrent table
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael.Taylor committed Jan 6, 2022
1 parent 0eaed99 commit 4b0d87b
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 16 deletions.
67 changes: 53 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@testing-library/user-event": "^12.1.10",
"electron": "^15.3.0",
"http-proxy-middleware": "^0.19.1",
"json2csv": "^5.0.6",
"lodash": "^4.17.21",
"minisearch": "^3.1.0",
"react": "^17.0.2",
Expand All @@ -30,6 +31,7 @@
"react-scripts": "4.0.3",
"redux": "^4.1.2",
"redux-thunk": "^2.4.0",
"socket.io-client": "^4.4.1",
"styled-components": "^5.3.3",
"updeep": "^1.2.1",
"web-vitals": "^1.0.1"
Expand Down
16 changes: 15 additions & 1 deletion src/pages/Projects/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useSelector, useDispatch } from 'react-redux';
import { withRouter, useHistory } from 'react-router-dom';
import styled from 'styled-components';
import { FormattedMessage } from 'react-intl';
import { jsonToCsv } from '../../utils/csvUtils';
import constants from '../../constants';

import {
Expand Down Expand Up @@ -169,6 +170,17 @@ const Projects = withRouter(() => {
return null;
}

const downloadTxtFile = () => {
const element = document.createElement('a');
const file = new Blob([jsonToCsv(climateWarehouseStore.projects)], {
type: 'text/plain',
});
element.href = URL.createObjectURL(file);
element.download = 'climateWarehouse.csv';
document.body.appendChild(element); // Required for this to work in FireFox
element.click();
};

return (
<StyledSectionContainer>
<StyledHeaderContainer>
Expand Down Expand Up @@ -224,7 +236,9 @@ const Projects = withRouter(() => {
})`}
/>
</Tabs>
<DownloadIcon />
<div onClick={downloadTxtFile}>
<DownloadIcon />
</div>
</StyledSubHeaderContainer>
<StyledBodyContainer>
<TabPanel value={tabValue} index={0}>
Expand Down
16 changes: 15 additions & 1 deletion src/pages/Units/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useSelector, useDispatch } from 'react-redux';
import { withRouter, useHistory } from 'react-router-dom';
import styled from 'styled-components';
import { FormattedMessage } from 'react-intl';
import { jsonToCsv } from '../../utils/csvUtils';
import constants from '../../constants';

import {
Expand Down Expand Up @@ -170,6 +171,17 @@ const Units = withRouter(() => {
return null;
}

const downloadTxtFile = () => {
const element = document.createElement('a');
const file = new Blob([jsonToCsv(climateWarehouseStore.units)], {
type: 'text/plain',
});
element.href = URL.createObjectURL(file);
element.download = 'climateWarehouse.csv';
document.body.appendChild(element); // Required for this to work in FireFox
element.click();
};

return (
<StyledSectionContainer>
<StyledHeaderContainer>
Expand Down Expand Up @@ -225,7 +237,9 @@ const Units = withRouter(() => {
})`}
/>
</Tabs>
<DownloadIcon />
<div onClick={downloadTxtFile}>
<DownloadIcon />
</div>
</StyledSubHeaderContainer>
<StyledBodyContainer>
<TabPanel value={tabValue} index={0}>
Expand Down
1 change: 1 addition & 0 deletions src/store/actions/climateWarehouseActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const getClimateWarehouseTable = (
if (response.ok) {
dispatch(setConnectionCheck(true));
const results = await response.json();

dispatch({
type: action,
payload: results,
Expand Down
6 changes: 6 additions & 0 deletions src/utils/csvUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { Parser } = require('json2csv');

export const jsonToCsv = json => {
const json2csvParser = new Parser();
return json2csvParser.parse(json);
};

0 comments on commit 4b0d87b

Please sign in to comment.