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 9cb2b33
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
1. [#13835](https://github.com/influxdata/influxdb/pull/13835): Render checkboxes in query builder tag selection lists
1. [#13856](https://github.com/influxdata/influxdb/pull/13856): Fix jumbled card text in Telegraf configuration wizard
1. [#13888](https://github.com/influxdata/influxdb/pull/13888): Change scrapers in scrapers list to be resource cards
1. [#13925](https://github.com/influxdata/influxdb/pull/13925): Export and download resource with formatted resource name with no spaces

## v2.0.0-alpha.9 [2019-05-01]

Expand Down
4 changes: 2 additions & 2 deletions ui/src/shared/components/CSVExportButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class CSVExportButton extends PureComponent<StateProps, {}> {
const {files} = this.props
const csv = files.join('\n\n')
const now = moment().format('YYYY-MM-DD-HH-mm')
const filename = `${now} Chronograf Data.csv`
const filename = `${now} Chronograf Data`

downloadTextFile(csv, filename, 'text/csv')
downloadTextFile(csv, filename, '.csv', 'text/csv')
}
}

Expand Down
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, '.json')
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,
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
23 changes: 23 additions & 0 deletions ui/src/shared/utils/downloads.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {formatDownloadName} from './download'

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

const expected1 = 'my_dashboard.json'
const expected2 = 'my_dash.json'
const expected3 = 'systemconfig.toml'

const extension = '.json'
const extension2 = '.toml'
const actual1 = formatDownloadName(name1, extension)
const actual2 = formatDownloadName(name2, extension)
const actual3 = formatDownloadName(name3, extension2)

expect(actual1).toEqual(expected1)
expect(actual2).toEqual(expected2)
expect(actual3).toEqual(expected3)
}),
])
2 changes: 1 addition & 1 deletion ui/src/telegrafs/components/TelegrafConfigOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class TelegrafConfigOverlay extends PureComponent<Props> {
telegrafConfig,
telegraf: {name},
} = this.props
downloadTextFile(telegrafConfig, `${name || 'config'}.toml`)
downloadTextFile(telegrafConfig, name || 'config', '.toml')
}
}

Expand Down

0 comments on commit 9cb2b33

Please sign in to comment.