-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add redux related code for exporting csv or json * Add conversion of bands in csv/json download * Use redux for the export of csv/json * Add tests for export.js util * Fix ESLint error (define before use)
- Loading branch information
1 parent
6696037
commit 204afec
Showing
8 changed files
with
228 additions
and
38 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,65 @@ | ||
import { FORMING_CSV, FORMING_JSON, SET_CSV_ERROR_MESSAGE, SET_JSON_ERROR_MESSAGE } from '../constants/ExportConstants'; | ||
import { convertBands, formCSV } from '../utils/export'; | ||
import config from '../config/export'; | ||
|
||
const { FILE_NAME } = config; | ||
|
||
const setForming = (type, sending) => ({ type, sending }); | ||
const setErrorMessage = (type, message) => ({ type, message }); | ||
|
||
/** | ||
* @const exportCSV - Export the search results as a CSV | ||
* | ||
* @param {Array} results - The array of business objects | ||
*/ | ||
export const exportCSV = (results) => (dispatch) => { | ||
dispatch(setForming(FORMING_CSV, true)); | ||
dispatch(setErrorMessage(SET_CSV_ERROR_MESSAGE, '')); | ||
|
||
setTimeout(() => { | ||
Promise.all(convertBands(results)).then(res => { | ||
const header = 'UBRN,Business Name,PostCode,Industry Code,Legal Status,Trading Status,Turnover,Employment,Company Reference Number'; | ||
const csv = formCSV(header, res); | ||
const uri = `data:text/csv;charset=utf-8,${escape(csv)}`; | ||
const link = document.createElement('a'); | ||
link.href = uri; | ||
link.download = `${FILE_NAME}.csv`; | ||
dispatch(setForming(FORMING_CSV, false)); | ||
link.click(); | ||
}).catch(() => { | ||
dispatch(setErrorMessage(SET_CSV_ERROR_MESSAGE, 'Error: Unable to download CSV file.')); | ||
}); | ||
}, 0); | ||
}; | ||
|
||
|
||
/** | ||
* @const exportJSON - Export the search results as JSON | ||
* | ||
* @param {Array} results - The array of business objects | ||
*/ | ||
export const exportJSON = (results) => (dispatch) => { | ||
dispatch(setForming(FORMING_JSON, true)); | ||
dispatch(setErrorMessage(SET_JSON_ERROR_MESSAGE, '')); | ||
|
||
setTimeout(() => { | ||
Promise.all(convertBands(results)).then(res => { | ||
// There is an issue with a.click() when the JSON string to append to the DOM | ||
// is too long, so we use a workaround from below. | ||
// https://stackoverflow.com/a/19328891 | ||
const a = document.createElement('a'); | ||
document.body.appendChild(a); | ||
a.style = 'display: none'; | ||
const json = JSON.stringify(res, null, 2); | ||
const blob = new Blob([json], { type: 'octet/stream' }); | ||
const url = window.URL.createObjectURL(blob); | ||
a.href = url; | ||
a.download = `${FILE_NAME}.json`; | ||
dispatch(setForming(FORMING_JSON, false)); | ||
a.click(); | ||
window.URL.revokeObjectURL(url); | ||
}).catch(() => { | ||
dispatch(setErrorMessage(SET_JSON_ERROR_MESSAGE, 'Error: Unable to download JSON file.')); | ||
}); | ||
}, 0); | ||
}; |
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,4 @@ | ||
export const FORMING_CSV = 'FORMING_CSV'; | ||
export const FORMING_JSON = 'FORMING_JSON'; | ||
export const SET_CSV_ERROR_MESSAGE = 'SET_CSV_ERROR_MESSAGE'; | ||
export const SET_JSON_ERROR_MESSAGE = 'SET_JSON_ERROR_MESSAGE'; |
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,45 @@ | ||
import { FORMING_CSV, FORMING_JSON, SET_CSV_ERROR_MESSAGE, SET_JSON_ERROR_MESSAGE } from '../constants/ExportConstants'; | ||
|
||
const initialState = { | ||
formingCsv: false, | ||
formingJson: false, | ||
csvErrorMessage: '', | ||
jsonErrorMessage: '', | ||
}; | ||
|
||
/** | ||
* @const exportReducer - The reducer to the exporting of results to CSV/JSON | ||
* | ||
* @param {Object} state - This current reducer state | ||
* @param {Object} action - An action which holds the type and any data | ||
* | ||
* @return {Object} - The new state (after the action has been applied) | ||
*/ | ||
const exportReducer = (state = initialState, action) => { | ||
switch (action.type) { | ||
case FORMING_CSV: | ||
return Object.assign({}, state, { | ||
...state, | ||
formingCsv: action.sending, | ||
}); | ||
case FORMING_JSON: | ||
return Object.assign({}, state, { | ||
...state, | ||
formingJson: action.sending, | ||
}); | ||
case SET_CSV_ERROR_MESSAGE: | ||
return Object.assign({}, state, { | ||
...state, | ||
csvErrorMessage: action.message, | ||
}); | ||
case SET_JSON_ERROR_MESSAGE: | ||
return Object.assign({}, state, { | ||
...state, | ||
jsonErrorMessage: action.message, | ||
}); | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export default exportReducer; |
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
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
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
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
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