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.
feat(incidentscsv): made functions more abstract
made getcsv and downloadlink functions re #2292
- Loading branch information
Showing
2 changed files
with
25 additions
and
16 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
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) | ||
} |