Skip to content

Commit

Permalink
feat(ui): Export and download resource with formatted resource name
Browse files Browse the repository at this point in the history
  • Loading branch information
ischolten committed May 14, 2019
1 parent 7b55b10 commit cd5e205
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions ui/src/shared/components/ExportOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ class ExportOverlay extends PureComponent<Props> {

private handleExport = (): void => {
const {resource, resourceName, onDismissOverlay} = this.props
const name = get(resource, 'name', resourceName)
downloadTextFile(JSON.stringify(resource, null, 1), `${name}.json`)
const name = get(resource, 'content.data.attributes.name', resourceName)
downloadTextFile(JSON.stringify(resource, null, 1), name)
onDismissOverlay()
}

Expand Down
12 changes: 11 additions & 1 deletion ui/src/shared/utils/download.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
export const formatDownloadName = (filename: string, extension: string) => {
return `${filename
.trim()
.toLowerCase()
.replace(/\s/g, '_')}${extension}`
}

export const downloadTextFile = (
text: string,
filename: string,
extension: string = '.json',
mimeType: string = 'text/plain'
) => {
const formattedName = formatDownloadName(filename, extension)

const blob = new Blob([text], {type: mimeType})
const a = document.createElement('a')

a.href = window.URL.createObjectURL(blob)
a.target = '_blank'
a.download = filename
a.download = formattedName

document.body.appendChild(a)
a.click()
Expand Down
18 changes: 18 additions & 0 deletions ui/src/shared/utils/downloads.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {formatDownloadName} from './download'

describe('formatDownloadName', () => [
it('formats name correctly', () => {
const name1 = 'My Dashboard '
const name2 = 'my_dash'

const expected1 = 'my_dashboard.json'
const expected2 = 'my_dash.json'

const extension = '.json'
const actual1 = formatDownloadName(name1, extension)
const actual2 = formatDownloadName(name2, extension)

expect(actual1).toEqual(expected1)
expect(actual2).toEqual(expected2)
}),
])

0 comments on commit cd5e205

Please sign in to comment.