-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
UI: download generated pki key #18381
Changes from 9 commits
9b654bb
5d0a20e
b727d12
7a2e422
d7b91ff
f329b01
7bef73f
5f9d07b
48c8c92
fa44075
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import { action } from '@ember/object'; | ||
import Component from '@glimmer/component'; | ||
import { action } from '@ember/object'; | ||
import { inject as service } from '@ember/service'; | ||
import errorMessage from 'vault/utils/error-message'; | ||
/** | ||
* @module DownloadButton | ||
* DownloadButton components are an action button used to download data. Both the action text and icon are yielded. | ||
* | ||
* * NOTE: when using in an engine, remember to add the 'download' service to its dependencies (in /engine.js) and map to it in /app.js | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❗ great reminder! |
||
* [ember-docs](https://ember-engines.com/docs/services) | ||
* @example | ||
* ```js | ||
* <DownloadButton | ||
|
@@ -18,45 +21,39 @@ import Component from '@glimmer/component'; | |
* Download | ||
* </DownloadButton> | ||
* ``` | ||
* @param {string} data - data to download | ||
* @param {string} [filename] - name of file that prefixes the ISO timestamp generated at download | ||
* @param {string} [data] - data to download | ||
* @param {string} [extension='txt'] - file extension, the download service uses this to determine the mimetype | ||
* @param {boolean} [stringify=false] - argument to stringify the data before passing to the File constructor | ||
* @param {string} [filename] - name of file that prefixes the ISO timestamp generated when download | ||
* @param {string} [mime='text/plain'] - media type to be downloaded | ||
* @param {string} [extension='txt'] - file extension | ||
*/ | ||
|
||
export default class DownloadButton extends Component { | ||
get extension() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wanted order of getters to be consistent and match arg order... |
||
return this.args.extension || 'txt'; | ||
} | ||
|
||
get mime() { | ||
return this.args.mime || 'text/plain'; | ||
} | ||
@service download; | ||
@service flashMessages; | ||
|
||
get filename() { | ||
const defaultFilename = `${new Date().toISOString()}.${this.extension}`; | ||
return this.args.filename ? this.args.filename + '-' + defaultFilename : defaultFilename; | ||
const timestamp = new Date().toISOString(); | ||
return this.args.filename ? this.args.filename + '-' + timestamp : timestamp; | ||
} | ||
|
||
get data() { | ||
get content() { | ||
if (this.args.stringify) { | ||
return JSON.stringify(this.args.data, null, 2); | ||
} | ||
return this.args.data; | ||
} | ||
|
||
// TODO refactor and call service instead | ||
get extension() { | ||
return this.args.extension || 'txt'; | ||
} | ||
|
||
@action | ||
handleDownload() { | ||
const { document, URL } = window; | ||
const downloadElement = document.createElement('a'); | ||
const content = new File([this.data], this.filename, { type: this.mime }); | ||
downloadElement.download = this.filename; | ||
downloadElement.href = URL.createObjectURL(content); | ||
document.body.appendChild(downloadElement); | ||
downloadElement.click(); | ||
URL.revokeObjectURL(downloadElement.href); | ||
downloadElement.remove(); | ||
try { | ||
this.download.miscExtension(this.filename, this.content, this.extension); | ||
this.flashMessages.info(`Downloading ${this.filename}`); | ||
} catch (error) { | ||
this.flashMessages.danger(errorMessage(error, 'There was a problem downloading. Please try again.')); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,10 @@ export default class PkiEngine extends Engine { | |
'namespace', | ||
'path-help', | ||
'router', | ||
'secret-mount-path', | ||
'store', | ||
'version', | ||
'wizard', | ||
'secret-mount-path', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alphabetized? 🤷 |
||
], | ||
externalRoutes: ['secrets'], | ||
}; | ||
|
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.
reordered args so that download service and button are consistent:
filename
,content/data
,extension