-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1612 from hackforla/sb/exportbutton
Sb/exportbutton
- Loading branch information
Showing
4 changed files
with
155 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import React, { useContext } from 'react'; | ||
import Button from '@mui/material/Button'; | ||
import PropTypes from 'proptypes'; | ||
import { connect } from 'react-redux'; | ||
import JSZip from 'jszip'; | ||
import Papa from 'papaparse'; | ||
import { saveAs } from 'file-saver'; | ||
import DbContext from '@db/DbContext'; | ||
import ddbh from '@utils/duckDbHelpers.js'; | ||
import { isEmpty } from '@utils'; | ||
import requestTypes from '../../../data/requestTypes'; | ||
|
||
// export button main function | ||
function ExportButton({ filters }) { | ||
const { conn } = useContext(DbContext); | ||
|
||
// creation zip file | ||
const downloadZip = async csvContent => { | ||
const zip = new JSZip(); | ||
zip.file('NeighborhoodData.csv', csvContent); | ||
|
||
const content = await zip.generateAsync({ type: 'blob' }); | ||
saveAs(content, '311Data.zip'); | ||
}; | ||
|
||
// data to add into zip file, queries then add results | ||
const getDataToExport = async () => { | ||
// define request status filter variable to reuse variable | ||
let requestStatusFilter = ''; | ||
|
||
if (filters.requestStatus.open === true && filters.requestStatus.closed === false) { | ||
// Only open is true, get all open requests | ||
// query = `SELECT * FROM requests WHERE Status='Open';`; | ||
requestStatusFilter = 'Open'; | ||
} else if (filters.requestStatus.closed === true && filters.requestStatus.open === false) { | ||
// Only closed is true, get all closed requests | ||
// query = `SELECT * FROM requests WHERE Status='Closed';`; | ||
requestStatusFilter = 'Closed'; | ||
} | ||
|
||
const formattedRequestTypes = requestTypes | ||
.filter(item => filters.requestTypes[item.typeId]) | ||
.map(v => `'${v.typeName}'`) | ||
.join(', '); | ||
|
||
// // in the case user chooses one neighborhood or all are selected + dates and status | ||
const query = `select * from requests where CreatedDate >= '${filters.startDate}' AND | ||
CreatedDate < '${filters.endDate}'${requestStatusFilter !== '' | ||
? ` AND Status='${requestStatusFilter}'` : ''} | ||
${filters.councilId !== null | ||
? ` AND NC='${filters.councilId}'` : ''} AND RequestType IN (${formattedRequestTypes});`; | ||
|
||
const dataToExport = await conn.query(query); | ||
const results = ddbh.getTableData(dataToExport); | ||
|
||
if (!isEmpty(results)) { | ||
// results chosen to csv | ||
const csvContent = Papa.unparse(results); | ||
downloadZip(csvContent); | ||
} else { | ||
window.alert('No 311 data available within the selected filters. Please adjust your filters and try again.'); | ||
} | ||
}; | ||
|
||
// action upon clicking | ||
const handleExport = () => { | ||
getDataToExport(filters); | ||
}; | ||
|
||
return ( | ||
<Button variant="contained" onClick={handleExport}> | ||
Export | ||
</Button> | ||
|
||
); | ||
} | ||
|
||
const mapStateToProps = state => ({ | ||
filters: state.filters, | ||
}); | ||
|
||
export default connect(mapStateToProps)(ExportButton); | ||
|
||
ExportButton.propTypes = { | ||
filters: PropTypes.shape({ | ||
startDate: PropTypes.string, | ||
endDate: PropTypes.string, | ||
councilId: PropTypes.number, | ||
requestStatus: PropTypes.shape({ | ||
open: PropTypes.bool, | ||
closed: PropTypes.bool, | ||
}), | ||
requestTypes: PropTypes.objectOf(PropTypes.bool), | ||
}).isRequired, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters