Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): Export and download resource with formatted resource name #13925

Merged
merged 1 commit into from May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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
@@ -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
@@ -0,0 +1,23 @@
import {formatDownloadName} from './download'

describe('formatDownloadName', () => [
it('formats name correctly', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 nice!

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
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