This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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 #2311 from reidmeyer/IncidentsCSV
feat(incidents): create downloadable incidents csv report
- Loading branch information
Showing
6 changed files
with
214 additions
and
29 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { getCSV, DownloadLink } from '../../../shared/util/DataHelpers' | ||
|
||
describe('Use Data Helpers util', () => { | ||
it('should construct csv', () => { | ||
const input = [ | ||
{ | ||
code: 'I-eClU6OdkR', | ||
date: '2020-09-06 12:02 PM', | ||
reportedBy: 'some user', | ||
reportedOn: '2020-09-06 12:02 PM', | ||
status: 'reported', | ||
}, | ||
] | ||
const output = getCSV(input).replace(/(\r\n|\n|\r)/gm, '') | ||
const expectedOutput = | ||
'"code","date","reportedBy","reportedOn","status""I-eClU6OdkR","2020-09-06 12:02 PM","some user","2020-09-06 12:02 PM","reported"' | ||
expect(output).toMatch(expectedOutput) | ||
}) | ||
|
||
it('should download data as expected', () => { | ||
const response = DownloadLink('data to be downloaded', 'filename.txt') | ||
|
||
const element = document.createElement('a') | ||
element.setAttribute( | ||
'href', | ||
`data:text/plain;charset=utf-8,${encodeURIComponent('data to be downloaded')}`, | ||
) | ||
element.setAttribute('download', 'filename.txt') | ||
|
||
element.style.display = 'none' | ||
expect(response).toEqual(element) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Parser } from 'json2csv' | ||
|
||
export function getCSV<T>(data: T[]): string { | ||
const fields = Object.keys(data[0]) | ||
const opts = { fields } | ||
const parser = new Parser(opts) | ||
const csv = parser.parse(data) | ||
return csv | ||
} | ||
|
||
export function DownloadLink(data: string, fileName: string) { | ||
const text = data | ||
const element = document.createElement('a') | ||
element.setAttribute('href', `data:text/plain;charset=utf-8,${encodeURIComponent(text)}`) | ||
element.setAttribute('download', fileName) | ||
|
||
element.style.display = 'none' | ||
document.body.appendChild(element) | ||
element.click() | ||
|
||
return document.body.removeChild(element) | ||
} |
08d7965
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: